Download MetaTrader 5

Closing all Pending Orders on new bar

To add comments, please log in or register
tparimore
112
tparimore  

Could someone tell me why the code below only closes one open trade per bar? e.g. I have two open orders. On each new bar ClosePending() is called. However, only one will close this bar and the second closes the next bar? Thanks in advance.

//---------------------------Close Pending Orders-------------------------------

void ClosePending()
  {
  
   bool   result;
   
   for(i=0;i<OrdersTotal();i++)
     {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUYSTOP)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_BUYSTOP Deleted");
          else Print( "Error Deleting OP_BUYSTOP ");
        }
          
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELLSTOP)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_SELLSTOP Deleted");
          else Print( "Error Deleting OP_SELLSTOP");
        }
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUYLIMIT)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_BUYLIMIT Deleted");
          else Print( "Error Deleting OP_BUYLIMIT ");
        }
          
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELLLIMIT)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_SELLLIMIT Deleted");
          else Print( "Error Deleting OP_SELLLIMIT ");
        }     
     }
   
   return(0);
  
  }
brewmanz
486
brewmanz  

because with i=0, you close first order with index [0].

second order changes from index[1] to index[0]

Then with next loop .. oops, no more orders.

Change

for(i=0;i<OrdersTotal();i++)

to

for(i=OrdersTotal()-1;i>=0;i--)
whroeder1
16105
whroeder1  
Count down and always test return codes.
 for(int pos=OrdersTotal()-1; pos >= 0; i--) if(
    OrderSelect(i,SELECT_BY_POS)
&&  OrderMagicNumber() == MagicNumber   // Only my orders,
&&  OrderSymbol()      == Symbol()      // on my chart,
&&  OrderType()        > OP_SELL){      // pending only.
    static OP.text[]={  "OP_BUY",       "OP_SELL",
                        "OP_BUYLIMIT",  "OP_SELLLIMIT",
                        "OP_BUYSTOP",   "OP_SELLSTOP"   };
    if (OrderDelete(OrderTicket()))
        Print(OP.text[OrderType()]," Deleted");
    else Print( "Error Deleting ",OP.text[OrderType()]," ",GetLastError());
}
tparimore
112
tparimore  
brewmanz:

because with i=0, you close first order with index [0].

second order changes from index[1] to index[0]

Then with next loop .. oops, no more orders.

Change

to


Duh... Thanks so much. This is why I ask the experts. Thanks again.
tparimore
112
tparimore  
WHRoeder:
Count down and always test return codes.

Thanks for your help. Can you please explain OrderType() > OP_SELL ? Also the use of static OP.text[] . Your code is very efficient.
Vasyl Nosal
19300
Vasyl Nosal  
tparimore:

Thanks for your help. Can you please explain OrderType() > OP_SELL ? Also the use of static OP.text[] . Your code is very efficient.

Many programmers writes like >OP_SELL to show they high "skill" of codding. :))))

It is for use less PC resources. But code becomes bad readed.

 

 

OP_BUY

0

Buy operation

OP_SELL

1

Sell operation

OP_BUYLIMIT

2

Buy limit pending order

OP_SELLLIMIT

3

Sell limit pending order

OP_BUYSTOP

4

Buy stop pending order

OP_SELLSTOP

5

Sell stop pending order

To add comments, please log in or register