ADX on TradingView — Pine Script v5 Source

The built-in ta.dmi and a coloured trend-strength background

ADX in Pine Script
ADX in Pine Script

Pine’s ta.dmi returns the whole directional system — +DI, −DI, and ADX — in one call, so plotting the explainer’s chart is short. We’ll add a threshold and a background that lights up when a trend is present.

What you’ll need

  • A TradingView account (free tier is fine)
  • The Pine Editor

The source

//@version=5
indicator("AlgoGen ADX", shorttitle="AG ADX", overlay=false)

diLen  = input.int(14, "DI length")
adxLen = input.int(14, "ADX smoothing")
level  = input.int(25, "Trend threshold")

[plusDI, minusDI, adxVal] = ta.dmi(diLen, adxLen)

plot(plusDI,  "+DI", color=#0ca30c, linewidth=2)
plot(minusDI, "−DI", color=#d03b3b, linewidth=2)
plot(adxVal,  "ADX", color=#2a78d6, linewidth=2)
hline(level, "Trend", color=color.gray)

// Background: green/red when a trend exists and has a direction.
trending = adxVal > level
bgcolor(trending and plusDI > minusDI  ? color.new(#0ca30c, 90) : na)
bgcolor(trending and minusDI > plusDI  ? color.new(#d03b3b, 90) : na)

ta.dmi(diLen, adxLen) returns [plusDI, minusDI, adx] — note it takes two lengths, one for the DI smoothing and one for the ADX smoothing (usually equal). The bgcolor lines tint the pane green in a strong up-trend and red in a strong down-trend, and stay clear when ADX is below the threshold — an at-a-glance regime read.

Using it as a filter

// Combine with any entry: only go long in a strong up-regime.
longRegime = adxVal > level and plusDI > minusDI
alertcondition(ta.crossover(plusDI, minusDI) and adxVal > level, "ADX bull", "DI cross in a trend")

A complete DI-cross strategy shell

Turn the filter into an actual strategy to test it:

//@version=5
strategy("AlgoGen ADX DI Cross", overlay=true)
[plusDI, minusDI, adxVal] = ta.dmi(14, 14)
trend = adxVal > 25
if trend and ta.crossover(plusDI, minusDI)
    strategy.entry("Long", strategy.long)
if trend and ta.crossunder(plusDI, minusDI)
    strategy.entry("Short", strategy.short)

Flip to the Strategy Tester to see whether gating DI crosses on ADX > 25 beats trading every cross — usually the filter cuts a lot of range-bound whipsaw.

Gotchas

  • Two length arguments. ta.dmi separates DI length from ADX smoothing; passing one number where two are expected is a common slip.
  • It’s laggy. ADX confirms trends late by design; use it to filter, not to time entries to the bar.
  • Tuple order. [plusDI, minusDI, adxVal] — DI lines first, ADX last.

Same indicator elsewhere: Python, MQL5, EasyLanguage, NinjaScript. Then test an ADX-filtered system 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. Pine Script built-ins (TradingView)

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