Deleting multiple pending orders

To add comments, please log in or register
siladeh
63
siladeh  
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);
     }
  }
//+----------------------------------------------------------------+
Ahmad Mehdiyev
471
Ahmad Mehdiyev  
Remove break. This code will probably delete the first order and stop.
Keith Watford
Moderator
21781
Keith Watford  

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.

Keith Watford
Moderator
21781
Keith Watford  
You also have return(0) before the 2nd run of the loop
Simon Gniadkowski
17237
Simon Gniadkowski  
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
siladeh
63
siladeh  
pro_:
Remove break. This code will probably delete the first order and stop.

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

Thank you for your help
siladeh
63
siladeh  
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);
     }
  }
Keith Watford
Moderator
21781
Keith Watford  
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
  }
Ahmad Mehdiyev
471
Ahmad Mehdiyev  
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.
siladeh
63
siladeh  
GumRai:

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


Thank you. Its now working right
12
To add comments, please log in or register