In this video, we are going to calculate the current time and the current weekday. This was a question from one of our premium course members so let’s find out how get the current weekday with mql4.
To get started please click on the 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 command lines here.
We start by creating a string variable for the custom time and let’s do that again for the weekday.
To get the time we want to call a custom function called get time and to calculate the weekday we want to call a custom function called get weekday. Both functions do not exist so far so we have to code them now. We want to return a string value for the get time function so let’s create a string called time with seconds. And to calculate the value we use time local, this returns a local time of a computer, in our case, it’s not the time of the operating system but the time in the current backtest and we use time underscore date, pipe, time underscore seconds, if you mark that and press F1 you will see that time underscore seconds will give us time values in this format and we use time to string to convert it to a string value and afterwards, we use the return statement to return the calculated time to the main function.
Let’s create a second function called get weekday. This time we need to create a variable that can hold a number, we will call it weekday number and to calculate the current weekday number we use the function day of week, this one will return an integer value for each day of the week but to make it more readable we create another string variable called weekday and depending on the return value for day of week we will assign the value Monday if the return value is one, the day Tuesday if the return value is two, and so on.
Depending on the country where you live in, you might be wondering why Sunday is equal to zero.
In Germany, we would consider Sunday to be day seven but just the way it is, so keep that in mind.
Finally, we want to use the return statement to return the calculated weekday to the main function, so let’s add a comment output here in the main function that will output the time is followed by the calculated time and in a new line I want to see the text today is followed by the calculated weekday.
That’s about it.
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 basic videos in this video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button.
I didn’t get any errors and if this is true for you, you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader, we want to click on view, strategy tester or press control and r, please pick the new file, simple date calculation dot ex4, mark the option for the visual mode here and start a test.
Here we are!
The expert advisor is running, we have the output the time is followed by the date and below the line today is Thursday.
So let’s check that!
And indeed January the 18th in 2018 was a Thursday so our little expert advisor is working as expected and in this little video you have learned how to create an expert advisor that is able to calculate the current time, the date and the current day of the week and you have coded it yourself with a few lines of mql4 code.