# Delete pending orders only if there are no open orders

330

Hey guys,

Can anyone help with a for loop to close all pending order if and only if there are currently no open orders?

Below is an attempt which I believe is pretty incorrect:

```bool DeletePending=false;
for(int z=OrdersTotal()-1; z>=0; z--)
{
{
if(OrderSymbol()==Symbol()
&& OrderMagicNumber()==MagicNumber)
{
{
DeletePending=false;
}
break;
}
}
}

for(int y=OrdersTotal()-1; y>=0; y--)
{
{
if(OrderSymbol()==Symbol()
&& OrderMagicNumber()==MagicNumber)
{
if(OrderType()>OP_SELLLIMIT)
{
DeletePending=true;
y=OrdersTotal();
}

if(DeletePending)
{
if(OrderType()>OP_SELL && OrderType()<=OP_SELLSTOP)
{
if(!OrderDelete(OrderTicket()))
Print("Deleting stop orders"," Ordertype:",OrderType());
}
}
}
}
}```
15576

1.  ```bool DeletePending=false;   for(int z=OrdersTotal()-1; z>=0; z--)    {     if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES))      {       if(OrderSymbol()==Symbol()         && OrderMagicNumber()==MagicNumber)           {            if(OrderType()=0; z--) if(          OrderSelect(z,SELECT_BY_POS,MODE_TRADES)       && OrderSymbol()==Symbol()       && OrderMagicNumber()==MagicNumber       && OrderType()
When you find an open order, why are you setting DeletePending to false again?
2.  ```          if(OrderType()>OP_SELLLIMIT)             {              DeletePending=true;              y=OrdersTotal();             }                                           if(DeletePending)             {              if(OrderType()>OP_SELL && OrderType()<=OP_SELLSTOP)               {                if(!OrderDelete(OrderTicket()))                 Print("Deleting stop orders"," Ordertype:",OrderType()); ``` ```if(OrderType()>OP_SELL)      if(!OrderDelete(OrderTicket()))       Print("Deleting stop orders"," Ordertype:",OrderType()); ```
What about a BuyLimit? You just found a pending, just delete it. Put your delete pending check outside the loop.
```if(DeletePending)
for(int y=OrdersTotal()-1; y>=0; y--)
```

330

WHRoeder:
1. When you find an open order, why are you setting DeletePending to false again?

I only want it to delete the pending orders if there are NO open orders, if there are open orders then the pending orders must not be deleted? Hence it must return false if there are open orders? If there are no open orders it must carry on through and delete the pending?