In this video we are going to create an Expert Advisor that is able to calculate the time in hours, minutes and seconds that has passed since we started the Expert Advisor. So let’s find out how to do that with MQL4. To get started please click on a little icon here or Press F4 on your keyboard. Now you should see the Meta Editor window and here you want to click on file, new file,  Expert Advisor from template, continue. I will call this file Simple Running Timer.


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. We start by creating a few variables. The first three are date time variables. The type date time will store the time that has passed since the 1 January 1970 and it is stored as number of seconds.


This is not very good to read for humans, so we are going to convert it. In the On Tick function. We first want to check if the start time equals zero. That would mean that the start time has no value and that is the case when we start the Expert Advisor for the very first time. And when it has no value, we will assign the return value for time local. That is the local time of the computer where the client terminal is running, to our start time. This will also work in the strategy tester where you can pick other time spans and to create a readable format we will use Time to string.


That function will get the start time as the first parameter and the second parameter is for the format. And we are going to use the time underscore date and the time underscore seconds value here. Let’s continue to calculate the current time. We will also use time local. This will be calculated each time the Ontick function is called. And please remember that will not be every second, because the On Tick function will only run when there is a price change. So please keep that in mind.


Now let’s calculate the past time in seconds. That is the current time and we subtract the start time here. And to convert that into a readable format we once again use Time to string for the current time. And we pass time underscore date and time underscore seconds as the second parameter. Now let’s create a chart output by using the comment statement to see what’s going on. And this output will contain the start time, the current time, the same values in seconds since the 1 January 1970 and the past time in seconds, in minutes and in hours. If this was too fast for you, or if you have no idea what all the code here does, you maybe want to watch one of the other videos in the basic video series, or maybe even the premium course on our website might be interesting for you.


That’s also where you can find a link to the store where you can download source codes like this one, in case you don’t want to type everything. For now please click on the compile button or Press F seven on your keyboard. You shouldn’t get any errors here and if that is the case you can click on a little button here or Press F four to go back to Meta-Trader. And in MetaTrader you want to click on View, Strategy tester or press Control and R. Please pick the new file simplerunningtimer Exfour Mark the option for the visual mode here and start your test. Here we are, the Expert advisor is running. It’s calculating the time that has passed. The current time is 19 minutes past zero and you can see that we have started at midnight and it says that 19 minutes are over. So our Expert advisor works as designed. And in this little video you have learned how to calculate the past time since you started an Expert Advisor, in case you want to do anything time related. And you have coded it yourself with a few lines of MQL4 code.