Exiting 'FOR' loop prematurely...
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"
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"
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
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()); } }