iMA() has a bug with with price mode; e.g. MODE_OPEN actually uses the CLOSE

 

Hi, I have an indicator calling iMA() to return a moving average.

If I use MODE_CLOSE in the code, it uses the LOW prices.

If I use MODE_OPEN in the code, it uses the CLOSE prices.

I can tell this by setting the period to 1 and seeing what prices the moving average tracks.

Here is the line of code I am using.

// moving average

ma[i] = iMA(Symbol(),0,MA_Period,0,MA_Type,MODE_OPEN,i); // as of Oct 20, 2011, MODE_OPEN actually calculates on the CLOSE price. THIS IS ABUG.


I don't know if other indicators suffer the same problem, but I am now very wary of everything inside MT4. I am using build 402.

Please provide a comment in regard to this issue.

Thanks,

saltminer

 
What does MA_Type equal ?
 
saltminer:

Hi, I have an indicator calling iMA() to return a moving average. If I use MODE_CLOSE in the code, it uses the LOW prices. [...]

It's a bug in your code, not in MT4, albeit it's abetted by a very unwise development decision in MT4.

The documentation of the applied price parameter (https://docs.mql4.com/constants/prices) for the iMA() function (https://docs.mql4.com/indicators/iMA) says that you should use PRICE_OPEN, PRICE_CLOSE etc, not MODE_OPEN, MODE_CLOSE.

MODE_OPEN etc are used with a different set of functions; see https://docs.mql4.com/constants/series.

It wasn't wise for the MT4 developers to have two different enumerations, with overlapping purposes/meanings, and with different values. I know of a very widely-used indicator which incorrectly uses MODE_OPEN etc instead of PRICE_OPEN, leading to some unintended behaviour of the indicator which is believed to be a "feature" rather than a bug.

 
You could use the integer equivalents, 0 for close, 1 for open etc, makes for shorter code too.
 

Many thanks, yes indeed I should use PRICE_Close. Thanks jjc and all.

saltminer

Reason: