|
Indicator Styles in Examples
The MetaTrader 5 Client Terminal includes 38 technical indicators that can be used in MQL5 programs using appropriate functions. But the main advantage of the MQL5 language is the ability to create custom indicators, which can then be used in Expert Advisors or simply applied on price charts for the purpose of technical analysis.
The entire set of indicators can be derived from several base drawing styles, known as plotting. Plotting denotes a way of displaying data, which the indicator calculates, stores and provides on request. There are seven such basic plotting types:
Each plotting requires one to five arrays of the double type, in which indicator values are stored. For the purpose of convenience, these arrays are associated with the indicator buffers. The number of buffers in an indicator must be declared in advance using compiler directives, for example:
#property indicator_buffers 3 // Number of buffers
|
The number of buffers in the indicator is always greater than or equal to the number of plots in the indicator.
Since each basic plotting type can have color variation or construction specifics, the actual number of plotting types in the MQL5 is 18:
Plotting |
Description |
Value buffers |
Color buffers |
---|---|---|---|
Is not visually displayed in the chart, but the values of the corresponding buffer can be viewed in the Data Window |
1 |
- |
|
A line is plotted on the values of the corresponding buffer (empty values in the buffer are undesirable) |
1 |
- |
|
Is drawn as line segments between the values of the corresponding buffer (usually has a lot of empty values) |
1 |
- |
|
Is drawn as a histogram from the zero line to the values of the corresponding buffer (may have empty values) |
1 |
- |
|
Is drawn as a histogram based on two indicator buffers (may have empty values) |
2 |
- |
|
Is drawn as symbols (may have empty values) |
1 |
- |
|
Similar to the style DRAW_SECTION, but unlike it, can plot vertical segments on one bar |
2 |
- |
|
Color fill between two lines. 2 values of the corresponding buffers are shown in the Data Window |
2 |
- |
|
Is drawn as bars. 4 values of the corresponding buffers are shown in the Data Window |
4 |
- |
|
Drawn as Japanese candlesticks. 4 values of the corresponding buffers are shown in the Data Window |
4 |
- |
|
A line for which you can alternate colors on different bars or change its color at any time |
1 |
1 |
|
Similar to the style DRAW_SECTION, but the color of each section can be set individually; color can also be set dynamically |
1 |
1 |
|
Similar to the style DRAW_HISTOGRAM, but each strip may have a different color, you can set the color dynamically |
1 |
1 |
|
Similar to the style DRAW_HISTOGRAM2, but each strip may have a different color, you can set the color dynamically |
2 |
1 |
|
Similar to the style DRAW_ARROW, but each symbol can have its color. Color can be changed dynamically |
1 |
1 |
|
The DRAW_ZIGZAG style with the options of individual coloring of sections and dynamic color changing |
2 |
1 |
|
The DRAW_BARS style with the options of individual coloring of bars and dynamic color changing |
4 |
1 |
|
The DRAW_CANDLES style with the options of individual coloring of candlesticks and dynamic color changing |
4 |
1 |
The difference between an indicator buffer and an array
In each indicator, on its global level, you should declare one or more arrays of the double type, which then must be used as an indicator buffer using the SetIndexBuffer() function. To draw indicator plots, only the values of the indicator buffers are used, any other arrays cannot be used for this purpose. In addition, buffer values are displayed in the Data Window.
An indicator buffer should be dynamic and does not require specification of the size – the size of the array used as the indicator buffer is set by the terminal execution subsystem automatically.
After the array is bound to the indicator buffer, the indexing direction is set by default like in ordinary arrays, but you can use the ArraySetAsSeries() function to change the way of access to the array elements. By default, the indicator buffer is used to store data used for plotting (INDICATOR_DATA).
If the calculation of indicator values requires holding intermediate calculations and storing the additional values for each bar, then such an array can be declared as a calculation buffer during binding (INDICATOR_CALCULATIONS). For the intermediate values, you can also use a regular array, but in this case, the programmer has to manage the size of the array.
Some plots allow setting a color for each bar. To store the information about color, color buffers are used (INDICATOR_COLOR_INDEX). The color is an integer type color, but all indicator buffers must be of type double. Values of color and auxiliary (INDICATOR_CALCULATIONS) buffers cannot be obtained by using CopyBuffer().
The number of indicator buffers must be specified using the compiler directive #property indicator_buffers number_of_buffers:
#property indicator_buffers 3 // the indicator has 3 buffers |
The maximum allowed number of buffers in one indicator is 512.
Relevance of Indicator Buffers and Plotting
Each plotting is based on one or more indicator buffers. So, for displaying simple candlesticks, four values are required - Open, High, Low and Close prices. Accordingly, to display an indicator in the form of candlesticks, it is necessary to declare 4 indicator buffers and 4 arrays of the double type for them. For example:
//--- The indicator has four indicator buffers
|
Graphical plots automatically use indicator buffers in accordance with the plot number. Numbering of plots starts with 1, numbering of buffers starts with zero. If the first plotting requires 4 indicator buffers, then the first 4 indicator buffers will be used to draw it. These four buffers should be linked with the appropriate arrays with correct indexing using the SetIndexBuffer() function.
//--- Binding arrays with indicator buffers
|
The plotting candlesticks, the indicator will use just the first four buffers, because plotting of "candlesticks" was announced under the first number.
Change the example, and add plotting of a simple line - DRAW_LINE. Now suppose that the line is numbered 1, and the candlesticks are number 2. The number of buffers and the number of plots has increased.
//--- The indicator has 5 indicator buffers
|
The order of the plots has changed, and now the line comes first, followed by Japanese candlesticks. Therefore, the order of the buffers is appropriate - first we announce a buffer for the line with the zero index, and then four buffers for the candlesticks.
SetIndexBuffer(0,LineBuffer,INDICATOR_DATA); // The first buffer corresponds to index 0
|
The number of buffers and plots can be set only by using compiler directives, it is impossible to change these properties dynamically using functions.
Color Versions of Styles
As can be seen in the table, the styles are divided into two groups. The first group includes styles in whose name there is no word COLOR, we call these styles basic:
In the second group, the style names contain the word COLOR, let's call them color versions:
All color versions of styles differ from the basic ones in that they allow specifying a color for each part of the plotting. The minimal part of plotting is a bar, so we can say that the color versions allow setting the color on each bar.
Exceptions are styles DRAW_NONE and DRAW_FILLING, they do not have color versions.
To set the plotting color on each bar, an additional buffer for storing the color index has been added to the color version. These indices indicate the number of a color in a special array, which contains a predefined set of colors. The size of the array of colors is 64. This means that each color version of a style allows painting a plot in 64 different colors.
The set and the number of colors in the special array of colors can be set via a compiler directive #property indicator_color, where you can specify all the necessary colors separated by commas. For example, such an entry in an indicator:
//--- Define 8 colors for coloring candlesticks (they are stored in the special array)
|
It states that for plotting 1, 8 colors are set, which will be placed in a special array. Further in the program we will not specify the color of the plotting, but only its index. If we want to set red color for the bar number K, the color index value from an array should be set in the color buffer of the indicator. The red color is specified first in the directive, it corresponds to the index number 0.
//--- set the candlestick color clrRed
|
The set of colors is not given once and for all, it can be changed dynamically using PlotIndexSetInteger(). Example:
//--- Set the color for each index as the property PLOT_LINE_COLOR
|
Properties of the indicator and plotting
For indicator plots, properties can be set by means of compiler directives and using the appropriate functions. Read more information about this in Connection between Indicator Properties and Functions. Dynamic change of indicator properties using special functions allows creating more flexible custom indicators.
Start of Indicator Drawing on the Chart
In many cases, according to the conditions of the algorithm, it is impossible to start calculating the indicator values immediately with the current bar, since it is necessary to provide a minimum number of previous bars available in history. For example, many types of smoothing imply using an array of prices over the previous N bars, and on the basis of these values, the indicator value on the current bar is calculated.
In such cases, either there is no way to calculate the indicator values for the first N bars, or these values are not intended to be displayed on the chart and are only subsidiary for calculating further values. To avoid plotting of the indicator on the first N bars of the history, set the N value to the PLOT_DRAW_BEGIN property for the corresponding plot:
//--- Binding arrays with indicator buffers for the candlesticks
|
Here: