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

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.


The "ma_shift" is a graphical shift of "the line" displayed. This is only relevant to displaying the array values. Not much relevant to coding EAs.

The "shift" is a value of element, taken into calculation. By default, the value of the shift is zero (the zero bar (the last bar)). Any shifts in bars in MQL4 are from the last bar backwards.

Example:
You compare two SMA. One is 20 periods/0 shift, the other is 10 periods/4 shift. Every comparison between the SMAs will be done between the 20 period SMA on the last bar in the array and the 10 period SMA 4 periods back in the array.
In numbers...
Lets say the 20 SMA in the last bar is 1.1000.
Lets say the 10 SMA is as follows:
1.1050 on 0 bar (last bar)
1.1000 on 1 bar (previous bar)
1.0950 on 2 bar (two bars back)
1.0900 on 3 bar (three bars back)

Result:
Is 20SMA( shift0 ) > 10SMA( shift0 ) => NO
Is 20SMA( shift0 ) > 10SMA( shift3 ) => Yes

In summary. the MA_shift is a shift of the line forward/backward. The shift is a barvalue shift backwards (from the 0/last bar).

Meaning, a 4 shift represents the MA value 4 bars back. This option is available in coding only, for the purpose of algorithm construction. The ma_shift is irrelevant to EAs, because when the computer calculates MA crosses it uses the array values, not the line itself.

Best of luck!

Tags:

Mql4