$ £ ¥
¥ £ $

Optimizing MetaTrader (MT4 and MT5) Expert Advisor to Trade on Certain Days of Week

One of the most overlooked parameters of a Forex expert advisor that can be used in optimization of trading results is a set of weekdays for the EA to trade on. Different strategies are good under different market conditions. And market conditions tend to vary greatly depending on the current day of the week. Of course, sometimes Monday can show higher volatility than Wednesday or Thursday, but statistical trends are quite definite here. That's why it is recommend optimizing your expert advisor to trade only on certain days of the week. Especially, when it can be done easily in MetaTrader platform.

Even though it is probably a good idea to limit the maximum number of trading days to five — Monday, Tuesday, Wednesday, Thursday, Friday — we will show how to build and optimize an EA with even the early Sunday's and late Saturday's sessions included. They can occur on MetaTrader servers with time zones that are too far from UTC+2 and might be useful to some Forex traders. Besides, cryptocurrency trading goes on even on weekends, so 7-day optimization makes sense.

Let's presume that you already have a nice expert advisor that performs some position opening and closing. It is a good idea to limit only position opening on certain weekdays — closing should be performed even on the "restricted" days.

The tutorial below shows how to optimize your expert advisor for weekdays both in MT4 and in MT5.


Adding day of week conditions in MQL4/MQL5

To begin, you just need to open your EA's source code in MetaEditor.

The best way to start is to declare a variable (as an input parameter, of course) for each day of the week. It is logical to make them of the Boolean type (bool). This part is the same for MQL4 and MQL5:

input bool Sunday    = true;
input bool Monday    = true;
input bool Tuesday   = true;
input bool Wednesday = true;
input bool Thursday  = true;
input bool Friday    = true;
input bool Saturday  = true;

The default values are all true, which means that the EA will trade on every day of the week.

To be able to optimize based on the day of the week in MQL4, we need to enclose the entry condition or function inside the following check:

if (((TimeDayOfWeek(TimeCurrent()) == 0) && (Sunday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 1) && (Monday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 2) && (Tuesday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 3) && (Wednesday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 4) && (Thursday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 5) && (Friday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 6) && (Saturday)))
    {
        Entry();
    }

If we want to check the current day of the week in MQL5, we need to start by getting a time structure with the current time and date:

MqlDateTime dt;
TimeToStruct(TimeCurrent(), dt);

Now we can time our position entry function with the code similar to the one used in MQL4:

if (((dt.day_of_week == 0) && (Sunday)) ||
    ((dt.day_of_week == 1) && (Monday)) ||
    ((dt.day_of_week == 2) && (Tuesday)) ||
    ((dt.day_of_week == 3) && (Wednesday)) ||
    ((dt.day_of_week == 4) && (Thursday)) ||
    ((dt.day_of_week == 5) && (Friday)) ||
    ((dt.day_of_week == 6) && (Saturday)))
    {
        Entry();
    }

Setting up optimization in Strategy Tester

Now you can either test it manually or use the MetaTrader 5 Strategy Tester to backtest the EA and try all the possible 128 combinations of the days of the week. In the input settings of the Strategy Tester, tick all seven days — for all of them, the optimizer will automatically set Start to false and Stop to true. As you can see, this results in 128 combinations:

Inputs for Weekdays Optimization of Expert Advisor in MetaTrader 5 Strategy Tester

Don't forget that for such optimization you have to choose a rather long period (a year for example) because testing on just one week will lead to nothing good — in case of the day of the week optimization, a sample of 1 week becomes is the same as a sample size of 1. At least 10 weeks are recommended. Here are the results of an example optimization process of one of our test EAs (Heiken Ashi Naïve) on a period of 12 months:


Example weekday optimization results

Results for Weekdays Optimization of Expert Advisor in MetaTrader 5 Strategy Tester

The optimization results are sorted by balance. The first thing that you can notice here is that Sunday and Saturday have zero influence on results. This is because the platform uses UTC+2 time zone and there are no partial trading sessions on Saturdays and Sundays for the EUR/USD currency pair.

As you see, the best result was when the EA traded only on Mondays and Thursdays, making 288 trades in a year. Adding Wednesday to the trading days reduces the profit only slightly but it increases the number of trades, making the result more stable. Adding Thursday reduces the profit (a little) further but significantly increases the total number of trades. Other results aren't that interesting. To conclude, we can say that excluding Tuesday from the weekdays when the EA opens its positions is an optimal decision in this case.


Further steps

Now you can use this technique to improve your own expert advisor or to optimize your manual trading strategy (if you can implement convert it into a test EA of course).

If you are interested in the topic of EA optimization, you can also learn about optimization of expert advisors based on trading hours within a day.

If you have any comments or questions about weekday trading optimization for MetaTrader expert advisors, you can discuss this topic on our Forex forum.

If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.