Close Buy Order when current close price is smaller than the lowest close of the previous 10 bars (MQL4)
Function check criteria for each market order and call 'order close' function, if criteria meet. Return the number of closed orders
int Close_Orders(int i_Magic = -1) { int i_Order = OrdersTotal(), i_Closed_Orders_Count = 0 ; while(i_Order-- > 0) { if(!OrderSelect(i_Order, SELECT_BY_POS)) continue; if(OrderType() > 1) continue; if(OrderSymbol() != _Symbol) continue; if(i_Magic > 0 && OrderMagicNumber() != i_Magic) continue; if(OrderType() == OP_BUY) { if(Bid > Low[iLowest( _Symbol, PERIOD_CURRENT, MODE_CLOSE, 10, iBarShift(_Symbol, PERIOD_CURRENT, OrderOpenPrice()) )]) continue; } else { if(Ask < High[iHighest( _Symbol, PERIOD_CURRENT, MODE_CLOSE, 10, iBarShift(_Symbol, PERIOD_CURRENT, OrderOpenPrice()) )]) continue; } // criteria meet if we are here // call your 'order close' function: if(Close_Order(OrderTicket())) i_Closed_Orders_Count++; } return(i_Closed_Orders_Count); }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello forum, good day.
I´m working on a Market Orders EA that may open only one order at a time, but there may be several open orders during the day. Up to what I have, all the orders remain open and I´m having trouble on closing each order separately when certain criteria is met.
The criteria is as follows: close a buy order if the current close price is less than or equal to the lowest close price of the 10 previous bars from which the order was opened. Here is what I've got so far:
Is this the correct way to close a market order, if not, how could I improve this?
My guess for the needed criteria is that I must associate the order's ticket to a variable holding the lowest close price of the previous 10 bars. If so, how could it be done? Your help will be much appreciated.
Best regards and thank you,
codeMolecules