MACD and Signal values differ to those shown in indicator

To add comments, please log in or register
brettosm8
241
brettosm8  

Hi there,

This is actually a sub question related to https://www.mql5.com/en/forum/144912.

Anyway, the big picture is that I'm trying to check if the previous macd < previous signal and if current macd > current signal.  To do that I need to make sure I'm calculating the MACD and signal values correctly and am comparing my results with the MACD indicator that comes with MT4 except I've changed the style from histogram to line.  The signal mode in the indicator is SMA although the MACD is calculated using EMAs.  My signal values differ from the indicator's signal values as shown below.

 

In the chart the red vertical line is on bar 1 i.e. the current finished bar.  The Data Window shows the MACD value of the indicator is -0.000582 and in the Journal tab below where the highlighted row has printed the CurrentMACD I have -0.00058180, which matches but for rounding.  However, the signal values differ. The Data Window has -0.000219 which is in the row under the highlighted row as PreviousSignal.  Not easy for me to get my head around how THAT happened.

The function I have for this is:

 

void CalcMACDAndSignalValuesV2(){
   // populate arrays for the EMA values to to calculate the MACD Line and Signal Line      
   PreviousMACD = iMA(NULL, TIMEFRAME, FAST_EMA, 0, MODE_EMA, PRICE_CLOSE, 2) - 
                  iMA(NULL, TIMEFRAME, SLOW_EMA, 0, MODE_EMA, PRICE_CLOSE, 2);
   CurrentMACD = iMA(NULL, TIMEFRAME, FAST_EMA, 0, MODE_EMA, PRICE_CLOSE, 1) - 
                 iMA(NULL, TIMEFRAME, SLOW_EMA, 0, MODE_EMA, PRICE_CLOSE, 1);
   
   for(int i = 0; i < 10; i++)
      {
      MACDArray[i] = iMA(NULL, TIMEFRAME, FAST_EMA, 0, MODE_EMA, PRICE_CLOSE, i+1) - 
                     iMA(NULL, TIMEFRAME, SLOW_EMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
      Print("MACDArray[" + i + "]:" + MACDArray[i]);
      }
      
   PreviousSignal = iMAOnArray(MACDArray, 0, SIGNAL, 0, MODE_SMA,1);
   CurrentSignal = iMAOnArray(MACDArray, 0, SIGNAL, 0, MODE_SMA,0);
      
   Print("PreviousMACD: " + PreviousMACD + ", PreviousSignal: " + PreviousSignal);
   Print("CurrentMACD: " + CurrentMACD + ", CurrentSignal: " + CurrentSignal);
   Print("CalcMACDAndSignalValuesV@ method ran");
   }

So I'm filling up the MACDArray with 10 indexes of MACD values.  The first index i.e. 0 contains the MACD for the bar at position 1, so for the last parameter in the iMA function I've set the shift to i+1.  Then, in the CurrentSignal and PreviousSignal variables, the last parameter of the iMAOnArray function points to the first index of MACDArray.  To me this should all make sense but it obviously doesn't so any assistance would be appreciated.

Thanks!

Btw, I've declared the MACDArray in the global area as:

double MACDArray[10];
whroeder1
17946
whroeder1  
brettosm8
241
brettosm8  

Spot on!

Thanks!!! 

To add comments, please log in or register