How to Cancel a Pending Order if it doesnt get triggered on the next following bar

To add comments, please log in or register
Praneeth
41
Praneeth  

hi guys im trying to create a stratergy where given a set of conditions becomes true in the current bar. i place a pending order.But if this pending order does not get triggered in the next following bar i want to cancel the trade.at the moment im struggling to come up with a solution for deleting the trade if it does not get triggered in the following bar. can any one help me out here

thankz

Simon Gniadkowski
17644
Simon Gniadkowski  
You need to select the order first, code example here: https://www.mql5.com/en/forum/135261 then determine if a time of Period() has elapsed since it's OrderOpenTime( ) if it has do an OrderDelete( int ticket, color Color=CLR_NONE)
Praneeth
41
Praneeth  
thanks for the reply but how would i know if the order was triggered or not.
OrderOpenTime( ) gives the time when i placed the pending order right. but i also need to know whether the order was triggered on the following bar. how can get this info .if the order was not triggered in the following bar i want to delete it
Simon Gniadkowski
17644
Simon Gniadkowski  
When a pending order is triggered it changes from an OP_BUYLIMIT or OP_BUYSTOP to an OP_BUY or OP_SELLLIMIT or OP_SELLSTOP to an OP_SELL so you check OrderType( ), if it's still a pending order it hasn't triggered.
Praneeth
41
Praneeth  
hey thankz for the info ill try that
Praneeth
41
Praneeth  
      OrderSelect(CurrentTicket, SELECT_BY_TICKET,MODE_TRADES);
      datetime OrderOptime=OrderOpenTime();
      datetime timeValuePerBar= Period() * 60;
      
      if(Time[0]>(OrderOptime+timeValuePerBar)&& (OrderType()==OP_BUYSTOP){
      
         ClosePendingOrder(Symbol(),CurrentTicket) // user defined function
      }
ok i have come up with this can RaptorUK or any one else kindly verify this code. im assuming that OrderOpenTime() and Time[0] returns the time in seconds
Simon Gniadkowski
17644
Simon Gniadkowski  

nirVaan:
ok i have come up with this can RaptorUK or any one else kindly verify this code. im assuming that OrderOpenTime() and Time[0] returns the time in seconds
Yes they do . . to be specific . . Date & Time functions "A group of functions providing the working with data of the datetime type (integer representing the amount of seconds elapsed from midnight, 1 January, 1970)."

Your code looks OK but doesn't do exactly what you said in your first post . . . ".But if this pending order does not get triggered in the next following bar" you code will potentially close an order during the following bar.

You don't need a user defined function, just do this . . .

OrderDelete(CurrentTicket);

Info: https://docs.mql4.com/trading/OrderDelete

Praneeth
41
Praneeth  
thanks for the help greatly appritiated
To add comments, please log in or register