iADX in Pine Script: ta.dmi matches iADXWilder, not iADX
Short answer
ta.dmi(n, n) ports iADXWilder, Wilder's original with α = 1/n smoothing. MetaTrader's iADX smooths the directional indicators with an EMA (α = 2/(n+1)) started from zero; Pine has no built-in for it, so write that recursion by hand.
MetaTrader ships two ADX indicators. iADX is MetaQuotes' own variant: it smooths the per-bar directional indicators with an exponential average (α = 2/(n+1)) started from zero. iADXWilder is Welles Wilder's original, with α = 1/n smoothing of the directional movement and true range. Pine's ta.dmi(diLength, adxSmoothing) implements Wilder's method, so it is the port of iADXWilder; porting iADX means writing the EMA recursion by hand.
What iADX and iADXWilder compute
Both take (symbol, timeframe, adx_period) and expose three buffers: MAIN_LINE (ADX, index 0), PLUSDI_LINE (1) and MINUSDI_LINE (2). Both start from the same raw directional movement: +DM = high − highprev, −DM = lowprev − low, negatives clamped to zero, the smaller of the two zeroed, and both zeroed on a tie. True range is max(high − low, |high − closeprev|, |low − closeprev|).
iADX (Indicators\Examples\ADX.mq5) then forms per-bar ratios 100·+DM/TR and 100·−DM/TR and smooths each with x·α + prev·(1−α), α = 2/(n+1), with the previous value 0 on bar 0. DX is 100·|+DI − −DI| / (+DI + −DI) from the smoothed lines, and ADX is the same EMA of DX. Values exist from bar 1 and ramp up from zero.
iADXWilder (ADXW.mq5) smooths +DM, −DM and TR separately with (prev·(n−1) + x)/n starting at bar n from a previous value of 0, then +DI = 100·s+DM / sTR, −DI likewise, DX as above and ADX as the same smoothing of DX. Cells before bar n are 0.0.
The Pine Script equivalent
For iADXWilder, ta.dmi(len, len) returns [plusDI, minusDI, adx] using ta.rma, which is Wilder's α = 1/n recursion; the only difference is the seed, since ta.rma starts from a simple average while MetaTrader starts from zero, and that difference decays by (1 − 1/n) per bar. For iADX there is no builtin: carry the three EMA states in var variables as below. The script plots both so you can compare them.
//@version=6
indicator("iADX / iADXWilder port", overlay = false)
len = input.int(14, "Period", minval = 1)
// ---- iADX: MetaTrader's own ADX. Per-bar DI, then an EMA seeded at zero.
upMove = high - high[1]
downMove = low[1] - low
plusDM = upMove > downMove and upMove > 0 ? upMove : 0.0
minusDM = downMove > upMove and downMove > 0 ? downMove : 0.0
tr = ta.tr(true)
rawPlus = tr == 0 ? 0.0 : 100 * plusDM / tr
rawMinus = tr == 0 ? 0.0 : 100 * minusDM / tr
alpha = 2.0 / (len + 1)
var float plusDI = 0.0
var float minusDI = 0.0
var float adx = 0.0
plusDI := nz(rawPlus) * alpha + plusDI * (1 - alpha)
minusDI := nz(rawMinus) * alpha + minusDI * (1 - alpha)
diSum = plusDI + minusDI
dx = diSum == 0 ? 0.0 : 100 * math.abs(plusDI - minusDI) / diSum
adx := dx * alpha + adx * (1 - alpha)
// ---- iADXWilder: Wilder smoothing of DM and TR, which is what ta.dmi does.
[wPlusDI, wMinusDI, wAdx] = ta.dmi(len, len)
plot(adx, "ADX (iADX)", color.rgb(30, 144, 255))
plot(plusDI, "+DI (iADX)", color.green)
plot(minusDI, "-DI (iADX)", color.red)
plot(wAdx, "ADX (iADXWilder)", color.gray)The MQL5 indicator being replaced, with a switch between the two handles:
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrGreen
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
input int InpPeriod = 14; // adx_period
input bool InpWilder = false; // true: iADXWilder, false: iADX
double AdxBuffer[];
double PlusBuffer[];
double MinusBuffer[];
int adxHandle;
int OnInit()
{
SetIndexBuffer(0, AdxBuffer, INDICATOR_DATA);
SetIndexBuffer(1, PlusBuffer, INDICATOR_DATA);
SetIndexBuffer(2, MinusBuffer, INDICATOR_DATA);
adxHandle = InpWilder ? iADXWilder(_Symbol, _Period, InpPeriod)
: iADX(_Symbol, _Period, InpPeriod);
if(adxHandle == INVALID_HANDLE)
return(INIT_FAILED);
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
if(BarsCalculated(adxHandle) < rates_total)
return(0);
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(adxHandle, MAIN_LINE, 0, to_copy, AdxBuffer) <= 0) return(0);
if(CopyBuffer(adxHandle, PLUSDI_LINE, 0, to_copy, PlusBuffer) <= 0) return(0);
if(CopyBuffer(adxHandle, MINUSDI_LINE, 0, to_copy, MinusBuffer) <= 0) return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iADX | MQL5 iADXWilder | Pine ta.dmi |
|---|---|---|---|
| DI smoothing | EMA (2/(n+1)) of per-bar 100·DM/TR | Wilder (1/n) of DM and TR separately, then divide | Same as iADXWilder |
| ADX smoothing | EMA of DX, same period | Wilder of DX, same period | Wilder of DX, separate adxSmoothing argument |
| Seed | Zero on bar 0 | Zero on bar n−1 | SMA of the first n values |
| Warm-up cells | Ramp from 0 starting at bar 1 | 0.0 before bar n | na before the seed |
| Steady state | Faster, noisier DI lines; more crossings | Identical recursion; values converge once the seed effect has decayed | |
| Tie in DM | Both zeroed | Both zeroed (+DM > −DM and −DM > +DM are both false) | |
What the Stratlate converter emits
The MQL5 → Pine engine maps iADX to prelude helpers f_adx, f_plusdi and f_minusdi that reproduce MetaTrader's EMA-based algorithm; because a helper cannot hold var state, they ride on ta.ema with a weighted warm-up so that its seed equals the zero-seeded EMA, and the source comment records a residual gap on the ADX line for roughly the first 2n bars. In the MQL5 handle form the channel is chosen from the CopyBuffer buffer index and reported as an approximation (MQLW0501); in our probe the ADX buffer came out as f_adx(close, close[1], 14), but the +DI and −DI buffers did not come out right (the helpers received a placeholder source and were then overwritten with the ADX line), so check those two buffers by hand. iADXWilder has no mapping at all: the engine logs MQLW0512 "no Pine mapping", emits na for the buffer and reports that the converted script would draw nothing (MQLE0522). Use ta.dmi for it as shown above.
Pitfalls
- Porting
iADXwithta.dmi. The trend-strength threshold (ADX > 25, say) is crossed on different bars and the DI crossovers do not line up with the MT5 chart. - Porting
iADXWilderwith the hand-written EMA version above. That reproducesiADX, the other indicator. - Comparing early bars. MetaTrader ramps from zero and Pine seeds from an average, so the first few hundred bars differ even when the steady-state recursion is identical.
- Writing
plusDM > minusDM ? plusDM : 0without the > 0 test. When both moves are negative, the larger (less negative) one must still be zero. - Reading buffer 1 and 2 in the wrong order: in MQL5 index 1 is
PLUSDI_LINEand index 2 isMINUSDI_LINE; Pine's tuple is[plusDI, minusDI, adx]with ADX last.
FAQ
Which MetaTrader ADX does ta.dmi correspond to?
iADXWilder. Both apply Wilder's 1/n smoothing to directional movement and true range and then to DX. iADX uses an exponential average with 2/(n+1) and is MetaQuotes' own variant.
Why does my converted ADX differ from MT5 for the first bars?
Seeding. MetaTrader starts its recursions at zero, Pine's ta.rma and ta.ema start from an average or the first value. The gap shrinks every bar and is negligible after a few hundred bars.
Can I pass different DI and ADX periods like ta.dmi allows?
Not with iADX or iADXWilder: both use one period for everything. To match them from Pine, pass the same value for diLength and adxSmoothing.
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter