pending delete function!!!

 

hello friends!

i have a pending function that should close one pending order while the other order active.

instead its closing the 2 pending  immediately (one of them should be hit first , and after that the function need to close the other one.)

the code:

void ClosePendingOrder(string symbol )<<<<---the func
 {
int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)<<<<<--master loop
{
   OrderSelect(i, SELECT_BY_POS);
   int type_active   = OrderType();

if (type_active == OP_BUY || OP_SELL )<<<--if false no entry to second loop only if true do second loop!
{
 total = OrdersTotal();
  for( i=total-1;i>=0;i--)<<<<<<---second loop while op_buy || op_sell run the code close the pending
    {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
   
    
    switch(type)
    {
      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
   // if(result == false)
   // {
      //Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
     // Sleep(3000);
   // }  
  }
  }
}  
  return(0);
}

 thx!

 
er007:

hello friends!

i have a pending function that should close one pending order while the other order active.

instead its closing the 2 pending  immediately (one of them should be hit first , and after that the function need to close the other one.)

This will not work:

if (type_active == OP_BUY || OP_SELL )

 https://www.mql5.com/en/forum/141790

 

 While you are in the first loop using an index of  i  you start a second loop using index  i   what you think is going to happen with the first loop ?

 
RaptorUK:

This will not work:

 https://www.mql5.com/en/forum/141790

 

 While you are in the first loop using an index of  i  you start a second loop using index  i   what you think is going to happen with the first loop ?

2013.01.22 16:20:02 2012.06.19 18:40  10 point test GBPUSD,Daily: Alert: Order 225 failed to close. Error:4008

 does not cloing the pending.

the code:

 

 void ClosePendingOrder(string symbol )
 {
int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
{
   OrderSelect(i, SELECT_BY_POS);
   int type_active   = OrderType();

if ((type_active == OP_BUY) ||(type_active== OP_SELL ))<<<<<---change formation ----
{

 total = OrdersTotal();
  for(int j=total-1;j>=0;j--)<<<<<<<------changed to j -----
    {
    OrderSelect(j, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
   
    
    switch(type)
    {
      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );<<<<--add thise to---
     // Sleep(3000);
    }  
  }
  }
}  
  return(0);
}

 

thanks!
 
er007:
2013.01.22 16:20:02 2012.06.19 18:40  10 point test GBPUSD,Daily: Alert: Order 225 failed to close. Error:4008

 does not cloing the pending.

You need to read your code and work through it for each value of the loops . . .   you have some glaring issues which you need to be able to see for yourself. For example,  what if type is OP_SELL  what happens with your switch statement ?  and then what is the value of  result ?

Did you look up error 4008 ?  what does it mean ? 

Reason: