Delete pending orders only if there are no open orders

Back to topics list To post a new topic, please log in or register
avatar
120
DeanDeV 2015.06.20 15: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()); 
              }
            }
          }
     }
   }

 

avatar
13004
WHRoeder 2015.06.20 22: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--)
    

avatar
120
DeanDeV 2015.06.21 02: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? 

Back to topics list  

To add comments, please log in or register