Edgoro
Trend indicator

ADX indicator: trend strength, DMI and Pine Script v6 code

How ADX and the +DI/-DI lines measure trend strength and direction, how traders use a 20-25 threshold as a filter, settings, and a free ADX script.

What it measures

The Average Directional Index measures how strongly price is trending, not which way. A rising ADX means the trend, up or down, is getting stronger; a low or falling ADX means the market is moving sideways or the trend is fading.

Direction comes from two companion lines, +DI and −DI, which together form the Directional Movement Index (DMI). Wilder introduced all three in 1978. Traders mostly use ADX as a filter: trend-following signals when ADX is high, range tactics when it is low.

The formula in words

  1. Directional movement. For each bar, the up-move is today's high − yesterday's high, and the down-move is yesterday's low − today's low. If the up-move is larger and positive, it counts as +DM (and −DM is zero); if the down-move is larger and positive, it counts as −DM.
  2. Smooth +DM, −DM and the true range over N bars (14) with Wilder's smoothing.
  3. +DI = 100 × smoothed +DM ÷ smoothed true range. −DI = 100 × smoothed −DM ÷ smoothed true range.
  4. DX = 100 × |+DI − −DI| ÷ (+DI + −DI).
  5. ADX = Wilder-smoothed average of DX over N bars.

Because it is a smoothed average of a smoothed ratio, ADX is slow. It confirms that a trend exists some time after it began.

How traders read it

  • The threshold. A common rule of thumb reads ADX below 20 as no meaningful trend and above 25 as a trending market. These are conventions, not laws; check them on your own market.
  • Direction from the DI lines. +DI above −DI points to upward pressure, −DI above +DI to downward pressure. DI crosses are sometimes used as signals, filtered by ADX being above the threshold.
  • Rising versus falling. ADX turning down from a high level means the trend is losing strength, which is not the same as reversing.
  • As a filter. Pair ADX with a trend-following entry, such as an EMA crossover, and only take signals when ADX is above your threshold, or use it to switch off a range strategy when a trend appears.

Common settings

SettingTypical valueNotes
DI length14Wilder's default.
ADX smoothing14Lower values make ADX react faster.
Threshold20 to 25Higher thresholds keep only the strongest trends.

Pine Script v6 code

The complete, self-contained script is below. Copy it into a new indicator in TradingView's Pine Editor, or download the .pine file.

Pine Script v6
//@version=6
indicator("ADX and DMI", shorttitle = "ADX", format = format.price, precision = 2)

diLen     = input.int(14, "DI length", minval = 1)
adxSmooth = input.int(14, "ADX smoothing", minval = 1)
threshold = input.int(25, "Trend threshold", minval = 1, maxval = 100)
showDi    = input.bool(true, "Show +DI and -DI")

[diPlus, diMinus, adx] = ta.dmi(diLen, adxSmooth)

trending    = adx > threshold
trendStarts = ta.crossover(adx, threshold)
diBullCross = ta.crossover(diPlus, diMinus)
diBearCross = ta.crossunder(diPlus, diMinus)
bullSignal  = diBullCross and trending
bearSignal  = diBearCross and trending

plot(adx, "ADX", color = color.new(color.purple, 0), linewidth = 2)
plot(showDi ? diPlus : na, "+DI", color = color.new(color.teal, 0))
plot(showDi ? diMinus : na, "-DI", color = color.new(color.red, 0))
hline(threshold, "Trend threshold", color = color.gray, linestyle = hline.style_dashed)
bgcolor(trending ? (diPlus > diMinus ? color.new(color.teal, 92) : color.new(color.red, 92)) : na, title = "Trend background")

alertcondition(trendStarts, "ADX above threshold", "ADX crossed above the trend threshold on {{ticker}} ({{interval}})")
alertcondition(bullSignal, "+DI crosses above -DI (trending)", "+DI crossed above -DI with ADX above the threshold on {{ticker}} ({{interval}})")
alertcondition(bearSignal, "-DI crosses above +DI (trending)", "-DI crossed above +DI with ADX above the threshold on {{ticker}} ({{interval}})")

The script uses the built-in ta.dmi(), which returns +DI, −DI and ADX in one call. The pane background is tinted only while ADX is above your threshold, coloured by whichever DI line is on top, so trending stretches stand out at a glance.

Alert conditions cover ADX crossing the threshold and DI crosses that happen while the market is trending.

Add it to your chart

  1. Open any chart on TradingView and open the Pine Editor.
  2. Create a new indicator, delete the template code and paste the script.
  3. Click Save, then Add to chart. Change lengths and levels from the indicator's settings.

Stuck on an error? See how to add a Pine Script indicator to TradingView.

Pitfalls

  • Reading ADX as direction. A high ADX in a falling market is a strong downtrend. Use the DI lines, or price itself, for direction.
  • Expecting early signals. ADX is double-smoothed. It confirms trends late and stays high for a while after they end.
  • Treating 25 as universal. Different markets and timeframes have different typical ADX levels. Look at your symbol's history before fixing a threshold.
  • DI crosses in ranges. Without the ADX filter, +DI and −DI cross often in sideways markets.

The script is an educational example: test it on your own markets before relying on it. Indicators describe the past; they don't predict the future. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Make it yours with Edgoro

Describe the change in plain English and Edgoro writes the Pine Script v6 for you, ready to paste. For example:

“Plot ADX(14) with +DI and -DI, tint the background when ADX is above 25, and alert when +DI crosses above -DI while ADX is rising.”

Build your ADX indicator