Deleting multiple pending orders

 
I am trying to delete several buylimits orders when the Stochastics are in the overbought area but the code seems not to work. Where is the problem?
//------------------------------------------------------------------+
void CheckForDeleteBL()
  {
   double DValue;
   int Total,cmd;
   bool result;
   Total=OrdersTotal();
   DValue=iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   for(int i=0;i<Total;i++)
     {
      if(DValue>=80)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            cmd=OrderType();
            if(cmd==OP_BUYLIMIT)
              {
               OrderPrint();
               result=OrderDelete(OrderTicket());
               if(result!=TRUE) Print("LastError = ", GetLastError());
               break;
              }
           }
         else { Print( "Error when order select ", GetLastError()); break; }
        }
      return(0);
     }
  }
//+----------------------------------------------------------------+
 
Remove break. This code will probably delete the first order and stop.
 

Also

 for(int i=0;i<Total;i++)

should be

 for(int i=Total-1;i>=0;i--)

otherwise you may delete order 0, then move on to order 1, but because order 0 has been deleted, order 1 is now order 0.

 
You also have return(0) before the 2nd run of the loop
 
GumRai:

Also

should be

otherwise you may delete order 0, then move on to order 1, but because order 0 has been deleted, order 1 is now order 0.

Loops and Closing or Deleting Orders
 
pro_:
Remove break. This code will probably delete the first order and stop.

Thank you. Let me try that
 
GumRai:
You also have return(0) before the 2nd run of the loop

Thank you for your help
 
GumRai:
You also have return(0) before the 2nd run of the loop
Is this correct now?
void start()
  {
   double DValue;
   int Total,cmd;
   bool result;
   Total=OrdersTotal();
   DValue=iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   for(int i=Total-1;i>=0;i--)
     {
      if(DValue>=80)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            cmd=OrderType();
            if(cmd==OP_BUYLIMIT)
              {
               OrderPrint();
               result=OrderDelete(OrderTicket());
               if(result!=TRUE) Print("LastError = ", GetLastError());
              }
            return(0);
           }
         else 
           { 
            Print( "Error when order select ", GetLastError());  
            break; 
           }
        }
      return(0);
     }
  }
 
Can't be sure, but see comments in the code
void start()
  {
   double DValue;
   int Total,cmd;
   bool result;
   Total=OrdersTotal();
   DValue=iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   for(int i=Total-1;i>=0;i--)
     {
      if(DValue>=80)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            cmd=OrderType();
            if(cmd==OP_BUYLIMIT)
              {
               OrderPrint();
               result=OrderDelete(OrderTicket());
               if(result!=TRUE) Print("LastError = ", GetLastError());
              }
            //return(0);  Why this return? The function will end after selecting one trade
           }
         else 
           { 
            Print( "Error when order select ", GetLastError());  
            break; 
           }
        }
     }
  return(0);  //putting the return here will allow the loop to complete
  }
 
Ok, Gumrai fixed the return(0) problem you had. I would also remove the second break. Because now it will stop at the first error. If the goal is to remove ALL pending orders, you would probably want it to continue removing the other orders regardless of the error it got on one of them, and not simply stop, right? As for the rest - don't see anything contradicting the intended logic.
 
GumRai:

Can't be sure, but see comments in the code


Thank you. Its now working right
Reason: