How to Limit Forex Trading Actions to Specific Hours with MQL4

When you trade in the global foreign exchange market, you know that some hours are better than others. Some currency pairs are more active during specific time intervals, and sometimes, you even want to avoid trading during particular periods of the day. In this guide, you will see how to use MetaTrader 4 platform and MQL4 language to limit Forex trading (and not only trading) during specific hours.

FX中文社

Forex Trading Hours

As you may already know, Forex is a 24-hour market. The currency market stops only for a few hours during the weekend. The rest of the time it is open and active. With Forex being a global decentralized market, you can always find trading opportunities in any area of the world and at any time of the day. Having said that, you must also know that there are time periods when the market is more active than during the others. For example, the London open at 8 AM London time and the overlap of the European session and New York open after 8 AM New York time are the most active periods for EUR, USD, and GBP trading.

Having a market that is open for 24 hours a day allows many traders to participate, and each trader can find some opportunity. For example:

  • Think about a trader living in Sydney with a full-time job and working during business hours. Due to the difference in time zone, that trader can easily participate during the European session focusing on EUR, GPB, and CHF.
  • Think about another trader living in Miami, busy during business hour with their day job. This trader can choose to trade the Asian session.

The 24-hour market surely gives many advantages, but what if your trading is automated? That's even better! Your trading strategy is run by a personal computer (or a server) and can be executed 24 hours non-stop. Some strategies though work better only during some hours of the day, and in this case, you need to find the way to enable your activity only during those periods.

How to Limit Forex Trading Only to Specific Time Period?

MetaTrader 4 can run your Forex trading strategy with an expert advisor all day, every day. And with MQL4, you can decide to execute some of the activities only during specific time intervals.

Enabling the execution of tasks during only some hours is not too difficult, it just needs coding some comparisons. As in many other cases, MQL4 provides useful tools to achieve your goal; in this case, the tool is the function Hour(). The Hour() function returns the last know hour of the MetaTrader server time. If your platform is connected to the server, this is the current hour, with an integer value from 0 to 23.

In MetaTrader, you can see the server time in the Market Watch panel:

FX中文社

You can set up a script or an expert advisor with two input parameters:

  • Start Hour — a number from 0 to 23; it will be the start of trading operations.
  • Last Hour — a number from 0 to 23 to set the last hour of operations (inclusively).

If Start Hour and Last Hour are the same, then trading is allowed only during that hour. Start Hour can be before or after Last Hour — in this case, operations will be enabled overnight.

Example: Start Hour = 7, Last Hour = 19. Operations enabled between 7 AM and 7 PM.

Example: Start Hour = 20, Last Hour = 6. Operations enabled from 8 PM to 6 AM.

The code to perform the check is the following: