bukosabino / ta

Technical Analysis Library using Pandas and Numpy
https://technical-analysis-library-in-python.readthedocs.io/en/latest/
MIT License
4.23k stars 864 forks source link

RSI has different results compared to Talib library #38

Closed asimsan closed 4 years ago

asimsan commented 5 years ago

The RSI calculated from Ta library is different from the one that you get from Trading View. I compared the results between Ta library and Talib library and , Talib gives you the same results as in tradingview.

MasterGeorge commented 5 years ago

Same here RSI is not working fine, there are huge diferences

ghost commented 5 years ago

Hi, reconciling ta RSI against simple excel compute and Bloomberg it is incorrect.

diegozr1 commented 5 years ago

How different?

Can you help providing more details on your excel computation?, remember data might differ from where you get it. Also please take a look at the implementation at momentum.py, check if something should be different.

    diff = close.diff()
    which_dn = diff < 0

    up, dn = diff, diff*0
    up[which_dn], dn[which_dn] = 0, -up[which_dn]

    emaup = ema(up, n, fillna)
    emadn = ema(dn, n, fillna)

    rsi = 100 * emaup / (emaup + emadn)
    if fillna:
        rsi = rsi.replace([np.inf, -np.inf], np.nan).fillna(50)
    return pd.Series(rsi, name='rsi')
ghost commented 5 years ago

The method used in below sheet rec's with Bloomberg and talib. The error is a country mile. Has not been implemented correctly, nor verified apparently.

https://stockcharts.com/school/lib/exe/fetch.php?media=chart_school:technical_indicators_and_overlays:relative_strength_in:cs-rsi.xls

fettunch commented 5 years ago

RSI is also not aligned with YAHOO RSI indicator but I think it's a matter on how to tune pandas ewm.

It all depends on how you specify the decay. Using com rather span and pass n-1 rather than n, it appears to be similar:

emaup = up.ewm(com=n-1, min_periods=0).mean()
emadn = dn.ewm(com=n-1, min_periods=0).mean() 

@bukosabino what is your thought on that? what is the golden source for this project to validate the indicators?

SebastianB12 commented 5 years ago

@fettunch is right. When I'm using:

emaup = up.ewm(com=n-1, min_periods=0).mean() emadn = dn.ewm(com=n-1, min_periods=0).mean()

the RSI values are absolutely identically to Bloomberg (I checked it on my Bloomberg on the S&P 500 Index for the last 20 days), which I would call the golden source, due to the fact, that most traders are using it. I would very much appreciate a change of the code in this way.

juliangall commented 5 years ago

I found this error before finding this thread on github. @fettunch solution doesn't work for me if I compare it with the calculation from first principles. It gets gradually more correct as time goes on but is a few percent adrift at the start of the series. I have altered the ema function in ta as follows, based on a Stackoverflow discussion about ema in pandas:

def ema_alt(series, periods, fillna=False):  
    if fillna:  
        sma = series.rolling(window=periods, min_periods=0).mean()[:periods]  
    else:  
        sma = series.rolling(window=periods, min_periods=periods).mean()[:periods]  
    rest = series[periods:]  
    return pd.concat([sma, rest]).ewm(alpha=1/periods, adjust=False).mean()

In summary, what this does is set the ema at period n to be the average of the first n periods, then uses ewm from that point onward.

N.B. I also had to set the first entry of up and dn to zero, as the diff procedure leaves them as nan, which upsets the averaging.

Edit I have now found this article that says there are three ways to calculate RSI that can often differ significantly.

fettunch commented 5 years ago

thanks @juliangall for the contribution. As you found there can be different way to compute RSI, that is why I am asking @bukosabino what is the golden source?

bukosabino commented 4 years ago

Hi all,

There is not a unique golden source.

The implementation proposed by @fettunch was included in the last version (0.4.7). You can use the new indicator updating the library installation:

pip install --upgrade ta

Thank you all of you.

traderblakeq commented 4 years ago

Is there any solution to this ? - Pandas_ta neither match same value as on Tradingview (TV)