How to calculate the current bar MACD exponential histogram value for multiple time frames?

 

I'm writing a tool that will display whether the current MACD EMA histogram is less than or greater than 0, for TF other than the current TF.

I know I need to download history for the other TF.

MACD MA is calculated correctly.

Given I have the history, why does this code not calculate the signal correctly?

And I've arbitrarily chosen 120 bars. Is there a better value?


extern int    FastEMA         = 12;                // Fast EMA Period
extern int    SlowEMA         = 26;                // Slow EMA Period
extern int    SignalEMA       = 9;                 // Signal SMA Period

#define MACD_BARS 120         // Number of bars to consider in MACD

double MacdBuffer[MACD_BARS];
double SignalBuffer[MACD_BARS];

for( int idx = MACD_BARS-1; idx >= 0; idx-- ) {
     MacdBuffer[idx] = iMA( _Symbol, PERIOD_D1, FastEMA, 0, MODE_EMA, PRICE_CLOSE, idx ) -
                       iMA( _Symbol, PERIOD_D1, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, idx );
}      
ExponentialMAOnBuffer( MACD_BARS, 0, 0, SignalEMA, MacdBuffer, SignalBuffer );
double MacdHist = MacdBuffer[0] - SignalBuffer[0];
 
ArraySetAsSeries(MacdBuffer, false);
ArraySetAsSeries(SignalBuffer, false);

Using "ExponentialMAOnBuffer" will need to reverse the order of the arrays.

If you prefer the MT4 method, use "iMAOnArray".

 
Nagisa Unada:

Using "ExponentialMAOnBuffer" will need to reverse the order of the arrays.


I've worked out that I must use a dynamic array so that I can set it to be a time series. Then this works:

   double MacdBuffer[];
   double SignalBuffer[];

   ArraySetAsSeries( MacdBuffer, true);
   ArraySetAsSeries( SignalBuffer, true);
   
   ArrayResize( MacdBuffer, MACD_BARS);
   ArrayResize( SignalBuffer, MACD_BARS);
   
   for( int idx = MACD_BARS-1; idx >= 0; idx-- ) {
        MacdBuffer[idx] = iMA( _Symbol, PERIOD_D1, FastEMA, 0, MODE_EMA, PRICE_CLOSE, idx ) -
                            iMA( _Symbol, PERIOD_D1, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, idx );
   }      
   ExponentialMAOnBuffer( MACD_BARS, 0, 0, SignalEMA, MacdBuffer, SignalBuffer );
   double MacdHist = MacdBuffer[0] - SignalBuffer[0];

And the order that I populate the MacdBuffer does not matter. 1st to last or last to first.

And the Signal becomes more accurate as you add more data points. MACD_BARS needs to be at least 9 to get anything at all. 40 to get some accuracy. 120 is quite accurate.

Lots of learning in this post. Thanks for your help.

 

If you include "MovingAverages.mqh", the Moving Average calculation requires ArraySetAsSeries to be "false", but the Moving Average of calculated values calculation could be "true".

I was mistaken. However, I am glad that my answer seemed to be helpful.

"iMAOnArray" instead of "ExponentialMAOnBuffer" produces the same result.

//ExponentialMAOnBuffer( MACD_BARS, 0, 0, SignalEMA, MacdBuffer, SignalBuffer );
  SignalBuffer[0] = iMAOnArray(MacdBuffer, 0, SignalEMA, 0, MODE_EMA, 0);
Reason: