MQL4 TUTORIAL – SIMPLE CUSTOM TIME FUNCTION

video
play-sharp-fill

In this video we are going to create a little custom function to calculate the local time. You see that we have an output for the current date that is traded here in the Strategy Tester and this is the current time and now we are going to create a custom function in MQL4 to calculate and output the time in this way.
To do that please click 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/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleCustomTimeFunction”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and let’s also delete the two comment lines here.
We start with a string that will be called: “myTime” it’s a string variable for the custom time that we are going to calculate and we are going to create a custom function that will be called: “GetTime” and to calculate “myTime” I just try to call the function: “GetTime” here. If I click on the “Compile” button now you will see that “GetTime” function is not defined so this is definitely nothing that comes with MQL 4 and now we are going to create it. It should return a string value, the name is: “GetTime” and as soon as I add two curly brackets and recompile the code the error is gone.
Now let’s add some logic to the “GetTime” function that we have created right now. I would like to output the time with seconds like we see it here: 17:55 means 5 minutes to 6:00 p.m. and to calculate the time we use a function that is called: “TimeLocal” and we want the output for the date; this is the first part here with the date (TIME_DATE), this is the pipe symbol and here is the second parameter for the time with seconds (TIME_SECONDS) – that’s the second part here – and we use time to string (TimeToStr) to convert the time from a datetime variable to a string.
Let’s mark “TimeToStr” press F1 and we see; okay, it converts the value of the time and seconds that have passed since January the 1st in 1970 into a string that has this format here and now that the calculation is done we want to return the calculated time to the main function that is done by using “return” for our string variable that we have calculated here, so everything within the “GetTime” function will be calculated here and the result is assigned to the variable: “myTime” so if we add the “Comment” function here for “myTime” it should create the desired chart output for the time we have calculated.
Now please click on the “Compile” button or press F7 that should work without any errors and if that is the case please click here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” or press CTRL + R that will bring up the Strategy Tester panel here and here you want to select the new file: “SimpleCustomTimeFunction”, please enable the visual mode here and start your test.
…and here we are.
Our Expert Advisor is working and now you know how to create a custom time function for Metatrader 4 and you have coded it yourself with a few lines of MQL4 code.

Download “MQL4 TUTORIAL - SIMPLE CUSTOM TIME FUNCTION”

SimpleCustomTimeFunction.txt – Downloaded 1908 times – 595.00 B