ADX calculation

 

Hi everyone,

I'm trying to figure out how ADX is calculated in MT. I have studied the documentation and the mql code of the indicator but am still observing something wrong.

I'm experimenting with monthly data to see the calculation from the first bars of history. Using the standard 14 period and Closed applied price.
The indicator start drawing on the chart from the 15th bar, but the data window shows values yet from the second bar.

Assume we have the following OHLC bar data:

2002.06.01,00:00,0.93240,0.99140,0.93240,0.99100
2002.07.01,00:00,0.99100,1.01180,0.97260,0.97730

Then the first value (starting from the second bar) should be calculated as follows:

1. price move
+M = High(i) - High(i - 1) => 0.0204
-M = Low(i - 1) - Low(i) => -0.0402

2. directional move and true range
+DM = if +M > -M and +M > 0 then +M else 0 => 0.0204
-DM = if -M > +M and -M > 0 then -M else 0 => 0
TR = Max((High - Low), Abs(High - Close(i - 1)), Abs(Low - Close(i - 1))) => 0.0392

3. the first exponential value of directional movement index
+DI = +DM / TR * 100 => 52.0408
-DI = -DM / TR * 100 => 0

4. the first exponential value of average directional movement index
ADX = Abs(+DI - -DI) / (+DI + -DI) * 100 => 100

But the terminal displayes the following:
+DI = 6.9388
-DI = 0
ADX = 13.3333

What is wrong here?
 
ADX = SUM ((+DI - (-DI)) / (+DI + (-DI)), N) / N
          Average Directional Movement Index - Trend Indicators - MetaTrader 5
 
William Roeder:
I'm using exactly the same formula, so why does the result differ?
 
Your not, PDI ≠ +DI and ADX ≠ EMA( ADX )
You
 My post

PDI = EMA( +DI )
NDI = EMA( -DI )
ADX = Abs(+DI - -DI) / (+DI + -DI) * 100 ADX = EMA( 100 * Abs( (PDI - NDI) / (PDI + NDI) )
 
William Roeder:
Your not, PDI ≠ +DI and ADX ≠ EMA( ADX )

The first value has nothing to be smoothed with, so the first EMA(value) equals the value itself. E.g. if you plot an EMA(PRICE_CLOSE, N) onto a chart you will see that the first EMA=PRICE_CLOSE.

So the very first value of EMA(+DI)=+DI and hence the very first ADX is not smoothed either. If not, how else would you calculate EMA(+DI) for the second bar in the history then?

 

I have finally got to the bottom of it, and dare say that MetaTrader calculate ADX incorrectly.

When EMA of PDI, NDI and DX is calculated for the second bar in history the preceding EMA value is set to zero. This contradicts the EMA formula which assume the first EMA value to be equal the value itself.
This influences the entire ADX calculation.

 

William Roeder was right. I managed to get the same results for ADX, +DI and -DI, though I used Excel for calculations.


I'd swap names of variables from comments above: PDI and +DI, NDI and -DI. PDI and NDI are +DI and -DI from terminal, so they are better used as intermediary variables names in order not to make confusion. Like, PDI =  +DM / TR * 100, whereas +DI = EMA (PDI) for N bars.

Reason: