twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.19k stars 1.01k forks source link

RSI_14 not the same ? #553

Open gugeswe opened 2 years ago

gugeswe commented 2 years ago

Which version are you running? The lastest version is on Github. Pip is for major releases.

import pandas_ta as ta
print(ta.version)

Do you have TA Lib also installed in your environment?

$ pip list

Did you upgrade? Did the upgrade resolve the issue?

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Describe the bug A clear and concise description of what the bug is.

To Reproduce Provide sample code.

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

Thanks for using Pandas TA!

Hi, I just noted that the RSI_14 from pandas_ta does not give the same values as I can get from trade view and my own formula. This is from pandas_ta:

TSLA RSI_14 from pandas_ta
2022-06-06    42.563087
2022-06-07    42.810747
2022-06-08    44.086149
2022-06-09    43.331821
2022-06-10    40.733739
*2022-06-13    35.655047
2022-06-14    38.245809
2022-06-15    43.956400
2022-06-16    37.774685
2022-06-17    39.460991
2022-06-21    47.886861
2022-06-22    47.552924
2022-06-23    47.173791
2022-06-24    51.528079
2022-06-27    51.192003
2022-06-28    46.142342

this is from my own formula: TSLA rsi from formula

2022-06-06    42.489825
2022-06-07    42.755708
2022-06-08    44.122326
2022-06-09    43.315136
2022-06-10    40.549813
*2022-06-13    35.209665
2022-06-14    37.958873
2022-06-15    43.975053
2022-06-16    37.533967
2022-06-17    39.295094
2022-06-21    48.035847
2022-06-22    47.689386
2022-06-23    47.296236
2022-06-24    51.775597
2022-06-27    51.427470
2022-06-28    46.213699

This is the formula I use (gives the same as tradeview.com)

def RSI( df, window_length=14):
    close = df['Close']
    delta = close.diff()
    up = delta.clip(lower=0)  
    down = -1 * delta.clip(upper=0)
    # WMA
    roll_up = up.ewm(com=window_length - 1, adjust=True, min_periods=window_length).mean()
    roll_down = down.ewm(com=window_length - 1, adjust=True, min_periods=window_length).mean()
    # Calculate the RSI
    RS = roll_up / roll_down
    return 100.0 - (100.0 / (1.0 + RS))

I am trying to detect minimum points in RSI and above I have marked with '*' the different values from pandas_ta and my own.

twopirllc commented 2 years ago

Hello @gugeswe,

Since you did not mention which version you are running, I am assuming it is version 0.3.14b.

I just noted that the RSI_14 from pandas_ta does not give the same values as I can get from trade view and my own formula.

That's highly probable as this package is a Python port of the de facto TA Lib library (if a user is unable to install TA Lib) or wrapper for TA Lib (if it is installed in their local environment). While this library does match many indicators with TradingView, this is not a TradingView library nor do they support it any way. 🤷🏼‍♂️

For more info regarding rsi being discussed can be found at Issue #69. In short:

That being said, I do not mind adding a TradingView version, but it is more difficult to test against as they do not have an easily testable library.

In the mean time, use your version of rsi until I, you or someone else makes a Pull Request to make an toggleable TradingView version. A numpy version would be best for performance reasons. 😎

Kind Regards, KJ

PilotGFX commented 1 month ago

Given that RSi is relatively simple and there is full concensus on how to calculate it, hence it should be a fully deterministic result without any variance, at least to the first 3-4 digits after the fraction denominator, as there can be induced microscopic variances due to different type of floats used etc. it is to be expected that it should not differ from platform to platform. if TA-Libs RSI calculation is wrong, i will recommend that we use the correct formula instead. even if we are not able to leverage the speedy C mechanics of TA-Lib, i think we should be able to calculate rsi pretty fast with numpy or similar. Ofcourse if your intend is to be a truthful wrapper to TA-Lib i can see this is a dilemma, but it would be justified to do given the above reasons :)