pandas-ta
pandas-ta copied to clipboard
fix: sync result with TV
Set the initial value to the average of previous values to get desired output. Ref #522.
Hello @hakumaku,
Excellent! 😎 I will update as soon as I can.
Thanks for the contribution! KJ
Please let me know if it is not met with development guidelines or if you find issues with the commit.
Was this deemed correct or flawed?
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()
Correlation
ADX, DM+, DM-
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
@hakumaku & @anon2010,
This is up on the development branch for testing and fixing when you can.
KJ