vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

Value of RSI Indicator does not match with Tradingview or PandasTA

Open a3rwow opened this issue 2 years ago • 4 comments

I wanted to backtest using RSI, but the values seem to be different as compared to PandasTA or tradingview. Values from tradingview match the PandasTA

import vectorbt as vbt import yfinance as yf nifty = yf.download(tickers="^NSEI",period="MAX", interval="1D")["Close"] #Nifty_50 Indian Index
rsi = vbt.RSI.run(nifty, window = 14) print(rsi.rsi)

Real Values of RSI - 58.6 - 8th 56.31 - 7th 52.32 - 6th

image

Is anyone else facing the same issue?

@barnjamin @polakowo @RileyMShea @emiliobasualdo @kmcentush

a3rwow avatar Jul 09 '22 13:07 a3rwow

I tested with vectorbtpro and compared several prices with Talib. I confirm that they are all different. I am not familiar with pandas-ta and did not test with it. I could not explain exactly why though. I could not find obvious mistake, I think ma_nb is different from the implementation in Talib.

I made 2 generic examples that could bring on the track. ts = pd.DataFrame({ 'c': [1.,2.,1.,2.,1, 2, 1, 2, 1, 2, 1, 2, 1,2,1,2,1,2,1,2], }, index=pd.DatetimeIndex([ datetime(2018, 1, 1), datetime(2018, 1, 2), datetime(2018, 1, 3), datetime(2018, 1, 4), datetime(2018, 1, 5), datetime(2018, 1, 6), datetime(2018, 1, 7), datetime(2018, 1, 8), datetime(2018, 1, 9), datetime(2018, 1, 10), datetime(2018, 1, 11), datetime(2018, 1, 12), datetime(2018, 1, 13), datetime(2018, 1, 14), datetime(2018, 1, 15), datetime(2018, 1, 16), datetime(2018, 1, 17), datetime(2018, 1, 18), datetime(2018, 1, 19), datetime(2018, 1, 20), ]))

So a succession of 1 and 2. With Talib: 2018-01-14 NaN 2018-01-15 50.000000 2018-01-16 53.571429 2018-01-17 49.744898 2018-01-18 53.334548 2018-01-19 49.524938 2018-01-20 53.130299

With vbt:

2018-01-14 NaN 2018-01-15 50.0 2018-01-16 50.0 2018-01-17 50.0 2018-01-18 50.0 2018-01-19 50.0 2018-01-20 50.0

Replacing the last 2 by a 1: 'c': [1.,2.,1.,2.,1, 2, 1, 2, 1, 2, 1, 2, 1,2,1,2,1,2,1,1],

With Talib: 2018-01-14 NaN 2018-01-15 50.000000 2018-01-16 53.571429 2018-01-17 49.744898 2018-01-18 53.334548 2018-01-19 49.524938 2018-01-20 49.524938

With vbt

2018-01-14 NaN 2018-01-15 50.000000 2018-01-16 50.000000 2018-01-17 50.000000 2018-01-18 50.000000 2018-01-19 50.000000 2018-01-20 46.153846

46.153846 is 6/13. So I understand how vbt calculates, but not Talib. Hope that helps.

psemdel avatar Jul 09 '22 21:07 psemdel

I think, I found the difference https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/momentum/rsi.py Uses positive_avg = rma(positive, length=length)

And RMA is """wildeR's Moving Average (RMA) The WildeR's Moving Average is simply an Exponential Moving Average (EMA) with a modified alpha = 1 / length.

https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/overlap/rma.py

VBT uses a normal MA, and Pandas_ta an exponential one. Reading this: https://www.macroption.com/rsi-calculation/ Part "Step 2: Averaging the Advances and Declines", it seems to be a choice.

psemdel avatar Jul 09 '22 21:07 psemdel

https://github.com/polakowo/vectorbt/issues/78#issuecomment-753969170

polakowo avatar Jul 09 '22 21:07 polakowo

Great analysis, as a rule of thumb: use vbt's methods for regular SMA/EMA, and talib for Wilder's. Both have the same performance (talib is slightly faster), and you can always convert between different smoothing methods, see the comment above.

polakowo avatar Jul 09 '22 21:07 polakowo