Why doesn't it delete pending orders after closing positions

 

Dear 

I have no idea why it does not delete pending orders after closing positions.

It close open position in perfect way.

Somehow, it does not delete pendings.

Any help?

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);
           }
        }
     }
  }
 
  1.       int Ticket=OrderTicket();
          double lots=OrderLots();
          string symbol=OrderSymbol();
          if(OrderSelect(Ticket,SELECT_BY_TICKET))

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

  2. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price.

  3. You are looping over position index. That is not a ticket. Select by position.

  4. All orders selected by position will have OrderCloseTime of zero, by definition.
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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()< 2)
              {
               // Close open orders
               if(OrderClose(Ticket,lots,OrderClosePrice(),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
              {
               // 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);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
William Roeder:
  1. You can not use any Trade Functions until you first select an order.

  2. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price.

  3. You are looping over position index. That is not a ticket. Select by position.

  4. All orders selected by position will have OrderCloseTime of zero, by definition.
William Roeder:
  1. You can not use any Trade Functions until you first select an order.

  2. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price.

  3. You are looping over position index. That is not a ticket. Select by position.

  4. All orders selected by position will have OrderCloseTime of zero, by definition.

I get that.

I will try them again.

Thank you for the help.

 
Raymond Codjia:

Hey. Raymond.

How are you doing?

Thank you for reply.

I will correct them.

:)

 
Forexalpha777:

Hey. Raymond.

How are you doing?

Thank you for reply.

I will correct them.

:)

Hey i am doing well, you are welcome

Reason: