22

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

What is wrong here?
19170

ADX = SUM ((+DI - (-DI)) / (+DI + (-DI)), N) / N
Average Directional Movement Index - Trend Indicators - MetaTrader 5
22

William Roeder:
I'm using exactly the same formula, so why does the result differ?
19170

You
My post

PDI = EMA( +DI )
NDI = EMA( -DI )
ADX = Abs(+DI - -DI) / (+DI + -DI) * 100 ADX = EMA( 100 * Abs( (PDI - NDI) / (PDI + NDI) )
22

William Roeder:

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?

22

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.