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

 

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

 
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)
 
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
 
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.
 
hey thankz for the info ill try that
 
      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
 

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

 
thanks for the help greatly appritiated