question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ADX value from plugin vastly different from Tradingview, just me or did I mess up some entry?

See original GitHub issue

Hi,

So first off this problem persists across multiple plugins and code. I love the pandas_ta library thus was hoping we could find a solution here.

First off here is the difference in values: image image

Here is my code:

def each_copy(ticker):
    ticker_data = pd.DataFrame.copy(df[df["ticker"]==ticker],deep=False)
    ticker_data.set_index('date',inplace=True)
    return ticker_data

test_data = each_copy("MRPL")
test_data["adx"] = ta.adx(high=test_data.high,low=test_data.low,close=test_data.close)["ADX_14"]
test_data["aDMP_14"] = ta.adx(high=test_data.high,low=test_data.low,close=test_data.close)["DMP_14"]
test_data["aDMN_14"] = ta.adx(high=test_data.high,low=test_data.low,close=test_data.close)["DMN_14"]
test_data.loc["2022-04-19 15:15:00+05:30":]

Data from the function ‘each copy’ is returned this way image

After adding adx data image

I hope this is enough, I did notice that the code on Tradingview is different from what we are using here. Could that be the reason? Does TV use some other formula for ADX?

I am not much of a coder thus relying on your help.

Please help and thank you.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hakumakucommented, Jul 20, 2022

The reason adx value is different is while TradingView handles the initial value differently, pands-ta does not.

I faced this issue recently, and I don’t know about the very detail of adx indicators, but I figured out where the difference comes from by looking at the code.

As you might know, rma depends on the previous value to get the current value. How to set the first value of rma seems to vary but TradingView uses the average of values.

The below is the code block of adx function. The version(master, dev) does not matter, because both do not seem to handle the first value.

def adx(
    high: Series, low: Series, close: Series,
    length: Int = None, lensig: Int = None, scalar: IntFloat = None,
    mamode: str = None, drift: Int = None,
    offset: Int = None, **kwargs: DictLike
) -> DataFrame:
    ...
    k = scalar / atr_
    dmp = k * ma(mamode, pos, length=length)
    dmn = k * ma(mamode, neg, length=length)

    dx = scalar * (dmp - dmn).abs() / (dmp + dmn)
    adx = ma(mamode, dx, length=lensig)
    ...

All of dmp, dmn and adx depend on ma("rma", ...) method.

If you delve into the function deeper, it basically uses pandas.DataFrame.ewm method.

It has adjustbool, default True parameter that you should pay attention to.

Setting it to True acts as the following: $y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + … + (1 - \alpha)^t x_0}{1 + (1 - \alpha) + (1 - \alpha)^2 + … + (1 - \alpha)^t}$

False which is what we want (to sync with TradingView): $\begin{split}\begin{split} y_0 &= x_0 \ y_t &= (1 - \alpha) y_{t-1} + \alpha x_t, \end{split}\end{split}$

It seems like it does not provide a way of handling $y_0 = x_0$ part, and I couldn’t find it either. (someone asked the question at stackoverflow)

To work around this… you should hack input values a bit to get desired result:

    # Set the initial value to sum() of 'length'.
    pos.iloc[length-1] = pos[:length].sum()
    pos[:length-1] = 0
    neg.iloc[length-1] = neg[:length].sum()
    neg[:length-1] = 0
    ...
    dmp = k * pos.ewm(alpha=alpha, adjust=False, min_periods=length).mean()
    dmn = k * neg.ewm(alpha=alpha, adjust=False, min_periods=length).mean()

adx is a bit more tricky. It requires length of both dmpand dmn values (at least for TradingView). If your data starts from 06-19, the first vaue of adx will appear at 07-16 which is 27 days later assuming the length is set to 14.

    ...
    # Set the initial value to sum() of 'length'.
    dx = dx.shift(-length)
    dx.iloc[length-1] = dx[:length].sum()
    dx[:length-1] = 0

    adx = dx.ewm(alpha=alpha, adjust=False, min_periods=length).mean()
    adx = adx.shift(length) # rollback

I’m not sure this is considered to be a bug or something, but I hope my explanation helps.

0reactions
anon2010commented, Oct 17, 2022

How can I implement this fix? Swap out Hakumaku’s def adx method for the one in adx.py ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Average Directional Index (ADX) - TradingView
The Average Directional Index (ADX) is a specific indicator used by technical analysts and traders in order to determine the strength of a...
Read more >
ADX and DI advanced — Indicator by ippp0031 - TradingView
Since i have no knowledge of programing i was wondering if would be possible to add columns that would indicate the difference of...
Read more >
[PX] ADX Boxes — Indicator by paaax - TradingView
Hello guys, today I would like to share an indicator that I had in my mind for quite some time. I call it...
Read more >
Page 5 Average Directional Index (ADX) — Technical Indicators
I have added 3 items to the RSI indicator which helps me to get more information at the same time. 1) ADX value...
Read more >
Tradingview exit indicators. Scripts 25. trendanalysis. ADX ...
For 1) The entry-stop loss will be 2% away so set at $8. ... intend to stay clear of if you wish to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found