How does the talib compute MACD ? Why the value is different ?

Hi everyone !

I accidentally found that the value returned by the talib was not equal to the value computed by the MACD formula.

e.g.

# MACD line computed by formula, saved in: macd_formula

EMA12 = talib.EMA(prices, timeperiod = 12)
EMA26 = talib.EMA(prices, timeperiod = 26)
macd_formula = EMA12 - EMA26

# MACD line computed by talib, saved in: macd_talib

macd_talib, signal, hist = talib.MACD(prices,
fastperiod=12,
slowperiod=26,
signalperiod=9)

'macd_formula' not equals to 'macd_talib', and has a 5% average difference.

Why this happened ?

Thanks,
Rein

4 responses

Could be Ema vs Sma ?

Looking at TA-Lib source code MACD should be using EMA... How far does 'prices' history go? You may be hitting memory effects http://ta-lib.org/d_api/ta_setunstableperiod.html if talib.EMA uses different memory range than talib.MACD. Try extending the history further out (e.g. 52 days) and compare only last few datapoints instead of entire arrays. See also this thread.

As I remember, TA_Lib MACD is multi output function.
Except Closing prices it has 3 inputs:
Period for fast EMA- 12
Period for slow EMA- 26
Period for signal line- 9
and 3 outputs:
The MACD Line is the difference between fast EMA and slow EMA.
Signal line is 9 period EMA of the MACD Line.
MACD Histogram is the difference between MACD Line and Signal line.

Rein Y. wu
May be you picking wrong columns to compare?
Try to record all 3 outputs of TA_Lib MACD and compare them to simple MACD Line calculation.

I know its an old question, but I just discovered the same thing. I am using an array of 100 numbers for testing. I calculated emas by hand and with talib also (It is using SMA for the inital EMA). That part is correct. But, when I do ema12-ema26 by hand and with MACD, the function is completely wrong. I tried using SMAs, but the result is still wrong...

EDIT: Yes, I'm looking at the MACD Line output.