ta-lib-python
ta-lib-python copied to clipboard
PLUS_DI gives different results
Hello!
I use ta-lib in the freqtrade bot. When I plot indicators, It seems that PLUS_DI indicator in the ta-lib and DMI with ADX smoothing = 14 and DI length = 14 on the tradingview.com give different results.
E.g.:
ETHBTC 14 May 19, 01:00(UTC) PLUS_DI in tradingview.com: 5.549
ETHBTC 14 May 19, 01:00(UTC) PLUS_DI in ta-lib: 3.070
So, how can I get the ta-lib DMI indicator in the freqtrade strategy with the same settings as on the tradingview.com?
What are the values of the indicator in the nearest candles on tradingview? Isn't they are just shifted by 1 (or -1) candle?
@hroff-1902 I don't think so. Please look at the plots below:
PLUS_DI in ta-lib:
PLUS_DI in tradingview.com:

Also, plots very clearly shows that PLUS_DI in ta-lib does not cross the 10 mark before May 14 01:00, but DMI in tradingview.com crosses this mark at May 13 19:00.
if you use ta-lib with freqtrade, make sure to apply the decimal patch (to the underlying C library) as otherwise it has some trouble with the small numbers BTC uses.
Also make sure you have enough history - iirc plus_di uses smoothening of TR, so with 14, 14 as parameters, you'll need 28 candles to get "good" results (but i'm not 100% sure on the formula). this would explain why it "seems" correct(er) a bit later
this is a working python code values are different from tradingview or investing, but timing of +di and -di crossovers are correct
adx, +di, -di embedded, reference from: https://medium.com/codex/does-combining-adx-and-rsi-create-a-better-profitable-trading-strategy-125a90c36ac
`import pandas as pd
def get_adx(high, low, close, lookback):
plus_dm = high.diff()
minus_dm = low.diff()
plus_dm[plus_dm < 0] = 0
minus_dm[minus_dm > 0] = 0
tr1 = pd.DataFrame(high - low)
tr2 = pd.DataFrame(abs(high - close.shift(1)))
tr3 = pd.DataFrame(abs(low - close.shift(1)))
frames = [tr1, tr2, tr3]
tr = pd.concat(frames, axis = 1, join = 'inner').max(axis = 1)
atr = tr.rolling(lookback).mean()
plus_di = 100 * (plus_dm.ewm(alpha = 1/lookback).mean() / atr)
minus_di = abs(100 * (minus_dm.ewm(alpha = 1/lookback).mean() / atr))
dx = (abs(plus_di - minus_di) / abs(plus_di + minus_di)) * 100
adx = ((dx.shift(1) * (lookback - 1)) + dx) / lookback
adx_smooth = adx.ewm(alpha = 1/lookback).mean()
return plus_di, minus_di, adx_smooth
`
df = pd.read_csv('1D/XU100-1D.csv') tempor = get_adx (df["High"], df["Low"], df["Close"], 14) df["PLUS_DI_14"] = tempor [0] df["MINUS_DI_14"] = tempor [1] df["ADX_14"] = tempor [2] pd.set_option('display.max_rows', df.shape[0]+1) print(df)