How To Set Expiration On A Trade To Less Than 10 Minutes

If you’ve created a robot forex trading system, and you set an expiration date on your order then you might be surprised to note that if your expiration is less than 10 minutes it gets set to 10 minutes – or you get error code 3 (or ERR_INVALID_TRADE_PARAMETERS), which means the parameters you’re sending through the OrderSend or OrderModify methods are not correct.

Why would you set an expiry date on an order?

The reason why you would want to set an expiry date on your pending order is that you only want your order to last for a specific period of time. Your system likely relies on the market displaying a certain type of behaviour and characteristic, but as price and time changes those characteristics no longer apply.

Therefore, when our system meets our predefined set of criteria, our next question is asked on what we should be doing:

Where should I get in?

In asking our first question we would be contemplating the following:

  • Does price need to confirm my direction? This would generally be seen by placing a stop entry order; or
  • Does price need to retrace before continuing in my direction? This is popularly seen as a limit entry order; or
  • Do I just get straight in? This is known as an at-market order.

The first two types of orders can allow us to ask an additional question when we put this order through to our broker, and that is:

How long should I hold this order?

With the MetaTrader OrderSend function we have the following parameters:

int  OrderSend(
   string   symbol, 
   int      cmd,    
   double   volume, 
   double   price,  
   int      slippage, 
   double   stoploss, 
   double   takeprofit,
   string   comment=NULL,
   int      magic=0,     
   datetime expiration=0, // set expiry date here, default is no expiry
   color    arrow_color=clrNONE 

To calculate the expiry date for an order you would need to know either when you’d like the order to be cancelled, this could be done in several ways with the two most popular being:

  • At the end of the current bar (or a number of bars); or
  • At a specific number of seconds in the future.

Minimum Expiry Time

Unfortunately both types of methods do run into the problem imposed by most forex brokers: expiration can only be set at a minimum of 10 minutes into the future.

Therefore, if your system uses the first method where you would like to close the order once the current bar has finished, and the current bar will finish in less than 10 minutes then you’ll need to manually handle your orders.

Same goes too for the second method that uses a specific number of seconds or minutes into the future – if it’s less than 10 minutes your order will either get an error, or your broker will impose a minimum of 10 minutes into the future when the order is placed.

How to manually handle expiration

To manually handle expiry we need a method that runs on each tick and simply checks if the current time is more than the number of seconds into the future we have set.

Before we jump into the code there is one other important aspect to consider as this could be a problem if we decide to manually handle the expiration of our orders:

Do you want to place a pending order prior to market close?

If you place an order prior to market close, and the difference in time between when the market closes and when we place the order is less than our manual expiration window then this means the order will be active on weekends.

If the market happens to gap at the open on Monday morning then you could get filled. Conditions could have changed over the weekend making your initial order no longer applicable, but because you’re handling it manually you’re stuck in a position you didn’t want.

Therefore, while the code below only focuses on handling the removal of pending orders, you would want to edit your entry functions to check you have plenty of time to close your orders.

Here is our function that manually handles expiration:


If you are placing stop or limit entry orders then I would highly encourage you to use the expiration parameter on the OrderSend or OrderModify methods.

To help get around any limitations your forex broker places on this expiration parameter you may want to manually check each order to determine if it is time for it to be removed, which the above code handles.

One other consideration you should also consider in the design of your robot forex trading system is whether the placement of your new order will be near the close of the market on Friday. You may want to look at adding a check on new orders such that you’re placing them with enough time to allow the above function to remove them if needed.

Recent Posts