# MACD Histogram values

When using the MACD indicator on the chart it shows 2 values -  a Histogram and a Signal line.

If you use the iMACD() function to calculaute the MACD values you have 2 options - MODE_MAIN and MODE_SIGNAL.

I have always assumed thet MODE_MAIN represents the Histogram values.

Now to my dismay I discover that this is a fallacy! It represents the MACD values - of how it is calculated I have no idea! (But I can find out if I am interested)

My trading decisions are based on the MACD Histogram values (among others) and now I find out that if I use MODE_MAIN I get incorrect values.

It seems there is a way to use iCustom() with "iMACD" as the indicator name to get these values - or I have to write code to calculate these values.

Which is the best way? or can it be done with iMACD also?

If code is required is the following code correct?

```//Define Inputs
input int  period  = 100;

// define the MACD parameters
int fast_ema_period = 12;
int slow_ema_period = 26;
int signal_period = 9;

// calculate the MACD values
double macd_main[], macd_signal[];
int macd_handle = iMACD(_Symbol, _Period, fast_ema_period, slow_ema_period, signal_period, PRICE_CLOSE);
CopyBuffer(macd_handle, 0, 0, period, macd_main);
CopyBuffer(macd_handle, 1, 0, period, macd_signal);
IndicatorRelease(macd_handle);

// calculate the MACD histogram values
double macd_histogram[];
for(int i = 0; i < period; i++)
{
macd_histogram[i] = macd_main[i] - macd_signal[i];
}

// output the MACD histogram values to the Experts tab
for(int i = 0; i < period; i++)
{
Print("MACD Histogram[", i, "]: ", macd_histogram[i]);
}
```

Ernest Klokow:

When using the MACD indicator on the chart it shows 2 values -  a Histogram and a Signal line.

If you use the iMACD() function to calculaute the MACD values you have 2 options - MODE_MAIN and MODE_SIGNAL.

I have always assumed thet MODE_MAIN represents the Histogram values.

Now to my dismay I discover that this is a fallacy! It represents the MACD values - of how it is calculated I have no idea! (But I can find out if I am interested)

My trading decisions are based on the MACD Histogram values (among others) and now I find out that if I use MODE_MAIN I get incorrect values.

It seems there is a way to use iCustom() with "iMACD" as the indicator name to get these values - or I have to write code to calculate these values.

Which is the best way? or can it be done with iMACD also?

If code is required is the following code correct?

simply:

```double signal=iMACD(_Symbol, PERIOD_CURRENT, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, index);
double main=iMACD(_Symbol, PERIOD_CURRENT, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, index);
double histogram=signal-main;```

Yashar Seyyedin #:

simply:

Thanks a lot Yashar!

Ernest Klokow #:

Thanks a lot Yashar!

You are welcome... Btw I coded MQ4 ... you should convert to MQ5 yourself.

And what if both signal and main line has a minus value?

I'd like to get only the histogram values but I think with that way we can have a mathematical error.

VikThor1029 #:

And what if both signal and main line has a minus value?

I'd like to get only the histogram values but I think with that way we can have a mathematical error.

So what? If main and signal are both negative, but main is above signal, then the histogram is positive (-3 - (-5) = +2).

William Roeder #:

So what? If main and signal are both negative, but main is above signal, then the histogram is positive (-3 - (-5) = +2).

btw MODE_MAIN is the histogram value in iMACD

VikThor1029 #: btw MODE_MAIN is the histogram value in iMACD

No, it is not. Main is the MACD value. Signal is a moving average of MACD. The histogram is the difference between them.

Is histogram an Array here? I'd like to be able to iterate back over the histogram values similar to how we use CopyBuffer, is there a way to do this? Thanks.

GJ #:
Is histogram an Array here? I'd like to be able to iterate back over the histogram values similar to how we use CopyBuffer, is there a way to do this? Thanks.

CopyBuffer is for MQL5. This thread is MQL4.