Download MetaTrader 5
To add comments, please log in or register
Sell your applications via the Market. It is secure!
DeanDeV
330
DeanDeV 2015.06.20 13:54 

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(OrderSelect(z,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderSymbol()==Symbol()
        && OrderMagicNumber()==MagicNumber)
          {
           if(OrderType()<OP_BUYLIMIT) 
            {
             DeletePending=false;
            }
             break;
          }
      }
    }
  
  for(int y=OrdersTotal()-1; y>=0; y--)
   {
    if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
     {
      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()); 
              }
            }
          }
     }
   }
whroeder1
13619
whroeder1 2015.06.20 20:45  
  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()<OP_BUYLIMIT) 
                {
                 DeletePending=false;
          bool DeletePending=false;
          for(int z=OrdersTotal()-1; z>=0; z--) if(
             OrderSelect(z,SELECT_BY_POS,MODE_TRADES)
          && OrderSymbol()==Symbol()
          && OrderMagicNumber()==MagicNumber
          && OrderType()<OP_BUYLIMIT) 
                     DeletePending=true;
    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--)
    

DeanDeV
330
DeanDeV 2015.06.21 00:24  
WHRoeder:
  1. When you find an open order, why are you setting DeletePending to false again?

Thanks for a reply. 

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? 

/
To add comments, please log in or register