MQL4 Limit Trading Time to Days

This is a trading days filter, that allows users to turn on or off particular days of the week. In addition, it allows users to turn on or off the day of, and day prior to, Non-Farm Payrolls, just in case you do not want to trade on this volatile day.

Moreover, the user can turn off the Christmas holidays and/or New Years holidays. Learn in this MQL4 Limit Trading Time to Days article how to program your Expert Advisor to trade only on certain sessions and avoid volatility.

MQL4 Limit Trading Time to Days Parameters

Sun to Friday
Bool: Indicate whether or not you want to trade each specific day. The default is true, each day is traded.

NFP_Friday
Bool: Whether or not you want to trade on volatile Non-Farm payroll Friday, first Friday of the month. Default is that the day is traded.

NFP_ThursdayBefore
Bool: Whether or not you want to trade on the Thursday before the volatile Non-Farm Payroll Friday. Default is that the day is traded.

ChristmasHoldays
Bool: Whether or not you want to trade during a custom defined period in December, during the low liquidity of the Christmas Holidays.

XMAS_DayBeginBreak
Double: This is the day in December you will start to filter out, as the beginning of the Christmas Holiday. The default is 15, but it can be any day.

NewYearsHolidays
Bool: Whether or not you want to trade during a custom defined period in the beginng of January, the low liquidity of the New Years Holidays.

NewYears_DayEndBreak
Double: This is the day in January you stop the filter, the end of the New Years holiday period. The default is 3, but it can be any day.

MT4 Code Snippets

extern string _7 = “— Trading Days —“;
extern bool Sunday = true;
extern bool Monday = true;
extern bool Tuesday = true;
extern bool Wednesday = true;
extern bool Thursday = true;
extern bool Friday = true;
extern bool NFP_Friday = true;
extern bool NFP_ThursdayBefore = true;
extern bool ChristmasHolidays = true;
extern double XMAS_DayBeginBreak = 15;
extern bool NewYearsHolidays = true;
extern double NewYears_DayEndBreak = 3;

 

bool DaytoTrade(){
bool daytotrade = false;

if(DayOfWeek() == 0 && Sunday) daytotrade = true;
if(DayOfWeek() == 1 && Monday) daytotrade = true;
if(DayOfWeek() == 2 && Tuesday) daytotrade = true;
if(DayOfWeek() == 3 && Wednesday) daytotrade = true;
if(DayOfWeek() == 4 && Thursday) daytotrade = true;
if(DayOfWeek() == 5 && Friday) daytotrade = true;
if(DayOfWeek() == 5 && Day() < 8 && !NFP_Friday ) daytotrade = false;
if(DayOfWeek() == 4 && Day() < 8 && !NFP_ThursdayBefore ) daytotrade = false;
if(Month() == 12 && Day() > XMAS_DayBeginBreak && !ChristmasHolidays ) daytotrade = false;
if(Month() == 1 && Day() < NewYears_DayEndBreak && ! NewYearsHolidays ) daytotrade = false;

return(daytotrade);}

 

Explanation

We are naming our day filter function DaytoTrade().

We declare a daytotrade (lower case) bool variable, and initiate it as false.

Next, we have a line for each day of the week, indicating if the day of the week should be traded or not. DayofWeek() function returns the current day of the week of the last known server time:

0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday

The logic of each line is as follows. If DayofWeek() is (==)  Sunday (0), and the Sunday external bool is true (&& Sunday), then daytotrade bool is true. If Sunday external bool has been manually set to false, daytotrade reverts to false. The same on through all the days.

Next we filter for Non-Farm Payroll days, which occur the first Friday of every month.

Lastly, we filter for the Christmas and New Years Holidays.

MT4 Code Usage

if (OrdersTotalMagicOpen()==0 && OpenBuy==true
&& DaytoTrade() ) {
//Place order
}

All you need to do is slip the DaytoTrade() function in as a condition for placing order. It will then check to see if you have declared any days not to trade (these days will indicated as false), as well as holidays not to trade (the holiday period will be indicated as false), before placing an order.

We generally use DaytoTrade() function in conjunction with the TradeTime() discussed here.