- Introduction to Creating a Time Calculation Expert Advisor (00:00 – 00:11)
- Introduction to the concept of creating an expert advisor in MQL5 that calculates the time elapsed since its start.
- Overview of the practical applications of such a timer.
- Starting with MetaEditor (00:11 – 00:27)
- Instructions on how to open the MetaEditor from MetaTrader.
- Steps to create a new expert advisor file named ‘Simple Running Timer.’
- Setting Up the Expert Advisor (00:27 – 00:43)
- Removing unnecessary lines and preparing the code environment.
- Explanation of the initial setup for the expert advisor.
- Defining Date Time Variables (00:43 – 01:00)
- Introduction to using date time variables in MQL5.
- Setting up variables to track the start time and current time.
- Initializing the Start Time (01:00 – 01:15)
- Checking if the expert advisor is running for the first time.
- Assigning the start time using the ‘TimeLocal’ function.
- Updating Current Time and Calculating Elapsed Time (01:15 – 01:30)
- Continuously updating the current time with each tick.
- Calculating the time elapsed since the expert advisor started.
- Using MQL DateTime Structure (01:30 – 02:05)
- Introduction to the MQL date time structure for time-based values.
- Calculating hours, minutes, and seconds using the structure.
- Extracting Time Components (02:05 – 02:20)
- Creating variables for hours, minutes, and seconds.
- Extracting these components from the date time structure.
- Displaying Time on the Chart (02:20 – 02:52)
- Using the ‘Comment’ function to display the start time, current time, and elapsed time on the chart.
- Explanation of the output format.
- Compilation and Testing in MetaTrader (02:52 – 03:24)
- Instructions on compiling the expert advisor and testing it in MetaTrader.
- Visual demonstration of the timer in action.
- Conclusion and Additional Resources (03:24 – 05:09)
- Recap of the tutorial and its purpose.
- Invitation to explore more videos and the premium course for further learning.
- Mention of the availability of source codes and subtitles in multiple languages.
In this video, we will be creating an Expert Advisor that calculates the elapsed time since the start of the Expert Advisor in hours, minutes, and seconds, and displays it on the chart.
To begin, please click on the small button at the top in MetaTrader 5 or press the F4 key to open the MetaEditor.
Code Description:
At the start of the code, we declare several date and time variables:
CurrentTime
: This variable stores the current time.StartTime
: This variable stores the start time when the Expert Advisor was initiated.PassedTime
: This variable will store the elapsed time.
Within the OnTick
function, which gets called on every market tick:
- We check if the
StartTime
variable has not been assigned a value. If it hasn’t:- We assign it the current local time using
TimeLocal()
. TheTimeLocal()
function returns the current time of the computer where the client terminal is running.
- We assign it the current local time using
- We then calculate the current time and the elapsed time by subtracting the start time from the current time.
- A structure variable of type
MqlDateTime
namedDateTimeStructure
is declared. This structure stores information about date and time. - We use the
TimeToStruct
function to convert the elapsed time (PassedTime
) into theMqlDateTime
structure. This structure will allow us to extract hours, minutes, and seconds.
From this structure, we extract:
PassedTimeInHours
: The hours since the Expert Advisor started.PassedTimeInMinutes
: The minutes since the start.PassedTimeInSeconds
: The seconds since the start.
Finally, we use the Comment
function to display various time-related information on the chart:
- The start time.
- The current time.
- The elapsed time in hours, minutes, and seconds.
Code Execution:
Upon running the Expert Advisor, it will display the elapsed time since the start of the Expert Advisor on the chart in real-time. You’ll observe the seconds, minutes, and potentially hours ticking up.
In conclusion, in this video, you’ve learned how to calculate the elapsed time since the start of an Expert Advisor using just a few lines of MQL5 code.
Thank you for watching, and see you in the next video!