MQL5 TUTORIAL BASICS – 55 HOW TO CALCULATE THE DAY OF THE WEEK

video
play-sharp-fill

 

  • Introduction to Calculating Day of the Week, Date, and Time (00:01 – 00:20) Introduction to calculating the day of the week, the date, and the current time in MQL5, as requested by a course member.
  • Opening MetaEditor and Creating a New Expert Advisor File (00:20 – 00:43) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Date Calculation.”
  • Setting Up the Code Structure (00:43 – 00:50) Deleting unnecessary code above the ‘on tick’ function.
  • Using TimeLocal to Get Local Computer Time (00:50 – 01:03) Using the ‘TimeLocal’ function to return the local time of the computer, which in this case is the time in the strategy test.
  • Converting Local Time to String Format (01:03 – 01:34) Using ‘TimeToString’ to convert the local time to a string format, showing hours and minutes.
  • Calculating Date and Year (01:34 – 02:01) Using ‘TimeToString’ with ‘TIME_DATE’ to calculate the date and year.
  • Using MQLDateTime to Calculate Day of the Week (02:01 – 02:49) Using ‘MQLDateTime’ and ‘TimeToStruct’ to calculate the day of the week.
  • Converting Day of the Week Number to Text (02:49 – 03:49) Creating a string variable for the weekday and assigning text values based on the day of the week number.
  • Creating Chart Output for Date, Day, and Time (03:49 – 04:45) Using the ‘Comment’ function to output the date, day of the week, and time on the chart.
  • Compilation and Testing in MetaTrader (04:45 – 05:34) Compiling the code and testing the expert advisor in MetaTrader using the strategy tester.
  • Demonstration of the Expert Advisor in MetaTrader (05:34 – 06:05) Demonstrating the expert advisor in MetaTrader, showing the date, day of the week, and time.

 

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.
That’s about it.
Well, if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button.
You shouldn’t get any errors and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader, please click on view, strategy tester or press control and r, please pick the new file, simple date calculation dot ex5, mark the option for the visual mode here and start a test.
Here we are!
The expert advisor is running, we see a date, today is Thursday, the time is running, this is the 28th of February in 2019, so let’s check the calendar!
This is February 2019 and indeed the 28th was a Thursday so our little expert advisor is working as expected and in this little video you have learned how to calculate the date, the day of the week and the current time and you have coded it yourself with a few lines of mql5 code.