Closing all Pending Orders on new bar
because with i=0, you close first order with index [0].
second order changes from index[1] to index[0]
Then with next loop .. oops, no more orders.
Change
for(i=0;i<OrdersTotal();i++)
to
for(i=OrdersTotal()-1;i>=0;i--)
for(int pos=OrdersTotal()-1; pos >= 0; i--) if( OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber() == MagicNumber // Only my orders, && OrderSymbol() == Symbol() // on my chart, && OrderType() > OP_SELL){ // pending only. static OP.text[]={ "OP_BUY", "OP_SELL", "OP_BUYLIMIT", "OP_SELLLIMIT", "OP_BUYSTOP", "OP_SELLSTOP" }; if (OrderDelete(OrderTicket())) Print(OP.text[OrderType()]," Deleted"); else Print( "Error Deleting ",OP.text[OrderType()]," ",GetLastError()); }
because with i=0, you close first order with index [0].
second order changes from index[1] to index[0]
Then with next loop .. oops, no more orders.
Change
to
Duh... Thanks so much. This is why I ask the experts. Thanks again.
Count down and always test return codes.
Thanks for your help. Can you please explain OrderType() > OP_SELL ? Also the use of static OP.text[] . Your code is very efficient.
Thanks for your help. Can you please explain OrderType() > OP_SELL ? Also the use of static OP.text[] . Your code is very efficient.
Many programmers writes like >OP_SELL to show they high "skill" of codding. :))))
It is for use less PC resources. But code becomes bad readed.
OP_BUY | 0 | Buy operation |
OP_SELL | 1 | Sell operation |
OP_BUYLIMIT | 2 | Buy limit pending order |
OP_SELLLIMIT | 3 | Sell limit pending order |
OP_BUYSTOP | 4 | Buy stop pending order |
OP_SELLSTOP | 5 | Sell stop pending order |
Could someone tell me why the code below only closes one open trade per bar? e.g. I have two open orders. On each new bar ClosePending() is called. However, only one will close this bar and the second closes the next bar? Thanks in advance.