MQL5 TUTORIAL BASICS – 126 SIMPLE RUNNING TIMER

video
play-rounded-fill

 

In this video we are going to create an Expert Advisor that is able to calculate the time that has passed since we have started the Expert Advisor. This can be useful for several things. Let’s find out how to do that with MQL Five. To get started, please click on a little icon here or Press F Four 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 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 defining three datetime variables. Date time is the type of variable that we are going to use when we use something that contains a date or a time. And to get the time that the Expert Advisor is running, we want to check if the start time equals zero.

That will be true the very first time we start the Expert Advisor. And if this is the case, we want to set the start time to time local. Time Local is a function that will return the local time of the computer where the client terminal is running. So this is the initial value for our counter and all the other times the current time will be set to time local because time is moving. So this value will change every time a tick comes in.

Well the Ontick function will not run every second, it runs whenever the price changes, so keep that in mind. Now let’s calculate the past time. The past time will be the current time and we subtract the start time. Now let’s use MQL date Time. This is a date type structure and it makes it basically possible to get time based values in different formats.

For example, we can calculate the hour, the minutes and the seconds. And that’s exactly what we are going to do now. To do that, we use Time to Struct. The first parameter is the past time. The second parameter is the date time structure that we have created here.

To calculate the hour based value, we create an integer variable here and we assign Date Time structure dot hour for the minutes. We do the same, but this time we use date time structure dot min. And for the seconds we use Date Time structure dot SEC. If you would need something like days, this also would be possible because once you hit the dot you can pick one of these values here. The hour, the minute and the seconds will be enough for us. Now let’s create a chart output using the comment statement.

This will output the start time, the current time, and the past time in hours, minutes and seconds. And that’s about it. Well 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 from the basic video series or maybe even the premium course might be interesting for you. You can find that one on our website where you can also find a link to the store where you can download source codes like this one in case you don’t want to type everything. By the way, this once again was a suggestion from a premium course member. And if you are a premium member and if you have an idea for a video like this one, please let me know. For now, please click on the compile button here or Press F seven on your keyboard.

You shouldn’t get any errors here and if that is the case you can click the little button here or Press F four 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 runningtimer EX5. Mark the option for the visible mode here and start your test. Here we are, our expert advisor is running. It counts the hours, the minutes and the seconds. This might be useful if you want to do something that is time related. From now on you will find the first videos for this MQL five tutorial in 26 languages. We have created subtitles for YouTube. And in this little video you have learned how to create an expert advisor that is able to calculate hours, minutes and seconds and you have coded it yourself with a few lines of MQL5 code.