Back to Community
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.


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,

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

Why this happened ?


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 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.