|
|
|
|
|
|
|
|
Array is a data structure consisting of a group of elements that are accessed by indexing. It is often necessary to use arrays in Expert Advisors and indicators. For example if in an indicator it is required to use for calculations more than eight indicator buffers, arrays defined as price series will help. Or it is required for some reasons to insert the logic of an indicator in the Expert Advisor to calculate the values of the indicator without addressing to the iCustom() function.
What is the difference between a common array and a price series? This difference appears only because additional functions were introduced in MT4 that can calculate values of some technical indicators on arrays. An indicator buffer doesn’t require additional efforts from the programmer, when a new bar appears the size of the indicator buffer as well as the index of all values are autoincremented. The bar that had an index of 6, now has an index of 7. New data (the closing price Close, Volume etc.) are recorded in the new bar that has an index of zero. Indicators on such buffers are calculated from the last index to the zero one. Such behavior of an array is a price series.
Contrary to a price series a common array behaves conversely, but only in relation to the functions i…onArray():
- iMAOnArray()
- iMomentumOnArray()
- iStdDevOnArray()
- iEnvelopesOnArray()
- iBandsOnArray()
- iCCIOnArray()
- iRSIOnArray()
That’s why when you don’t need to use these functions you may not make a difference between an array and a price series. But if you want to calculate a moving average of an array you will feel the difference at once. This is the same as when you are waiting for the train at the station but numbering of coaches for some reason starts from the end of the train. To illustrate the difference the ArraySample.mq4 script is written. The algorithm is simple: an array of 100 elements is filled and a 4 period moving average of this array is calculated with the help of the iMAOnArray() function, at first for a common array, and then an effort is made to turn the array into a price series and make the output once more.
|
|
|
|
The sum+=Array[i] expression means the same that sum=sum+Array[i]. Once the script is executed we open the file ArraySample.csv and we see that the declaration of the array as a price series with the help of ArraySetAsSeries() hasn’t helped, the iMAOnArray() function returns the same values. Though the moving average of the array is calculated correctly. We will modify the script, after declaring the array as price series we will fill it over again with values and then will check it.
|
|
|
|
We will open the achieved file ArraySample2.csv in Excel. Now the outputs for the array and for the price series are different. It means that it is necessary to declare an array as a price series before the first call. Let’s make manual check of the price series. At first we find the sum of four cells.
|
|
|
|
Then we’ll find the moving average for these 4 cells:
|
|
|
|
Next we fill the rest of cells with this formula:
|
|
|
|
We can see that the calculation of the four period moving average of our array in the script coincides with the manual calculation in Excel.
|
|
|
|
The iMAOnArray() function is well suited to cases when it is required to smooth an array of values, suppose some indicator. For example, watching EURUSD H1, we notice that at the end of the American session the indicator ATR(8) starts declining (8 – the duration of the session in hours). In order to achieve smoother values of the indicator we smooth if by a 5 period moving average. For this we drag a standard indicator Moving Average on the window of the ATR indicator and set as starting values «Previous Indicator’s Data».
|
|
|
|
If we don’t want to implement these manipulations each time, we can write a custom indicator, in which the values of the ATR indicator will be smoothed.
For example the code of the indicator VolATR.mq4:
|
|
|
|
The usage of this indicator substitutes two standard indicators, applied one on another.:
|
|
|
|
Similarly we consider the iMomentumOnArray() function. It is needed when it is required to calculate the ratio of some elements of the array to other by some period.
|
|
|
|
The check file ArraySample3.xls with formulas is enclosed. An example of how this function is used is the indicator of the simplest calculation of the dollar index SumLogSample.mq4.
And the last function we will consider is iStdDevOnArray(). In MT4 this function characterizes the measure of data spread around the moving average of these data. This measure of spread in statistics is called standard deviation. I.e. at first a moving average of the array data is calculated (the period and type of the moving average are set by the 3-rd and 4-th parameters), and then the spread of data around this average is calculated. I’ll quote the Help on the standard deviation from MS Excel.
|
|
|
|
The script is written so that it would be easier to make manual check in Excel.
|
|
|
|
In what cases may we need this function? Suppose we need to choose one of two MTS, a criterion is needed. It may be the measure of spread of the MTS testing results, when an array is filled with profits and losses, and the average transaction and the standard deviation are found. It is logically that where the standard deviation in the array of transactions will be less the lesser volatility is expected.
Or another variant. After running a backtest, based on the results of the transactions an approximating line (red) is plotted according to the least squares method. And again we calculate the standard deviation from this line. It is easy to make these calculations in the block deinit() of the Expert Advisor.
|
|
|
|
The iEnvelopesOnArray() function calculates the moving average of the array (the average isn’t output) and adds (or subtracts) to this average some percent. We achieve the upper or lower percentage band. That’s why it is not difficult to write a script for this function (you may write it yourself for practicing).
The iBandsOnArray() function also calculates the average of the array and then adds (or subtracts from it) some number of standard deviations. And again we achieve the upper or lower Bollinger Band. As we have already made sure that calculations of the average and standard deviation of an array are made correctly in MT4, it doesn’t make any sense to check these two functions from the script. They are alike to some extent.
The iCCIOnArray() and iRSIOnArray() functions are more specific. They are rarer used in custom codes, that’s why I didn’t consider them. The main mistakes of using all these functions on arrays:
- one forgets to declare the type double for arrays
- one forgets to declare arrays as price series (indicator buffers don’t require this).
In all the given examples a simple smoothing is used for simplicity of manual check in Excel. You may choose the way of smoothing by yourself.
The codes of scripts, indicators and files Microsoft Excel you may download here .
Go to article «Work with arrays».
|
|