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.4k stars 1.05k forks source link

Adx not completely correct with tradingview #68

Closed tzvetkovg closed 4 years ago

tzvetkovg commented 4 years ago

I've tried the ADX indicator like below adx = ta.adx(df[ticker]['high'], df[ticker]['low'], df[ticker]['close'], length=14) and returns close but not exactly the same results as trading view. The difference is about 1% up/down.

twopirllc commented 4 years ago

Hello @tzvetkovg,

Thanks for trying pandas-ta!

I test my indicators against TA-lib which is generally the de facto standard for Technical Analysis. As such ta.adx is currently 98.9% correlated with TA Lib.

While I do like TradingView, there is currently no easy way, that I know of, to run pandas-ta correlations against their TA library. However, you are welcome to fork and make changes to help narrow the difference with TA Lib. Any improvement would be grateful.

Thanks, KJ

twopirllc commented 4 years ago

Hello @tzvetkovg,

Here are the Correlation Matrix among: TradingView, Panda TA, and TA Lib.

Columns: TradingView: DI+, DI-, ADX Pandas TA: ADX_14, DMP_14, DMN_14 TA Lib: TAL_DI+, TAL_DI-, TAL_ADX_14

Screen Shot 2020-08-22 at 6 58 27 PM

For the time being, I will not be making adjustments to adx as I am currently satisfied with the correlations among the three. As I said earlier, you are welcome to contribute to help improve the accuracy.

Thanks for using Pandas TA!

Regards, KJ

slhawk98 commented 3 years ago

Hey! Looks like pandas_ta adx results matches Tradingview code at this link https://www.tradingview.com/script/v4xHOAfK-ADX-and-DI-Average-Directional-Index-CORRECT-FORMULA/

twopirllc commented 3 years ago

@slhawk98,

Looks like pandas_ta adx results matches Tradingview code at this link

What does that even mean?

slhawk98 commented 3 years ago

The comments from this Tradingview version of ADX indicator are listed below. Notice the words "CORRECT-FORMULA" in the title.

I have an ADX trading strategy I which to automate via python. What this means for me?
Values returned by PANDAS_TA ADX are plotted correctly on the Tradingview chart. This will allow me to manual back test my strategy to see were it may need tweaked.

In my opinion, there is nothing wrong with the PANDAS_TA ADX calculations but instead an error in how some of the original Tradingview ADX indicators were constructed (see comments).

I am excited to move forward with confidence in developing my new strategy. Thanks!

COMMENTS FROM Tradingview Indicator: So... the popular version of "ADX and DI" shown on tradingview by masanakamura uses some wacky and incorrect formula for DI and uses SMA instead of the Wilder's MA that ADX /DI is supposed to use, and doesn't give the correct ADX and DI values. The Tradingview "Average Directional Index" uses the correct calculation but doesn't show DI+ and DI-. So, here it is with the DI+ and DI- visible. Enjoy!

AGG2017 commented 3 years ago

I think the difference comes from the RMA function. I did 100% copy of the TV RMA in my OSCAR implementation and the result was exactly what was shown at TV.

twopirllc commented 3 years ago

@slhawk98

Thanks for elaborating in https://github.com/twopirllc/pandas-ta/issues/68#issuecomment-912472551. Makes more sense.

Also in Pandas TA's adx (help(ta.adx)), you can change the ma of adx. It's default is rma.

import pandas_ta as ta

print(f"Ma Modes: {', '.join(ta.ma())}")

df = # ohlcv
df.ta.adx(mamode=None, append=True) # Default "rma"
df.ta.adx(mamode="ema", append=True)

Good luck! KJ