all pending orders should be deleted if an active order is available ...
what I'm doing wrong?
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() ){ nOrders++; //count your buy and sell separate } if (OrdersTotal() > 0 && OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) //OrdersTotal() is not the same as nOrders { OrderSelect(pos,SELECT_BY_POS,MODE_TRADES); if(OrderType()==OP_SELLSTOP) { OrderDelete(OrderTicket()); Print("Deleting SELL_STOP"," Ordertype:",OrderType()); return(1); } if(OrderType()==OP_BUYSTOP) { OrderDelete(OrderTicket()); Print("Deleting BUY_STOP"," Ordertype:",OrderType()); return(1); } }The deleting of pendingtrades has to be done also inside a loop
int nOpenOrders = 0; for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair && OrderType() <= OP_SELL // and only open orders. ){ nOpenOrders++; } if (nOpenOrders > 0) for(iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair && OrderType() > OP_SELL // and only pending orders. ){ if(!OrderDelete(OrderTicket()) Alert("OrderDelete failed: ", GetLastError()); }
int nOpenOrders = 0; int EAtrades =0; for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol) // and my pair { if(OrderType() <= OP_SELL) { nOpenOrders++; } // only open orders. EAtrades++; // all trades this EA } if (nOpenOrders > 0 && EAtrades > nOpenOrders) for(iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair && OrderType() > OP_SELL // and only pending orders. ){ if(!OrderDelete(OrderTicket()) Alert("OrderDelete failed: ", GetLastError()); }not needed doing the loop again if there is no pending trade
all pending orders should be deleted if an active order is available ...
what I'm doing wrong?
pending orders could occur earlier or later in the list than the active order. This code compiles but is not guaranteed to be bug free ...
int Magic = 3; int start(){ bool killPending=false; for( int pos = OrdersTotal()-1; pos >= 0 ; pos-- ){ if( OrderSelect(pos, SELECT_BY_POS) ){ if( OrderMagicNumber()==Magic && OrderSymbol()==Symbol() ){ if( OrderType()== OP_BUY || OrderType()==OP_SELL ){ // we have a valid open order killPending=true; break; // we have found what we want so why bother carrying on } } } } if( killPending ){ for( pos = OrdersTotal()-1; pos >= 0 ; pos-- ){ if( OrderSelect(pos, SELECT_BY_POS) ){ if( OrderMagicNumber()==Magic && OrderSymbol()==Symbol() ){ if( OrderType()==OP_SELLSTOP ){ OrderDelete(OrderTicket()); Print("Deleting SELL_STOP"," Ordertype:",OrderType()); } if( OrderType()==OP_BUYSTOP ){ OrderDelete(OrderTicket()); Print("Deleting BUY_STOP"," Ordertype:",OrderType()); } // ... other types, buy_limit etc } } } } return( 0 ); }
Of course the order delete could fail, so more testing is required.
pending orders could occur earlier in the list than the active order. This code compiles but is not guaranteed to be bug free ...
Of course the order delete could fail, so more testing is required.
Your solution makes it with every new tick checking all open orders again to close pending trades if an active order is available ...
So it is not a faster solution..... you take also a whole loop to check every tick
Also why checking type of pending if you wanna close them all ?
With return( 1 ); I think you are ending the loop before you checked them all
with
if (nOpenOrders > 0 && EAtrades > nOpenOrders)it's pending orders can only be deleted if one of the pending trades is triggerred to a normal sell or a buy and there are still some pending trades open
With return( 1 ); I think you are ending the loop before you checked them all
Your solution makes it with every new tick checking all open orders again to close pending trades if an active order is available ...
Why not . . .
int Magic = 3; string type[] = {"", "", "OP_BUYLIMIT", "OP_SELLLIMIT", "OP_BUYSTOP", "OP_SELLSTOP" }; int start() { bool killPending = false; for( int pos = OrdersTotal()-1; pos >= 0 ; pos-- ) { if( OrderSelect(pos, SELECT_BY_POS) ) { if( OrderMagicNumber() == Magic && OrderSymbol() == Symbol() ) { if( OrderType() < OP_BUYLIMIT && !killPending) // we have a valid open order { killPending = true; pos = OrdersTotal() } if (killPending) { if (OrderType() > OP_SELL && OrderType() <= OP_SELLSTOP ) { if OrderDelete(OrderTicket()) Print("Deleted order: ", type[OrderType()] ); } } } } } } return( 0 ); }
Not tested or tried to compile it . . .
I'm not sure what point you are making here. On every tick we have to check all open orders in case one is relevant. A pending order could have been converted to a market order so the OrdersTotal would not have changed. There is no need to check for OrdersTotal greater than 0 because the first test in the For loop does that already. What am I missing?
Thanks for asking. We improve our coding only that way.... Also I can...
What I mean that is that in this part of the coding given
int nOpenOrders = 0; int EAtrades =0; for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol) // and my pair { if(OrderType() <= OP_SELL) { nOpenOrders++; } // only open orders. EAtrades++; // all trades this EA }
You check in one time not only if there are open buys and/or sells (nOpenOrders)
but also the total with pending include (EAtrades)
Only when nOpenOrders > 0 and when the total trades > nOpenOrders you have to delete the pending
In that case the check is done atonce
If all pending are deleted the next tick this loop for deleting pending will not be done again.....
I think it is faster...
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
all pending orders should be deleted if an active order is available ...
what I'm doing wrong?