Main characteristics of indicators

The indicator implements a certain calculation algorithm applied by bars to a given initial timeseries or several timeseries. All such timeseries are the terminal's own arrays (see the function ArrayIsSeries): the terminal allocates memory for them and adds new elements whenever new bars are formed. Naturally, among such arrays, arrays with symbol quotes on different timeframes play a fundamental role as they are filled in by the terminal. However, the launched indicators can significantly expand the set of timeseries available for analysis.

The indicator usually saves its operation results in dynamic arrays, which are registered as indicator buffers using a special function (SetIndexBuffer) and also become the terminal's own arrays. In addition to allocating memory for them, the terminal provides public access to these arrays as to new timeseries, on which other indicators can be calculated.

The entry point to the calculated part of the indicator is the OnCalculate function — an event handler of the same name. In Overview of event handling functions, we have already mentioned this function: its presence in the source code alone is enough for the MQL program to be perceived by the terminal as an indicator. The OnCalculate function will be described in detail in the next section. In particular, the main feature of OnCalculate is the presence of two different forms. The programmer should select the option at the very beginning of the indicator design, because this determines the purpose and possible use cases.

The OnCalculate function is not the only distinguishing feature of the indicator. In addition to it, a group of special preprocessor directives #property is intended exclusively for indicators — we will consider them step by step in several relevant sections of this chapter. Earlier we have already seen some General program properties, and such directives, of course, also apply to indicators.

As MetaTrader 5 users know, each indicator has a way to display its graphical constructions (timeseries): either in the main window which displays symbol prices or in a separate subwindow. Such a subwindow is created in the lower part of the window when a specific indicator (or group of indicators) is added to the chart if it is designed to work in a subwindow. For example, the standard Moving Average (MA) indicator is drawn on the price chart, while the Williams Percent Range (WPR) is drawn in a separate subwindow.

From the developer's point of view, this means that you should initially determine whether the indicator will be displayed in the main window or in a subwindow because these two modes cannot be combined. Moreover, this characteristic, as well as the number of indicator buffers, can be set only once using the #property directives (see Two types of indicators and Setting the number of buffers and graphic plots), and then it will not be possible to change them using MQL5 API function calls as such functions are simply not provided. Unlike these immutable attributes, most other indicator properties can be dynamically adjusted by special functions. Thus, as we study the technical aspects of indicator programming, we will be able to establish correspondences between the #property properties and MQL5 functions.

Also, indicators usually implement OnInit and OnDeinit handlers (see Reference events of indicators and Expert Advisors). OnInit is especially important for assigning arrays that will act as indicator buffers, i.e., to accumulate the results of intermediate and final calculations, visible to the user and available to other programs, such as Expert Advisors.

The indicator is one of the interactive MQL programs that can, if necessary, work with timer events (OnTimer) and chart changes (OnChartEvent) produced by the user or other programs. These technical features are optional for indicators and are based on the chart event queue. We will discuss them separately in the chapter on charts.