Sorry guys! Back again with another problem. I have the following code that has to select all my open Sell Orders and then close them under certain conditions.
It works well if there is just one open order, but as soon as I have two (or more ?) open Sell Orders it only closes the last one.
How can I change the code so that it cycles through and ALL open Sell orders are closed?
Note: The CCIMiddleS value is read with shift = 1 so it should stay the same for two cycles or many more, as will the SellMiddleLevel variable which is set to a certain fixed value before run.
Sorry, I can't follow your code with the way you have your braces . . .
Count = 0; for (Count = OrdersTotal()-1; Count >= 0; Count--) if ( OrderSelect(Count, SELECT_BY_POS) && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber ) { SellOrder = OrderTicket(); double CCIMiddleS = iCCI(NULL,PERIOD_H1,CCI,PRICE_TYPICAL,1); if (CCIMiddleS < SellMiddleLevel) SellMiddle = true; if (SellMiddle == true && CCIMiddleS > 0) { while(IsTradeContextBusy()) Sleep(10); Closed = OrderClose(SellOrder, OrderLots(), Ask, UseSlippage, Red); // Error handling if(Closed == false) { ErrorCode = GetLastError(); ErrDesc = ErrorDescription(ErrorCode); ErrAlert = StringConcatenate(" Close Sell Order Error - Error ",ErrorCode,": ",ErrDesc, " Ask is ", Ask, " SellTakeProfit is ", SellTakeProfit); Alert(ErrAlert); ErrLog = StringConcatenate("OrderTicket: ",OrderTicket()); Print(ErrLog); } if (Closed == true) SellMiddle = false; } }
OK, I think this is happening . . . .
This is not true . . .
( CCIMiddleS < SellMiddleLevel )
but when the for loop starts . . .
SellMiddle = true;
so it doesn't matter for the first run through the loop that the condition fails as SellMiddle is already true . . . but after the 1st order is closed, the oldest one in the pool, SellMiddle is set to false and because the condition fails it is never setback to true.
To test this theory add this . . .
SellMiddle = false;
. . before the loop and you should find that none of the orders are closed.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Sorry guys! Back again with another problem. I have the following code that has to select all my open Sell Orders and then close them under certain conditions.
It works well if there is just one open order, but as soon as I have two (or more ?) open Sell Orders it only closes the last one.
How can I change the code so that it cycles through and ALL open Sell orders are closed?
Note: The CCIMiddleS value is read with shift = 1 so it should stay the same for two cycles or many more, as will the SellMiddleLevel variable which is set to a certain fixed value before run.