When creating a trading strategy a developer often faces the necessity to draw graphically in a security window a certain dependence calculated by a user programmer. For this purpose MQL4 offers the possibility of creating custom indicators. Custom Indicator is an application program coded in MQL4; it is basically intended for graphical displaying of preliminarily calculated dependences. The main principle underlying custom indicators is passing values of indicator arrays to a client terminal for drawing indicator lines via exchange buffers.
MQL4 standard implies the possibility of drawing up to eight indicator lines using one custom indicator. One indicator array and one buffer are brought into correspondence with each indicator line. Each buffer has its own index. The index of the first buffer is 0, of the second one - 1, and so on, the last one has the index 7.
Passing values of indicator arrays via a buffer to a client terminal. Calculations are conducted in a custom indicator; as a result numeric values are assigned to indicator array elements. On the bases of value arrays received from buffers a client terminal displays indicator lines.
Let's analyze a simple custom indicator that shows two lines - one line is build based on maximal bar prices, the second one uses minimal prices. Let's analyze in details the indicator parts. In any application program written in MQL4 you can indicate setup parameters that provide the correct program servicing by a client terminal. In this example the head program part see Program Structure contains several lines with directives property. The first directive indicates in what window the client terminal should draw the indicator lines:.
In MQL4 there are two variants of drawing indicator lines: Main window is the window containing a security chart.
In the analyzed example two indicator lines are drawn. One buffer is assigned to each buffer, so the total number of buffers is two. These figures are parts of constant names that are set in accordance with buffers. For each constant color can be set at the discretion of a user. The indicator is intended for drawing two indicator lines, so we need to declare two global one-dimension arrays, one for each line. Names of indicator arrays are up to user.
It is necessary to declare arrays on a global level, because array elements values must be preserved between calls of the special function start. The described custom indicator is built on the basis of two special functions -init and start. The function init contains the part of code used on the program only once see Special functions.
For the zero buffer 0 a client terminal should use the following drawing styles: Thus, according to the code of the special function init both indicator lines will be drawn in the main security window.
Indicator lines can be drawn by other styles as well see Styles of Indicator Lines. Values of indicator arrays elements are calculated in the special function start. To understand correctly the contents of start code pay attention to the order of indexing bars. The section Arrays describes in details the method of indexing arrays-timeseries. According to this method bar indexing starts from zero. The zero bar is a current yet unformed bar.
The nearest bar's index is 1. The next one's is 2 and so on. As new bars appear in a security window, indexes of already formed history bars are changed. The new current, just formed, rightmost bar gets the zero index, the one to the left of him that has just fully formed gets the index 1 and values of indexes of all history bars are also increased by one.
It was said earlier that indicator lines are constructed on the basis of numeric information contained in indicator arrays. An indicator array contains information about dots coordinates upon which an indicator line is drawn. And the Y coordinate of each dot is the value of an indicator array element , and X coordinate is the value of an indicator array element index. In the analyzed example the first indicator line is drawn using maximal values of bars.
Correspondence of coordinates of an indicator line to values of an indicator array. Index value of an indicator array is out by a client terminal into correspondence with a bar index - these index values are equal. It must be also taken into account that the process of constructing indicator lines goes on in real time mode under conditions when in a security window new bars appear from time to time.
And all history bars are shifted to the left. To have the indicator line drawn correctly each line dot above its bar it must also be shifted together with bars. So there is need technical need to re-index an indicator array.
For example, the zero bar in Fig. The bar opened at 6: To have the indicator line drawn correctly on this bar, the client terminal will change the index of the indicator array element corresponding to the bar opened at 6: In the table in Fig.
Together with that indexes of all array elements will be increased by the client terminal by one. An the index of the array element corresponding to the bar opened at 6: The indicator array will become larger by one element. The index of a new added element will be equal to 0, the value of this element will be a new value reflecting coordinate of the indicator line on a zero bar. This value is calculated in the special function start on each tick.
Calculations in the special function start should be conducted so that no extra actions were performed. Before the indicator is attached to a chart, it does not reflect any indicator lines because values of indicator arrays are not defined yet.
That's why at the first start of the special function start indicator array values must be calculated for all bars, on which the indicator line should be drawn. In the analyzed example these are all bars present on a chart the initial calculations can be conducted not for all available bars, but for some last part of history; it is described in further examples. Ar all further starts of the special function start there is no need to calculate values of indicator array for all bars again.
These values are already calculated and are contained in the indicator array. It is necessary to calculate the current value of the indicator line only on each new tick of the zero bar. For the implementation of the described technology there is a very useful standard function in MQL4 - IndicatorCounted. It means the indicator array does not contain any element with earlier predefined value, that is why the whole indicator array must be calculated from beginning to end.
The indicator array is calculated from the oldest bar to the zero one. Index of the oldest bar, starting from which calculations must be started, is calculated the following way:. Suppose at the moment of attaching the indicator there are bars in a chart window. This is the value of the predefined variable Bars. So, as a result we obtain that i index of the first uncounted bar the latest one, starting from which calculations should be conducted is equal to While i is within the range from the first uncounted bar to the current one 0 inclusively, values of indicator array elements are calculated for both indicator lines.
Note, missing values of indicator array elements are calculated during one the first start of the special function start. During calculations the client terminal remembers elements, for which values were calculated.
The last iteration in while is performed when i is equal to 0, i. When the loop is over, the special function start finishes its execution and control is passed to the client terminal. The client terminal in its turn will draw all in this case two indicator lines in accordance with the calculated values of array elements.
On the next tick start will be started by the client terminal again. Further actions will depend on the situation we will continue analyzing the example for bars. A new tick comes during the formation of the current zero bar the most common situation. The processed tick belongs to the current bar. The analyzed situation will be the same for both ticks. Let's trace the execution of start that was launched at the moment t 2.
During the execution of the function start the following line will be executed:. IndicatorCounted will return the value , i. As a result i index value will be equal to 0 It means in the next while loop the values of array elements with the zero index will be calculated.
In other words, the new position of an indicator line on the zero bar will be calculated. When the cycle is finished, start will stop executing and will pass control to the client terminal. The processed tick is the first tick of a new zero bar. In this case the fact of appearance of a new bar is important. Before control is passed to the special function start , client terminal will draw again all bars present in the security window and re-index all declared indicator arrays set in correspondence with buffers.
Besides, client terminal will remember that there are already bars, not in a chart window. That's why, though now the first bar with index 1 finished at the moment t 2 was calculated by the indicator, function IndicatorCounted will return value that was on the previous bar , i.
In the next line index i will be calculated, in this case for the first tick of a new bar it will be equal to 1 It means calculation of indicator array values in while loop at the appearance of a new bar will be performed both for the last bar and for the new zero bar. A little earlier during re-indexation of indicator arrays the client terminal increased sizes of these arrays. Values of array elements with zero indexes were not defined before the calculations in the loop.More...