Why can I have negative MA shift in charts but not in code?

 

A negative MA shift value works fine in charts, but returns 0 in MQL4 code.

Why is that?


Please don't tell me that "we can't look into the future." That is not the point.

The point is a that negative MA shift value plots a certain position in the chart, and I want to obtain that same exact position/value with code.

How does that work exactly?


TIA

 

It makes it much easier for others to help you if you post some relevant code and how/when it is returning 0.

double  iMA(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // averaging method
   int          applied_price,    // applied price
   int          shift             // shift
   );

I think that it is unfortunate that they use the word shift twice as it can be confusing for some people.

Maybe it could be MA Offset instead of shift.

I think that your issue is related to you expecting a value when there is no value for a particular bar index.

Of course you can use a negative MA shift in code

double ma_value=iMA(_Symbol,PERIOD_CURRENT,21,-10,MODE_EMA,PRICE_CLOSE,1);

but the above will return 0.

Why?

Because with a -10 MA shift. the MA is shifted backwards. That means there is no MA at bar 1 so bar 1 will return 0. As will all bar indexes up to and including 9.

double ma_value=iMA(_Symbol,PERIOD_CURRENT,21,-10,MODE_EMA,PRICE_CLOSE,10);

This will return a value because bar 10 is the most recent bar where the MA has a value.

Reason: