What do the shift parameters of iMA() function mean?

alex555 picture alex555 · Aug 19, 2013 · Viewed 20.2k times · Source

Can somebody explain me what is the difference between both shift parameters of the iMA function on an example?
According to the MQL4 documentation:

ma_shift - Moving Average shift. Indicators line offset relate to the chart by timeframe.

shift - Index of the value taken from the indicator buffer ( shift relative to the current bar the given amount of periods ago )

Which parameters are taken by the standard MA indicator?

Answer

Raiden picture Raiden · Oct 3, 2013
double iMA(string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)

For the packaged standard Indicator 'Moving Average', the 'Shift' field amends the 'ma_shift' parameter.

Indicator

For the packaged Custom Indicator 'Moving Averages', the 'MA_Shift' field amends the 'ma_shift' parameter.

Custom Indicator

Nothing in either indicator allows you to amend the last 'shift' parameter.

comparison


Graphically, for the standard Indicator 'Moving Average', changing the 'Shift' field shifts the MA line right (with a +ve number) and left (with a -ve number) by the number of periods as defined by the integer value.

ma_shift = 0: default

ma_shift = 4: ma_shift +4

ma_shift = -4: ma_shift -4

Code-wise, when polling iMA() and setting ma_shift to 4, e.g.

double iMA("EURUSD", PERIOD_H1, 8, 4, MODE_SMA, PRICE_CLOSE, 0)

you will get the moving average value 4 periods back.


This is a simple textual indicator showing the iMA() value, with the period, ma_shift, and shift parameters editable. Play with it and verify against the 'Moving Average' indicator(bring up Data Window):

#property indicator_chart_window

extern int period   = 8;
extern int ma_shift = 0;
extern int shift    = 0;

void start(){
   string A1=StringConcatenate("Stat: ", DoubleToStr(MA(),5));
   Comment(A1);
   return (0);
}

double MA(){
   return(iMA(NULL, 0, period, ma_shift, 0, 0, shift));
}

The last 'shift' parameter in the iMA() function shifts the periods used for calculation, and can only be a +ve number. A -ve number will request future non-existent periods. You can try putting a -ve number in the text indicator above to see what you get. (0.00000) As mentioned above, the indicators do not allow editing this parameter, simply because they are effectively the same.

double iMA("EURUSD", PERIOD_H1, 8, 4, MODE_SMA, PRICE_CLOSE, 0)

same as

double iMA("EURUSD", PERIOD_H1, 8, 0, MODE_SMA, PRICE_CLOSE, 4)

So why is it there? Most likely as a standardisation with other indicators, e.g. http://docs.mql4.com/indicators/iAlligator where the 'shift' parameter is an overarching determiner for which periods to calculate from, and the separate jaw_shift, teeth_shift, lips_shift being independent parameters to graphically shift the lines drawn.