iMaOnArray (Moving average of indicator)

Hi folks,

I've got a lot messages in the forex-tsd forum asking me how to use iMaOnArray function to get the Moving Average of a specific indicator. Today I'm going to answer all of you in this article.
 

What's the moving average of indicator?

When you attach an indicator to the chart it uses the price to for it calculation and save (draw) them . You can calculate the moving average of this array.

I'll give you a manual example before going to create our mql4 version.

Let's say that we will attach the CCI (Commodity Channel Index) indicator to our chart (Figure 1).

 

Now we want to get the moving average of the CCI. We can do that by dragging the Moving Average indicator to the window holds the CCI indicator and choose the Apply to:  Previous Indicator's Data (Figure 2).

 

And that's what you get (Figure 3)

 

How to do that programicly?

If you are interested in getting the moving average of any indicator like the above manual setup, you can 'trigger' your MetaEditor and write this code:

//+------------------------------------------------------------------+
//| iMAOnArray.mq4 |
//| Coders Guru |
//| http://www.metatrader.info |
//+------------------------------------------------------------------+

#property copyright "Coders Guru"
#property link "http://www.metatrader.info"


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 DarkBlue

double ExtMapBuffer1[];
double ExtMapBuffer2[];

int init()
{
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(1,ExtMapBuffer2);

return(0);
}

int deinit()
{
return(0);
}

int start()
{
int bar, limit;

int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-IndicatorCounted();


for(bar=0; bar<limit; bar++)
ExtMapBuffer1[bar] = iCCI(NULL,0,14,PRICE_TYPICAL,bar);

for(bar=0; bar<limit; bar++)
ExtMapBuffer2[bar]=iMAOnArray(ExtMapBuffer1,Bars,14,0,MODE_EMA,bar);

return(0);
}

 

As you can notice in the above code that we use the function iCCI and iMAOnArray to calculate the Moving average of the CCI indicator.

The iCCI function simply calculate the CCI of a giving bar and return its value. We store those values returned by iCCI function in our buffer ExtMapBuffer1.

Now we have a full of data buffer (ExtMapBuffer1), how to get the Moving Average of this buffer?

iMAOnArray:

With the aid of the iMAOnArray function we can calculate the moving average of the values stored in an array(buffer).

This is the syntax of the iMAOnArray function:

double iMAOnArray(double array [],int total,int period,int ma_shift,int ma_method,int shift)

The iMAOnArray - as you see in its syntax - returns double value; this is the value of the moving average - counted on the array - of the given bar. Don't worry you will understand well when you know the parameters of the iMAOnArray function.

Parameters:

double array []:

This is the array of the values you want to calculate the moving average of it.
You have to fill this array with double data type items.
In our example we used the bufferex1 as the array[] parameter passed to iMAOnArray after filling that array with the values of the CCIs of the bars in our chart.

int total:

You use the total parameter to indicate the count of items of the data from the array you want to use to calculate the moving average.
If you want to use all the items in the array in your moving average calculation pass 0 in the total parameter.

int period:

The moving average period of the moving average you want to use.
If you are familiar to moving average you can skip this section.
when use the moving average indicator with the hourly chart and use 12 as the period of moving average calculation ; it means that you want to know the average of the price of the previous 12 hours.
when use the moving average indicator with the daily chart and use 30 as the period of moving average calculation ; it means that you want to know the average of the price of the previous 30 days.

int ma_method:

The moving average method you want to use in your calculation.
These are the moving average methods available in MetaTrader