Exiting 'FOR' loop prematurely...

 
Hi. This has me completely baffled. The code below simply closes a basket of currency pairs that have the same magic number. The OrdersTotal() function reports the correct number of open orders, whose value is used in the 'FOR' loop. However, this loop is often exited prematurely, especially if there are a lot of orders open - as if there is a 'break' command being executed! Also below is a sample of the debugging - note that the number of open orders reported by OrdersTotal() is correct (91), but the 'FOR' iterations is not (58). I would really appreciate it if someone would have a look at it. :)

Is this something obvious I am overlooking or is this another Metatrader bug???

for (i=0; i<OrdersTotal(); i++)
   {
    Print(i);
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    msg = msg + DoubleToStr(OrderTicket(),0) + " ";
    
    if (OrderMagicNumber() == SecMagic && OrderCloseTime() == 0)
    {
     if (OrderType() == OP_BUY)
      Price = MarketInfo(OrderSymbol(),MODE_BID);
     else if (OrderType() == OP_SELL)
      Price = MarketInfo(OrderSymbol(),MODE_ASK);
      
     ticket = OrderTicket();
     if (OrderClose(OrderTicket(),OrderLots(),Price,3,CLR_NONE))
     {
      if (OrderSelect(ticket,SELECT_BY_TICKET))
      {
       ClosedProfit = ClosedProfit + OrderProfit() + OrderSwap() + OrderCommission();
       if (OrderSwap() > 0)
        ClosedPosSwap = ClosedPosSwap + OrderSwap();
       else ClosedNegSwap = ClosedNegSwap + OrderSwap();
       Print("CTrader - ",SecMagic," Secondary order closed successfully...");
       ClosedOrdersTotal++;
      }
     }
     else Print("*** WARNING *** - Error closing Order ",SecMagic," - ",GetLastError());
    }    
   }



2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: All Open Tickets found = (91) - 2240780 2240835 2251041 2251791 2251984... etc.
2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: *** WARNING *** - Failed to Close 7 Orders...
2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: CTrader - 242628 Secondary order closed successfully...
2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: close #2252793 buy 0.40 GBPUSD at 2.1128 at price 2.1092
2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: 58
2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: CTrader - 242628 Secondary order closed successfully...
2007.11.09 08:22:26	CTrader_v2 EURUSD,H1: close #2252791 buy 0.40 EURCHF at 1.6512 at price 1.6532
2007.11.09 08:22:25	CTrader_v2 EURUSD,H1: 57
2007.11.09 08:22:25	CTrader_v2 EURUSD,H1: CTrader - 242628 Secondary order closed successfully...
2007.11.09 08:22:25	CTrader_v2 EURUSD,H1: close #2252789 buy 0.40 USDCAD at 0.9274 at price 0.9299
2007.11.09 08:22:24	CTrader_v2 EURUSD,H1: 56
2007.11.09 08:22:24	CTrader_v2 EURUSD,H1: CTrader - 242628 Secondary order closed successfully...



 
Use (for i = OrdersTotal()-1; i >= 0; i--)

when you close an order, OrdersTotal() becomes smaller.

If you have trades a, b and c, OT = 3

Close a, now OT = 2

and you miss closing b in your loop because b is "order #0"
 
Use (for i = OrdersTotal()-1; i >= 0; i--)

when you close an order, OrdersTotal() becomes smaller.

If you have trades a, b and c, OT = 3

Close a, now OT = 2

and you miss closing b in your loop because b is "order #0"





Duh! - hell could have frozen over and that wouldn't have occurred to me :D

Thanks, it is really, really appreciated :)
 
Caught me in the beginning, too
Reason: