I can not Delete pending orders

 

I can close BUY and SELL open position but I can not delete pending orders.

What did I missed?

void CloseAllOrders()
  {
   for(int pos=OrdersTotal()-1; pos>=0; pos--)
     {
      int Ticket=OrderTicket();
      double lots=OrderLots();
      string symbol=OrderSymbol();
      if(OrderSelect(Ticket,SELECT_BY_TICKET))
        {
         if(OrderCloseTime()==0)
           {
            if(OrderType()==OP_SELL)
              {
               double ask=SymbolInfoDouble(OrderSymbol(),SYMBOL_ASK);
               // Close open sell orders
               if(OrderClose(Ticket,lots,ask,9999,Red))
                  Print("Sell order closed. Ticket=",Ticket,", symbol=",symbol,", Lots=",lots);
               else
                  Print("Error in closing sell order: Error code=",GetLastError(),". Ticket=",Ticket,", symbol=",symbol,", Lots=",lots);
              }
            else
               if(OrderType()==OP_BUY)
                 {
                  double bid=SymbolInfoDouble(OrderSymbol(),SYMBOL_BID);
                  // Close open buy orders
                  if(OrderClose(Ticket,lots,bid,9999,Red))
                     Print("Buy order closed. Ticket=",Ticket,", symbol=",symbol,", Lots=",lots);
                  else
                     Print("Error in closing buy order: Error code=",GetLastError(),". Ticket=",Ticket,", symbol=",symbol,", Lots=",lots);
                 }
               else
                  //Delete pending orders
                  if(OrderDelete(Ticket,CLR_NONE))
                     Print("Pending order deleted. Ticket=",Ticket,", symbol=",symbol,", Lots=",lots);
                  else
                     Print("Error in deleting pending order: Error code=",GetLastError(),". Ticket=",Ticket,", symbol=",symbol,", Lots=",lots);
           }
 
Forexalpha777: I can close BUY and SELL open position but I can not delete pending orders. What did I missed?
You failed to mention what is output to the log file and what error code is reported.
 
Forexalpha777:

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. 

Files:
 
There are also several logic bugs in your code. I'm currently coding corrections for it as well as simplifications. I will post it shortly!
 
  1.    for(int pos=OrdersTotal()-1; pos>=0; pos--)
         {
          int Ticket=OrderTicket();

    You can not use any Trade Functions until you first select an order.


  2.    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.

 
rosspet6:

Why don't you just use this script? Works well for me. 

Thank you for sharing :)

 
William Roeder:
  1. You can not use any Trade Functions until you first select an order.


  2. 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.

 
Fernando Carreiro:

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!

 
Fernando Carreiro:

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!! :)

 
Forexalpha777: It is working!! :)
You are welcome!
Reason: