Event handling in MQL5: Changing MA period on-the-fly

This short article is devoted to one of the new MQL5 features of MetaTrader 5 platform, developed by MetaQuotes Software Corp. Perhaps this article is slightly late (it should have been issued in September-October 2009 - then it would be timely), but there were no similar articles on this topic. Moreover, in that time there were no such possibilities for handling events in indicators.

Imagine that we have some simple price indicator applied to a chart (in this case it is Moving Average, i.e. MA), and we want to change its smoothing period. In the MT4 platform we had the following options:

In MetaEditor you can edit expert input parameter (extern), responsible for the MA period, and then compile the source file.

Without switching to MetaEditor, right in the terminal window you can open the indicator's Properties dialog box and there edit corresponding input parameter.

You can open Win32 API library, find functions of messages capture, then tweak the indicator code so that it responds to events from the keyboard.

As we know, the desire for minimum of efforts is the greatest engine of progress. Now, thanks to a new MT5 platform, that allows indicators events handling initiated by the user, we can skip the above possibilities and change indicator parameters with single press of a key. This article covers the technical implementation of this problem solution.

Task assignment and problems

The indicator source code, used in our experiments, ships with Client Terminal. The unchanged source code file (Custom Moving Average.mq5) is attached in the end of this article.

For now we will not analyze the source code and especially the changes compared to its MQL4 original. Yes, in some places it has changed significantly, and that is not always obvious. Corresponding explanations, related to the restructuring of the base part of the calculation, can be found on forum and in online help.

Nevertheless, the main part of indicator in MQL4 remained unchanged. At least 80% of all changes in code, intended to resolve our problem, have been made based on the idea of computational functions of indicator as the "black boxes".

An example of what we want to achieve looks like this. Suppose that we've applied this indicator to a chart, and at a given moment it displays an exponential MA (EMA) with zero shift and period of 10. Our goal is to increase the smoothing period of simple MA (SMA) by 3 (up to 13), and to shift it by 5 bars to the right. The assumed action sequence is as follows:

Pressing the TAB key a few times to change the displayed MA from exponential to simple (changing MA type).

Pressing the UP arrow key on the main part of the keyboard three times to increase the period of simple MA by 3.

Pressing the UP (8) arrow key on the numeric keypad 5 times to offset MA by 5 bars to the right.

MQL5: Added the possibility of event handling by custom indicators, similar to that by Expert Advisors.

So, now we have no problems with adding new event handlers in the indicator. But for this we still have to slightly modify its code.

First, in MQL5 the status of indicator's external parameters has changed: you can't modify them in code. The only way to change them is via Properties dialog box in Client Terminal. Generally, in urgent need to change them this restriction is easily bypassed: just copy the values of external parameters into new global variables of indicator, and all calculations are done as if these new variables are actually the indicator's external parameters. On the other hand, in this case goes away the feasibility of external parameters, whose values can only mislead users. Now these parameters are simply not needed.

Thus, there are no external (input) parameters in indicator. The variables playing the role of external parameters, now will be the Terminal Global Variables or TGV, for short. If you want to see the TGV, responsible for the former external parameters of indicator, you can simply press F3 key in terminal. I can't find another simple way to control the indicator parameters.

Second (and this is important), on any change in external parameters of indicator we have to recalculate all its values throughout the history, again and from scratch. In other words, we'll have to perform calculations, that are usually made only on very first start of indicator. Indicator's calculations optimization remains, but now it becomes more subtle.

Several code parts of the first version of modified indicator are below. Full code is attached in the end of this article.

The first three options - are the period, the offset and the type of MA, and the fourth - Updated - is responsible for the calculations optimization, when changing the MA parameters. Explanations are few lines below.

These are codes for "up arrow" and "down arrow" keys, similar arrows on the numeric keyboard (key "8" and "2"), as well as the TAB key. The same codes (with other names of VK_XXX constants) actually exist in the <MT5dir>\MQL5\Include\VirtualKeys.mqh file, but in this case I decided to leave it as it is.

I've made a little tweak in CalculateLWMA() function: in original version the weightsum variable was declared using the static modifier. Apparently, the only reason why developers did that was the need to pre-calculate it on the first call of this function. Furthermore in code this variable remains unchanged. Here's the original code of this function, in which the parts related to the calculation and weightsum use are marked with comments:

Previously, this variant worked quite fine, but when I ran the "indicator + advisor" tandem (this is mentioned in the end of this article), in this very type of MA came troubles. The main one was caused by the circumstances described above, i.e. weightsum was the static variable: this variable was constantly increasing since on each change of MA parameter on-the-fly it was necessary to recalculate it from scratch.

