Orders are not deleting?

 

Hey guys,

I have a problem with deleting all the pending orders when there are opened orders in the order pool.

I am checking if there are any opened orders with this function

int CheckActive()
{

      int Active = 0;

      for(int z = OrdersTotal()-1; z >0; z--)
      {

         OrderSelect(z, SELECT_BY_TICKET,MODE_TRADES);
   
         if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            {Active += 1;}   
   
      }

return(Active);

}
Print("activ :",CheckActive());

When I check the return value with the print function it works fine.

After that I try to delete all pending orders with this condition but it seems it does not work

 for(int e = 0; e<OrdersTotal(); e++)
        {
         OrderSelect(e, SELECT_BY_POS, MODE_TRADES);
           if(CheckActive() >= 1 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)     
           {
           if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)
              OrderDelete(OrderTicket());
               
           
           }
       }

After two days of searching for my error I decided to post it to the forum for help.

Thanks in advance.

 
rori4:

After that I try to delete all pending orders with this condition but it seems it does not work

After two days of searching for my error I decided to post it to the forum for help.

Your loop must count down . . . read this to find out why: Loops and Closing or Deleting Orders
 

I have also tried with counting it down with this function but it is also not working. I don't get any errors in the journals also.

        for(int e = OrdersTotal()-1; e >=0; e--)
        {
         OrderSelect(e, SELECT_BY_POS, MODE_TRADES);
           if(CheckActive() >= 1 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)     
           {
           if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)
              OrderDelete(OrderTicket());
               
           
           }
       }
 
rori4:

I have also tried with counting it down with this function but it is also not working. I don't get any errors in the journals also.

Your CheckActive() code is wrong . . .

OrderSelect(z, SELECT_BY_TICKET,MODE_TRADES);

z is an index not a ticket number . . . use SELECT_BY_POS

 
and
int CheckActive()
{
 int Active = 0;
 for(int z = OrdersTotal()-1; z >0; z--)
      {
       OrderSelect(z, SELECT_BY_TICKET,MODE_TRADES);      //wrong not ticket
                                                              //??????   what Symbol what magicnumber  ?????
       if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            {Active += 1;}   
   
      }

return(Active);

}




 
int Markettrades = CheckActive();
for(int e = 0; e<OrdersTotal(); e++)  //count down 
        {
         OrderSelect(e, SELECT_BY_POS, MODE_TRADES);
           //if(CheckActive() >= 1 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic) 
           if(Markettrades >= 1 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)  //no need to check several times same loop if there are markettrades open !!
           {
           if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)
              OrderDelete(OrderTicket());
               
           
           }
       }
if you count also your pendingtrades when you do CheckActive() then you can do also if (pending > 0){for(int e = 0; e<OrdersTotal(); e++)  //count down .....
 

Ok thanks for the help but now I am baffled. The code deletes only the buy pending order if there is any active order. if there is an active buy the sell pending is not deleting

int CheckActive()
{

      int Active = 0;

      for(int z = OrdersTotal()-1; z >0; z--)
      {

         OrderSelect(z, SELECT_BY_POS,MODE_TRADES); // Changed from SELECT_BY_TICKET  
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
         {
         if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            {Active += 1;}   
         }     
      }

return(Active);

}

  int Markettrades = CheckActive();  // Added
        for(int e = OrdersTotal()-1; e >=0; e--)
        {
         OrderSelect(e, SELECT_BY_POS, MODE_TRADES);
           if(Markettrades >= 1 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)     // Added the magic and Symbol
           {
           if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)
              OrderDelete(OrderTicket());
               
           
           }
       }

With the green have marked what I have changed and added

 
rori4:

Ok thanks for the help but now I am baffled. The code deletes only the buy pending order if there is any active order. if there is an active buy the sell pending is not deleting

With the green have marked what I have changed and added

CheckActive() is still wrong . . .

change this . . .

for(int z = OrdersTotal()-1; z >0; z--)

to this . . .

for(int z = OrdersTotal() - 1; z >= 0; z--)
 
Yes!!! That fixed it :) Thanks a lot ! Will have to learn a lot more about coding. Thanks again :)
Reason: