bennycode / trading-signals

Technical indicators to run technical analysis with JavaScript & TypeScript. 📈
https://bennycode.com/trading-signals
MIT License
621 stars 90 forks source link

is ADX correct? (difference with TradingView) #306

Closed enriquepiatti closed 3 years ago

enriquepiatti commented 3 years ago

I'm comparing the results from TradingView charts (with ADX built in indicator), same input period, same candles (this is ok because the SMA for example is returning same values in TradingView and with trading-signals), but the result for ADX is not the same!
Does somebody know why is that?
Do you know some other chart with ADX indicator to compare with a 3rd reference?

This is the code used by TradingView (in PineScript):

up = change(high)
down = -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 = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / truerange)
minus = fixnan(100 * rma(minusDM, len) / truerange)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)

plusDM and minusDM is the equivalent to
const { mdm, pdm } = this.directionalMovement(this.prevCandle, candle);
and they are returning the same values in TradingView and trading-signals., but the rest of the code is a bit confusing, can you note some difference in the way is calculated the ADX in TradingView? which one is the correct?

enriquepiatti commented 3 years ago

One difference seems to be the smooth function used, TradingView is using "rma":

Moving average used in RSI. It is the exponentially weighted moving average with alpha = 1 / length.

that gives some small difference in "plus" and "minus" from TradingView (corresponding to pdi and mdi from trading-signals).

But the important difference in values I think is in the last formula for adx, are TradingView and trading-signals doing different things?

bennycode commented 3 years ago

Hi @enriquepiatti. I think TradingView's ADX implementation is not standard compliant here. I have tested my ADX implementation and verified it with another implementation as you can see here: https://github.com/bennycode/trading-signals/blob/v2.0.1/src/ADX/ADX.test.ts#L36-L39

My implementation uses SMMA for smoothing. If it makes sense we can also add RMA (Relative Moving Average) as an option. Can you give me some input data and expected results for testing?

enriquepiatti commented 3 years ago

I think is not the only difference the SMMA vs RMA, here is the data for testing:

close: [49320,48777,48708,47911.5,47883.5,47026,46860,46891.5,46897.5,46769,46956,46828.5,46928,47185,46832.5,46791.5,46870.5,46959,46999,46922.5,46981.5,47249.5,47179,46835.5,47261,47250,47213.5,46785.5,46917.5,47108,47162,47483,47185.5,47193,47410,47455.5,47340,47262.5,48123.5,48172,48190.5,48190,48333.5,48292.5,48911.5]

high: [49370.5,49365,48825,48728.5,48018,48049,47244.5,47024,47072.5,47000,47040,47174.5,47400,47245.5,47399.5,47040,47050,47090,47078,47027,47046,47312,47422,47390,47300,47647,47490.5,47263,46974.5,47189,47249,47575,47582.5,47329,47495.5,47787,47539.5,47399,48275,48426.5,48273,48337,48370,48559,49213.5] low: [48844.5,48777,48644.5,47845.5,47600.5,46813,46735,46566.5,46685,46741,46702,46805.5,46272,46852.5,46828,46632,46505,46870,46718,46735.5,46834,46981,47116.5,46815.5,46335.5,47172,47203.5,46667,46573,46856,47027.5,46840,47125.5,46980.5,47182,47344.5,47230,46999.5,47178.5,48124,48004.5,48162,48140,48275,48185]

Expected result (this is the ADX value from TradingView for the last item): 24.51

RMA implementation in PineScript (according to https://stackoverflow.com/questions/48055618/how-tradingview-pine-script-rma-function-works-internally):

pine_rma(x, y) =>
    alpha = 1/y
    sum = 0.0
    sum := alpha * x + (1 - alpha) * nz(sum[1])
bennycode commented 3 years ago

Thank you for providing sample data. On Trading View there are multiple implementations of ADX from different authors. Which one are you using?

image

enriquepiatti commented 3 years ago

I'm using official ADX from TradingView, is marked as "built in", Average Directional Index.
It allows to see the pinescript code too, is the one I've posted (actually I have simplified it a bit to make it more clear what it's doing)

enriquepiatti commented 3 years ago

I couldn't replicate the same result from TradingView for RMA, not sure what I'm doing wrong, maybe you can see it. This was my best approx:

class RMA extends MovingAverage_1.MovingAverage {
    constructor() {
        super(...arguments);
        this.prices = [];
        this.alpha = 1/this.interval; 
        this.rma = 0;
    }
    update(price) {

        this.prices.push(new big_js_1.default(price));
        if (this.prices.length > this.interval) {
            this.prices.shift();
        }

        if(this.rma === 0){
            this.rma = price;
        }
        // this.rma = this.alpha * price + (1 - this.alpha) * this.rma;
        this.rma = this.rma + (price - this.rma)*this.alpha;
        this.setResult(new big_js_1.default(this.rma));
bennycode commented 3 years ago

Can you reproduce the results from Tulip Indicators' ADX implementation? https://tulipindicators.org/adx

enriquepiatti commented 3 years ago

reproduce how? I don't know how to use Tulipan indicators, do they have an online version available? Anyway, I think this is something related with TradingView, they must be calculating the ADX in a different way, probably is just that RMA instead of SMMA.
The problem I could't reproduce the values from RMA either!
and I think that's because RMA from TradingView is not using only the candles from the period/interval passed to the indicator, but is using all the candles, the interval is used for the calculation, but it requires more history to get the value used in TradingView (I will try that later).
But maybe could be a good addon to have an RMA and an ADX similar to TradingView too, is a very popular chart

bennycode commented 3 years ago

You don't have to run Tulip Indicators. They have their test input and test output listed here: https://tulipindicators.org/adx

I also have some other indicator implementations listed here: https://github.com/bennycode/trading-signals#alternatives

Usually, I check the test data of other libraries to know if my library is compatible or consistent with their output. That's an alternative approach to testing with TradingView.