HOW TO DISPLAY THE LOCAL TIME ON YOUR CHARTS—PART II:
As previously mentioned, expert advisers are responsible for executing tasks, such as managing trades. However, the only thing that the EA which was just created in the previous post did was display the local time; and delivering or displaying information is the job of a (custom) indicator—
not the job of an expert advisor. Accordingly,
this post will explain how to display the local time using a custom
indicator.
So then, why even bother with the previous exercise?
Recall that it was to illustrate how you can make an EA run, even when no ticks are coming in, by using the OnTimer() function to call the OnTick() function containing the Comment() function line.
Another reason that it is better to use a custom indicator for this kind of task is that only one EA can be put on a chart at a time. So, if an EA is used just to display information, there is not going to be any way to have the program execute and/or manage trades.
It therefore makes more sense to code the local time as a custom indicator rather than an expert advisor. Do this by using the following steps...
STEP ONE: As before, click on the MetaQuotes Language Editor Icon, and when the MetaEditor opens up, click on File in the upper left-hand menu and select New Project from the drop-down menu. However, this time, when the MQL Wizard: Project window appears, select the Custom Indicator option rather than Expert Advisor (template). Then click on the Next > button.
STEP TWO: Once again, fill in the Name (but do not delete Indicators\), Author and Link, and then click the Next > button.
STEP THREE: In the window that follows, select the OnTimer custom indicator event handler, and then click the Next > Button once again.
STEP FOUR: My guess is that the "Indicator in separate window" option is for creating lower panel indicators, which you will not be doing. So, skip this widow by simply clicking on the Finish button and move on.
STEP FIVE: Click on the Local Time.mq4 tab if necessary, and then go to the initialization function and insert the timer line, which is…
EventSetTimer(1);
…directly under the words: "indicator buffers mapping."
STEP SIX: Note that the next section is for the Custom indicator iteration function. In other words, this time the template did not automatically provide an OnDeinit() function for the EvenKillTime() function to terminate the relevant/related activity at the proper time. Consequently, you will need to copy and paste the corresponding text from the previous expert advisor. Here it is again…
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
//
}
STEP SEVEN: Note that in actuality, the OnCalculate() function will not be used in this indicator. Nonetheless, the Custom indicator iteration function text must remain given that an indicator will not compile without it, because without it, the program will recognize that this is an indicator. So, just skip this section and move on to the Timer function.
STEP EIGHT: Before, when you created the EA, the OnTimer() function had to call the expert advisor function so that it could tell it to fire off every second, which was then told in turn to display what it was doing in the chart's comment section. But here, you are simply going to tell the timer function to display this information directly itself by typing Comment (TimeLocal()); between its braces...
STEP NINE: Click on the Compile button. If you get an error, double click the error to make in jump the cursor to where the problem is.