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.5k stars 1.08k forks source link

RSI/Stoch/ADX questions #40

Closed fypdoctave closed 4 years ago

fypdoctave commented 4 years ago

Hi,

I have been using your library to understand TA. First off, thank you for the amazing work. It has been really useful to have!

Wherever possible, I have been double checking the indicator values calculated either on Yahoo Finance (YF) or StockCharts (SC). As such, I wanted to ask for some clarifications on couple of things:

  1. Using df.ta.rsi(length=14) gives me RSI results that are little bit off from YF or SC or manual calculation in excel. However, with df.ta.rsi(length=13), results pretty match with YF (Period 14) or manual work (again using 14 periods). I am not sure why there is a mismatch with ta.rsi default value of 14. Can you please check?

  2. In df.ta.stoch(), I noticed calling with default values gave different results from YF/SC. Digging deeper, in stoch.py, lines 18 & 19, I'm thinking shouldn't the value used be fast_k instead of slow_k. If I update the below to fast_k, I do get matching results with both sources mentioned. Maybe you had something else in mind while implementing?

Calculate Result

lowest_low   =  low.rolling(slow_k).min()
highest_high = high.rolling(slow_k).max()
  1. Using df.ta.adx(), I do get the ADX_14 value that matches with YF/SC but DMP_14 & DMN_14 are not matching. I was trying to understand your methodology of ADX calculation. I think the difference is due to smoothing techniques used. In adx.py, atr & rma calculations use ewm techniques. SC uses a different way of smoothing TR, +DM & -DM. For example:

First TR14 = Sum of first 14 periods of TR1 Second TR14 = First TR14 - (First TR14/14) + Current TR1 Subsequent Values = Prior TR14 - (Prior TR14/14) + Current TR1

as explained here: https://school.stockcharts.com/doku.php?id=technical_indicators:average_directional_index_adx

I'm not sure what the above smoothing methodology is called but can you please comment on the difference? Also, is there way to use your existing functions to implement this smoothing method?

Sorry for the long post. I greatly appreciate your work and am trying to understand as much as possible. Any further help is greatly appreciated too!

twopirllc commented 4 years ago

Hello @fypdoctave,

Thanks! Glad you find it useful.

I have addressed calculation issues before with Issue #23.

Now I do not use Yahoo Finance nor StockCharts for TA comparisons. They do not list what TA Library that they use or if it's proprietary. StockCharts does not address it anywhere on their FAQ. I will be not cross referencing with YF' RSI (for example) because I can only export the historical data and not it's RSI calculations without having to transcribe the RSI values from the chart to a CSV by hand and then run correlation tests. Likewise, I will not be using SC because I do not have an account and I'm sure it is the same cumbersome process just to compare indicators for correlations as well. I do use TradingView for two purposes when developing pandas_ta indicators: visual test of output and if needed easy export of data to CSV.

You are correct, there are some indicators that are not exact. These can be found by running python -m unittest -v if you also have TA=Lib installed. I run a correlation test between the pandas_ta indicators versus those that are implemented in TA-Lib. Indicators that are not greater than 99% correlated and implemented in TA-Lib include: APO, CCI, CMO, STOCH, ADX, AROON Down & Up, BB Upper and Lower Bands, and AD (open).

Are the indicators that are less than 99% correlated less than desirable? Certainly. Could they be improved? Of course, I would like to improve them. But sadly, I have spent more time on them than I thought just to get them to their current percentage of correlation. Nevertheless, there will be some lack of 1-to-1 correspondence between different libraries (and languages) because floating point calculations are imperfect.

Regarding:

  1. ta.rsi: When the length is greater than 21, only then does rsi begin to deviate from TA-Lib's RSI.
  2. ta.stoch: I ran a quick test with swapping 'slow_k' with 'fast_k' as suggested. Using 'slow_k', the correlations with TA Lib are: Screen Shot 2020-05-02 at 1 15 36 PM

    With your suggestion with 'fast_k' instead, yielded the following correlations with TA Lib:

    Screen Shot 2020-05-02 at 1 18 51 PM
  3. ta.atr is 99% correlated with TA Lib.

I hope that this has addressed most of your concerns regarding the indicators in question and their accuracy. In your quest to learn more about Technical Indicators and their calculations, this is a good start. Since I know absolutely nothing about you, I recommend learning about TA by writing your own on TradingView using their language called Pinescript. Pinescript is very easy to use in contrast to Python and allows you to immediately see your results.

Since you recently joined Github, welcome! However, may I also recommend installing other Open Source TA Library Repositories and test their libraries for calculation and comparison and let me know what you think?

Hope this helps, KJ

fypdoctave commented 4 years ago

Thank you KJ for taking the time and explaining. I will definitely utilize some of the packages you mentioned above.