pandas-ta icon indicating copy to clipboard operation
pandas-ta copied to clipboard

fix: sync result with TV

Open hakumaku opened this issue 3 years ago • 2 comments

Set the initial value to the average of previous values to get desired output. Ref #522.

hakumaku avatar Aug 15 '22 08:08 hakumaku

Hello @hakumaku,

Excellent! 😎 I will update as soon as I can.

Thanks for the contribution! KJ

twopirllc avatar Aug 21 '22 22:08 twopirllc

Please let me know if it is not met with development guidelines or if you find issues with the commit.

hakumaku avatar Aug 22 '22 00:08 hakumaku

Was this deemed correct or flawed?

anon2010 avatar Oct 17 '22 18:10 anon2010

Hello @hakumaku & @anon2010,

Current Status

  • Included TA Lib's ADX, DMP and DMN since they were not part of the source at the time.
  • @hakumaku's TV version
  • Prior Pandas TA version of adx that does not match either TA Lib's or @hakumaku's version.

TV v5 Data and Code

Here is the exported csv based on TV's version 5 of ADX, DMP, DMN. (It also includes and RWI version 3; which you can drop, it's for another PR).

//@version=5
indicator("Average Directional Index", shorttitle="ADX", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")

dirmov(len) =>
	up = ta.change(high)
	down = -ta.change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
	minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = ta.rma(ta.tr, len)
	plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
	[plus, minus]

adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)

sig = adx(dilen, adxlen)
plot(sig, color=color.black, linewidth=3, title="TVv5_ADX")

[dmp, dmn] = dirmov(dilen)
plot(dmp, color=color.green, linewidth=1, title="TVv5_DM+")
plot(dmn, color=color.red, linewidth=1, title="TVv5_DM-")

Current Output

@hakumaku, do you recall which TV version of ADX you were basing your code on? The code you provided satisfies your ADX test but takes some bars before it converges with TV's.

Values

import pandas as pd
import pandas_ta as ta

pd.options.display.min_rows = 30

adxdf = pd.read_csv(
    "BATS-SPY-D-TVv5-ADX_DMP_DMN_RWI.csv",
    index_col=0, parse_dates=True,
    infer_datetime_format=True, keep_date_col=True,
)
adxdf.drop(columns=["Volume", "Volume MA", "High", "Low"], inplace=True)

adxdf.ta.adx(talib=False, tvmode=True, append=True)

adxdf.drop(columns=["open", "high", "low", "close"], inplace=True)
adxdf.iloc[25:50].style.background_gradient()
Screenshot 2023-03-24 at 4 38 38 PM

Correlation

Screenshot 2023-03-24 at 4 38 52 PM

ADX, DM+, DM-

Screenshot 2023-03-24 at 4 39 42 PM

I will push the current version this weekend and hopefully we can iron the differences by the end of next week with a final TV version PR. 😎 (A corrected Python version matching TA Lib will have to be done with another PR).

Apologies for not being able to address this fix sooner. 😬

Kind regards, KJ

twopirllc avatar Mar 25 '23 00:03 twopirllc

@hakumaku & @anon2010,

This is up on the development branch for testing and fixing when you can.

KJ

twopirllc avatar Mar 25 '23 18:03 twopirllc