I can close BUY and SELL open position but I can not delete pending orders.
What did I missed?
Why don't you just use this script? Works well for me.
-
for(int pos=OrdersTotal()-1; pos>=0; pos--) { int Ticket=OrderTicket();
You can not use any Trade Functions until you first select an order.
-
for(int pos=OrdersTotal()-1; pos>=0; pos--) { ⋮ if(OrderSelect(Ticket,SELECT_BY_TICKET)) { if(OrderCloseTime()==0)
If you select by position, the OrderCloseTime must be zero, or you wouldn't have it in the position list.
Here is the revised code (compiled but not tested):
void CloseAllOrders() { for( int pos = OrdersTotal() - 1; pos >= 0; pos-- ) { if( OrderSelect( pos, SELECT_BY_POS ) ) { int ticket = OrderTicket(), type = OrderType(); double lots = OrderLots(); string symbol = OrderSymbol(); switch( type ) { case OP_BUY: case OP_SELL: if( OrderClose( ticket, lots, OrderClosePrice(), 9999, Red ) ) PrintFormat( "Market Order #%d closed (Symbol: %s, Lots: %f)", ticket, symbol, lots ); else PrintFormat( "Error %d closing Market Order #%d (Symbol: %s, Lots: %f)", _LastError, ticket, symbol, lots ); break; default: if( OrderDelete( ticket, CLR_NONE ) ) PrintFormat( "Pending Order #%d deleted (Symbol: %s, Lots: %f)", ticket, symbol, lots ); else PrintFormat( "Error %d deleting Pending Order #%d (Symbol: %s, Lots: %f)", _LastError, ticket, symbol, lots ); }; }; }; };
Please note that the OrderSelect() is by position and not by ticket (as in your code), and that the order properties, such as "symbol" and "lots", are obtained AFTER the selection and not before as in your code.
Also, instead of Ask or Bid, just use OrderClosePrice(), as that will report the correct market price irrespective of it being a Buy or Sell order.
There is also no need to use the OrderCloseTime() as you are selecting from current trades (default value of "MODE_TRADES" in the OrderSelect()).
I am also using the "switch" statement to simplify code and do away with nested "if"s.
Why don't you just use this script? Works well for me.
Thank you for sharing :)
-
You can not use any Trade Functions until you first select an order.
-
If you select by position, the OrderCloseTime must be zero, or you wouldn't have it in the position list.
Thank you for the advice. I get it.
Here is the revised code (compiled but not tested):
Please note that the OrderSelect() is by position and not by ticket (as in your code), and that the order properties, such as "symbol" and "lots", are obtained AFTER the selection and not before as in your code.
Also, instead of Ask or Bid, just use OrderClosePrice(), as that will report the correct market price irrespective of it being a Buy or Sell order.
There is also no need to use the OrderCloseTime() as you are selecting from current trades (default value of "MODE_TRADES" in the OrderSelect()).
I am also using the "switch" statement to simplify code and do away with nested "if"s.
Wow. Appreciate for you revised version. I will try it out. Thank you for help again!
Here is the revised code (compiled but not tested):
Please note that the OrderSelect() is by position and not by ticket (as in your code), and that the order properties, such as "symbol" and "lots", are obtained AFTER the selection and not before as in your code.
Also, instead of Ask or Bid, just use OrderClosePrice(), as that will report the correct market price irrespective of it being a Buy or Sell order.
There is also no need to use the OrderCloseTime() as you are selecting from current trades (default value of "MODE_TRADES" in the OrderSelect()).
I am also using the "switch" statement to simplify code and do away with nested "if"s.
It is working!! :)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I can close BUY and SELL open position but I can not delete pending orders.
What did I missed?