The easiest way to directly and immediately calculate the weightsum value (it is equal to the sum of integers from 1 to MA period - for this purpose there is a simple formula for the sum of arithmetic progression), and at the same time deny its status as a static, which I did. Now, instead of previous weightsum declaration using the static modifier, we declare it without him, just initialize with "correct" value and thus remove the initial loop of "variable accumulation".

int weightsum = MA_Period *( MA_Period + 1 ) / 2;

Now, all works correctly.

OnCalculate() function as a handler

I had to make a lot of changes in the OnCalculate() function, and therefore I quote here its code completely.

The main change relates to the perceived need for complete calculation of indicator "from scratch": it is obvious, that if a user's keyboard manipulation changed MA period from 13 to 14, all previous optimizations of its calculations are already useless, and we have to calculate MA again. This occurs when the Updated variable has 0 value (TGV has changed after pressing the hot-key, but the tick, that redraws the indicator, has not came yet).

However, in addition, previously we have to explicitly call the OnInit() function, because we needed to change the indicator short name, which will be displayed when a cursor hovers over the line. After initial MA calculation the Updated TGV is set to 1, which opens the way to the optimized indicator calculation - until you again are not willing to change some indicator's parameter on-the-fly.

The handler works as follows: you press the hot-key, its virtual code is defined, and then the changeTerminalGlobalVar() auxiliary function is started and is correctly modifying the desired TGV. After this the Updated flag is reset to zero, waiting for tick, which will launch OnCalculate() and redraw the indicator "from scratch".

Auxiliary function that "correctly" changes the TGV

And, finally, the code of changeTerminalGlobalVar() function, used in the OnChartEvent() handler:

The main purpose of this function - the correct calculation of new MA parameters with taking into account "physical limitations". Obviously, we can't make the MA period less than 1, the MA shift can be random, but the MA type is the number from 0 to 3, corresponding to a conditional member number in the ENUM_MA_METHOD enumeration.

Checking. It works, but "at C grade". What we can do?

OK, let's apply our indicator to the chart and start to sporadically press the hot-keys, that change the MA parameters. Yes, everything works correctly, but there is one unpleasant fact: the TGVs are changing immediately (you can check this by calling the TGV using F3 key), but MA are redrawing not always immediately, but only on the new tick arrival. If we have an American session with the active ticks flow, then we can hardly notice the delay. But if it happens at night, during the calm, we can wait the redrawing for a several minutes. What's up?

Well, as they say, what ye write, so shall ye got. Prior to build 245 in indicators there was only one "entry point" - the OnCalculate() function. Of course, I'm not talking about OnInit() and OnDeinit() functions, that provide indicator's initial calculations, initialization and completion. Now there are several entry points and they are connected with the new Timer and ChartEvent events.

However, the new handlers are doing only what they include and are not formally associated with the OnCalculate() handler. So, what can we do with our "alien" OnChartEvent() handler to make it work "right", i.e. it would allow to immediately redraw the MA?

In general, there are several ways to implement this requirement:

"Matryoshka" (OnCalculate() call inside of OnChartEvent()): insert the OnCalculate() function call into this handler, pre-filling all of its parameters. As the OnChartEvent() handler implies changing at least one MA parameter, then it will affect all of its history, i.e. we must recalculate again it "from scratch', without the optimization of calculations.

