Pending Order Expiration without ORDER_TIME_SPECIFIED

 

Hi there! 
I'm working on an EA that uses pending orders and I need them to expire x bars after they are created but my broker/symbol doesn´t support GTC or Time Specified Expiration.

this is the workaround I´ve created:

When conditions are met, it creates a pending order and stores the Expiration Time as a variable (using the number of bars that I input and converting it into seconds), like this:

if (condition)
{
trade.BuyStop(lots,PRC,_Symbol,STL,TKP);
Expiration= TimeCurrent()+(expirationbars*PeriodSeconds(_Period));
}

And then it deletes it when timeCurrent() is greater than Expiration, like this:

 if( IsPending && TimeCurrent()>=Expiration )
     { CancelPending(); }

the function I created for deleting pending orders is the following:

void CancelPending ()
 {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      ulong    ticket = OrderGetTicket(i);
      string   symbol = OrderGetString(ORDER_SYMBOL);
      ulong    magic  = OrderGetInteger(ORDER_MAGIC);
      
      if(symbol==_Symbol && magic==magicNum)
        {
         trade.OrderDelete(ticket);
        }
     }
 
 }

it works fine but the thing is that I realised that sometimes I have more then one pending order active and each one must expire at a different moment. and the way i did it it will always cancel all of them when expiration time is reached.

Can anyone help me out here? Do you have any ideas on how I can do this?
I need to create different pending orders with different expiration values (always 2 bars after they are created) without using  Time Specified Expiration.

Thanks!


 
 
fxsaber #:

thanks for your response!

interesting... I´m not sure if I get it though. can you expand on that?

are you suggesting that I write the ORDER_TIME_SETUP value on a comment when placing the Order,
then check if there are any orders with this particular comment to be deleted?

fxsaber
fxsaber
  • 2024.04.16
  • www.mql5.com
Trader's profile
 
Bruno Chimenti #:

are you suggesting that I write the ORDER_TIME_SETUP value on a comment when placing the Order?

This value is known for each order, see the documentation.

 
fxsaber #:
ORDER_TIME_SETUP

it worked. thanks a lot