# Deleting multiple pending orders

63

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)
{
{
cmd=OrderType();
{
OrderPrint();
result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
return(0);
}
}
//+----------------------------------------------------------------+```
471

Remove break. This code will probably delete the first order and stop.
Moderator
21781

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.

Moderator
21781

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

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
63

pro_:
Remove break. This code will probably delete the first order and stop.

Thank you. Let me try that
63

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

63

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)
{
{
cmd=OrderType();
{
OrderPrint();
result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());
}
return(0);
}
else
{
Print( "Error when order select ", GetLastError());
break;
}
}
return(0);
}
}```
Moderator
21781

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)
{
{
cmd=OrderType();
{
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
}```
471

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

GumRai:

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

Thank you. Its now working right