In this video, we are going to calculate the day of the week, the date and the current time. This is a question from one of the premium course members who wanted to calculate the current weekday so let’s find out how to do that with mql5.
To get started please click on a little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple date calculation, click on continue, continue and finish. Now you can delete everything above the on tick function and let’s also remove the two comment lines here.
To get started we want to use the function time local, this returns the local time of a computer, in our case, it’s the time in the strategy test.
In my case, it’s the date from February 2019, not my local computer time from today and I would like to get hours and minutes so I use time to string to convert the local time that we have calculated here and I use the parameter time underscore minutes – all in capital letters – that will return the hours and the minutes in this format.
If you need the seconds you can also use time seconds that would output the hour, the minute and the second.
Now that we have the time I would also like to calculate the date and the year.
This is almost similar but this time we are using time underscore date as the second parameter but actually, we wanted to calculate the day of the week and to get that value we need to do some more things.
First, we use mql date time, if you mark that and press F1 you will learn that this type of date structure contains eight fields, it will return integer values and it also contains the day and the day of the week.
Afterwards, we use time to struct for the local time that we have calculated above and the date time structure that we have created here.
Time to struct will convert the value into a structure and now we can actually calculate the number for the day of the week by using date time structure dot day of week. So if you cut out this part and press the dot button you will see that you can now get each of these values.
For example, date of year would deliver the current number of days for the year while the day of week will return the number of the day in the week and that’s what we need.
But humans prefer text values instead of numbers so we want to create a string variable called weekday, so far it does not have any value so let’s add the values.
If the day of the week equals one the weekday would be Monday, value two stands for Tuesday, and so on.
Actually when we fast forward the backtest we will see that from Friday to Monday we don’t see any weekdays here, that’s because the markets are closed so actually we could leave out Saturday and Sunday. One special thing, depending on the country where you live, you will think that Monday is the first day of the week. In Germany, Sunday would be day seven but in this case, it has the value zero.
Now let’s create a chart output here by using the comment statement.
We will see the text the date is, followed by the year and the date and in a new line I want to see today is and the weekday and the third line will output the time is and the hours and minutes.