"Artificial tick" that transfers control to the beginning of OnCalculate() function, which modifies the graphical buffer. Apparently, there are no "legitimate" methods, as reflected in the MT5 documentation (although, perhaps I've searched not so thoroughly). If you are interested you can search for something like «API», «PostMessageA», etc. Therefore, we will not consider this variant here, because it doesn't guarantee that undocumented features someday will not change. I have no doubt that it can be realized.

"Matryoshka" works!

It turns out, that we have already done the most important thing. Below is a very simple code of function. You can simply insert its call directly in front of the return operator of OnChartEvent() handler.

After compiling indicator and applying it to the chart, we see that in general the code works quickly and independently of ticks arrival.

The disadvantage of this implementation is that exactly the Close prices are copied into the price[] array. If desired, the CopyClose() function can be replaced with what we want by setting the field "Apply to" on "Settings" tab of the indicator properties dialog box. If the current price will be basic (Open, High,
Low,
Close), then we already have the corresponding CopyXXXX() function. In the case of more complicated prices (Median, Typical or Weighted) we have to calculate the array in different way.

I'm not sure if we don't need the CopyClose() function, that copies the entire history of array. On the other hand, this function is fast enough when the history is not too deeply loaded. Checking the indicator on EURUSD H1 with history before 1999 (about 700 thousand bars) showed that the indicator deals with calculations and does not show any slowdown. On such history the possible slowdowns may be caused not by the CopyXXXX() function, but by the need of more complicated indicator recalculation from the beginning of history (this is obligatory).

Several findings and conclusion

What is better - one indicator file or "indicator + advisor" tandem?

In fact, this question is not so simple. On the one hand, it is good if we have one indicator file, because all functions, including event handlers, are concentrated in one place.

On the other hand, let's imagine that there are 3 or 4 indicators applied to chart along with an Expert Advisor - this situation is not uncommon. Furthermore, assume that each indicator is equipped with its own event handler, in addition to standard OnCalculate(). To avoid confusion with events processing in this "motley crue" it's more reasonable to concentrate all event handlers, now allowed in the indicators, in one place - in Expert Advisor.

For long time software developers have been deciding to give us the ability to process events in the indicator: from the non-public beta-release 09.09.09 (when the indicator is considered as "pure calculation & mathematical entity" and must not be contaminated by any features that hinder the speed of calculation) passed exactly 5 months. Likely, the "purity of idea" has to suffer - and now some real chaos of programmers fantasies will be unleashed. But the balance is always somewhere in the middle between pure, but limited idea, and not so clean, but more powerful capability.

In September-October of 2009, when the MT5 beta version build number has not even reached 200, I've wrote and debugged code of "Expert Adviser + Indicator" tandem, that allowed to manage MA parameters on-the-fly, but "at C grade": it was updated only after tick arrival, but not immediately. At that time this tandem was the only possible solution, and now unlikely it's interesting to anyone.

I could not then think of how to bring indicator functionality to "B grade", i.e. as it is presented in the latest version. Now I am pleased to provide more convenient solution to all whom it may be interesting.

Attached is my short video, demonstrating the work of our creation. Smooth change of the MA curve (only the period is changing - at first it increases, then decreases) is even dazzling in some way. This is Matryoshka (similarly to famous Russian set of nesting dolls).

Of course, such tricks are useful only when the calculation of indicator itself from scratch doesn't takes too much time. The simple MA, contained in this indicator, meet this requirement.

One slippery aspect

Remember, that the former external parameters of the indicator are now the terminal global variables (TGV), which you can see by pressing F3 key. Suppose, that you've opened the Global Variables dialog box and changed one of TGV's (for example, the MA period). You expect that this change is immediately reflected in the indicator on chart.

Currently in terminal there is no event, corresponding to TGV editing made by user (for example, CHARTEVENT_GLOBALVAR_ENDEDIT). Also, I think, we can't disable TGV modification in Global Variables dialog box. Therefore, here we can't reckon on any event except for the tick. What will happen in real?

If you don't touch the keyboard, then even on the next tick the update will be "wrong": the Updated variable wasn't set to zero, and therefore only the "optimized" calculation of indicator (corresponding to previous value of changed TGV) will be made. In this case, to restore justice, we can advise only one thing: after editing TGV you should at least once press the hot-key, that modifies the TGV, sets the Updated = 0 and causes full recalculation of indicator.

With new version of MQL programming language available not only the approach of dealing with indicators have changed, but there are also new ways of how to create indicators. Furthermore, you have additional flexibility working with indicator's buffers - now you can specify the desired direction of indexing and get exactly as many indicator's values as you want. This article explains the basic methods of calling indicators and retrieving data from the indicator's buffers.

Any new subject seems complicated and hard-to-learn for a newbie. Subjects that we know seem very simple and clear to us. But we simply do not remember, that everyone has to study something from scratch, and even our native language. The same is with the MQL5 programming language that offers wide possibilities of developing one's own trading strategies - you can start learning it from basic notions and simplest examples. Interaction of a technical indicator with the MetaTrader 5 client terminal is consider in this article on the example of the simple custom indicator SMA.

In order to obtain values of a built-in or custom indicator in an Expert Advisor, first its handle should be created using the corresponding function. Examples in the article show how to use this or that technical indicator while creating your own programs. The article describes indicators that are built n the MQL5 language. It is intended for those who don't have much experience in the development of trading strategies and offers simple and clear ways of working with indicators using the offered library of functions.

The idea of digital signal filtering has been widely discussed on forum topics about building trading systems. And it would be imprudent not to create a standard code of digital filters in MQL5. In this article the author describes the transformation of simple SMA indicator's code from his article "Custom Indicators in MQL5 for Newbies" into code of more complicated and universal digital filter. This article is a logical sequel to the previous article. It also tells how to replace text in code and how to correct programming errors.