Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value of RSI Indicator does not match with Tradingview or PandasTA #471

Closed
a3rwow opened this issue Jul 9, 2022 · 4 comments
Closed

Value of RSI Indicator does not match with Tradingview or PandasTA #471

a3rwow opened this issue Jul 9, 2022 · 4 comments

Comments

@a3rwow
Copy link

a3rwow commented Jul 9, 2022

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

@psemdel
Copy link

psemdel commented Jul 9, 2022

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
Copy link

psemdel commented Jul 9, 2022

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.

@polakowo
Copy link
Owner

polakowo commented Jul 9, 2022

#78 (comment)

@polakowo
Copy link
Owner

polakowo commented Jul 9, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants