Order close not working correctly.

 

Is this the correct implementation of OrderClose () ?

 for( int ii=0;ii<OrdersTotal();ii++ )
                    {
                     if(OrderSelect( ii, SELECT_BY_POS, MODE_TRADES ))
                       {
                       
                       if( OrderType() == OP_BUY && OrderMagicNumber() == MagicStart && OrderSymbol() == symb)                         
                       {
                         
                       int tikt= OrderTicket();
                       
                       if(!OrderClose(tikt,OrderLots(),bid,4,clrPurple))
                        {Print ("Close Error", GetLastError());}
                       }
                       
                       if( OrderType() == OP_SELL && OrderMagicNumber() == MagicStart && OrderSymbol() == symb)                         
                       {
                         
                       int tikt= OrderTicket();
                       
                       if(!OrderClose(tikt,OrderLots(),ask,4,clrPurple))
                       {Print ("Close Error", GetLastError());}
                          
                       }
                         
                       }
                    } 

It works for the first time and then it stops working , 
i am using this code right before opening a trade , for example if there is a signal for buy then it closes the sell first and then opens a buy and if there is a sell signal then it closes a sell first and then opens a buy , but it only does this for the first time and wont work after that.

let say there is a sell signal then it will close the buy and open the sell , but when there is a second signal for a buy then it wont close the sell neither will it open a buy.

there is no error in the experts tab , the only thing i receive in the experts tab is a message like this: Positions order mismatch
it does not appear like an error or a warning , it just appears as a message 


 
for( int ii=0;ii<OrdersTotal();ii++ )

In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:

  1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 programming forum
  2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
              CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
              MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE

  3. and check OrderSelect in case later positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().