ADX in MetaTrader 5 — MQL5 Source Code

Reading the built-in iADX for a clean directional system

ADX in MetaTrader 5
ADX in MetaTrader 5

The ADX is a multi-stage calculation (see the explainer), and MT5’s built-in iADX implements it correctly, so the pragmatic MQL5 approach is to read the handle rather than re-derive the whole chain by hand. That gives you ADX, +DI and −DI ready for an Expert Advisor.

What you’ll need

  • MetaTrader 5 with MetaEditor

Reading iADX in an EA

//+------------------------------------------------------------------+
//| AlgoGen ADX reader — for use inside an Expert Advisor            |
//+------------------------------------------------------------------+
input int InpPeriod = 14;
input double InpTrendLevel = 25.0;

int adxHandle;

int OnInit()
  {
   adxHandle = iADX(_Symbol, _Period, InpPeriod);
   if(adxHandle == INVALID_HANDLE)
      return(INIT_FAILED);
   return(INIT_SUCCEEDED);
  }

// iADX buffers: 0 = ADX (MAIN), 1 = +DI (PLUSDI), 2 = -DI (MINUSDI)
bool ReadADX(double &adx, double &plusDI, double &minusDI)
  {
   double a[], p[], m[];
   if(CopyBuffer(adxHandle, 0, 0, 2, a) < 2) return(false);
   if(CopyBuffer(adxHandle, 1, 0, 2, p) < 2) return(false);
   if(CopyBuffer(adxHandle, 2, 0, 2, m) < 2) return(false);
   adx = a[1]; plusDI = p[1]; minusDI = m[1];   // [1] = last closed bar
   return(true);
  }

// Example regime filter: only signal a long when a real up-trend exists.
bool StrongUptrend()
  {
   double adx, p, m;
   if(!ReadADX(adx, p, m)) return(false);
   return(adx > InpTrendLevel && p > m);
  }

The handle-and-CopyBuffer pattern is the standard MT5 way to consume an indicator in an EA. Buffer 0 is the ADX itself; buffers 1 and 2 are the +DI/−DI direction lines shown in the output chart.

The math behind the buffers

If you ever need to build it yourself (say, for a non-standard smoothing), the chain is: compute +DM/−DM (only the larger, positive directional move counts), Wilder-smooth them and the ATR, form +DI = 100·smoothed(+DM)/ATR and likewise −DI, then DX = 100·|+DI−−DI|/(+DI+−DI), and finally Wilder-smooth DX into ADX. It’s four Wilder-smoothed series and two ratios — correct but fiddly, which is exactly why iADX is the sensible default.

Gotchas

  • Buffer order. iADX is ADX(0), +DI(1), −DI(2). Reading them in the wrong order silently swaps strength and direction.
  • Use [1], not [0]. Read the last closed bar for stable signals; bar [0] is still forming.
  • MQL4. iADX exists there too, using mode constants (MODE_MAIN, MODE_PLUSDI, MODE_MINUSDI).

Same indicator elsewhere: Python, Pine Script, EasyLanguage, NinjaScript. Then test an ADX filter in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. Technical indicator functions (MetaQuotes)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works