ADX & DMI in Pine Script
Two directional lines that show whether up moves or down moves dominate, and the ADX, which measures how one-sided the market is without saying which way, with 25 as the usual trend threshold.
Definition
The directional movement index (DMI) is a set of 3 lines in a pane. +DI measures the share of recent range made up of upward expansion, −DI the share made up of downward expansion, and the average directional index (ADX) measures the gap between the two, smoothed, on a scale of 0 to 100. ADX rises whether the trend is up or down: it is a strength reading with no direction, and the DI lines supply the direction. It belongs to the trend strength family and is the standard answer to the question of whether a market is trending at all.
The system works because a trend leaves a footprint in the bars: in an uptrend the highs expand more than the lows, and in a downtrend the reverse. Directional movement counts only that expansion, ignores inside bars, and divides by true range so the reading is a proportion rather than a price. When one side dominates for long enough, the smoothed gap between the DI lines grows and ADX climbs. When the sides alternate, the gap shrinks and ADX falls towards the floor, which is the signature of a range.
J. Welles Wilder published the directional movement system in 1978 alongside RSI, ATR and Parabolic SAR, with a 14 period length and his own smoothing, which weights each new value at 1/14. The conventions are that ADX above 25 marks a trend and below 20 a range. The library's implementation plots ADX in amber when it is above the threshold and grey below, +DI in green and −DI in red, with a dashed line at the threshold, and marks Long when +DI crosses above −DI with ADX over the threshold and Short on the opposite cross.
On the chart ADX moves slowly. It climbs during a trend, often peaks around the strongest stretch of the move and turns down before price does, because a falling ADX means the trend is losing one-sidedness rather than reversing. The DI lines cross often in ranges and separate widely in trends; a DI cross with ADX below 20 is usually noise. Because of the double smoothing, ADX confirms a trend well after it has started, which makes it a filter rather than a trigger.
Reading the chart
ADX answers strength and the DI lines answer direction, so reading the pane is a two-step question asked in that order.
- Read ADX first. Above 25 the market is trending on this length and trend tools apply; below 20 it is ranging and mean-reversion tools apply; between is transition.
- Read the slope of ADX. A rising ADX means the trend is strengthening even if the value is still low; a falling ADX from a high means the trend is losing one-sidedness, which often precedes a range.
- Read the DI lines for direction. +DI above −DI means up moves dominate; −DI above +DI means down moves dominate. The gap between them is the trend's decisiveness.
- Treat a DI cross with ADX above the threshold as the trigger. The library's script marks Long and Short on those crosses only, on confirmed bars, and ignores crosses in a range.
- Note extreme ADX values. Readings above 50 are rare and mark a mature, one-sided move where the risk is a sharp counter-move, not a continuation entry.
- Compare with the higher timeframe. A daily ADX above 25 with +DI on top gives the intraday chart its bias; an intraday DI cross against it is a bounce at best.
The maths
Directional movement counts the expansion of the range on each side, smooths it with Wilder's method against true range, and the ADX is the smoothed gap between the two.
Only the larger of the two movements counts on a bar, and an inside bar counts for neither side, so a day that expands both ways contributes to one line only.
Wilder's smoothing responds more slowly than an EMA of the same length; the double smoothing (DI then ADX) is why ADX lags a trend by several bars.
The library's script uses one length for both the DI lines and the ADX; some platforms expose them separately, with the same defaults.
The marks require both a DI cross and ADX above the threshold on the same confirmed bar; a cross that happens while ADX is low and a later rise of ADX do not together produce a mark.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Length | 14 | A shorter length makes ADX and the DI lines react sooner and cross more often; a longer one gives a smoother strength reading that confirms trends later. |
| ADX threshold | 25 (20 for a looser filter) | Raising it demands a stronger trend before DI crosses count, which cuts trades and whipsaw; lowering it lets weaker trends qualify. |
In practice
- As a regime filter: the filter allows trend-following entries only while ADX is above 25 and mean-reversion entries only while it is below 20. This is the main use and the one Wilder intended.
- As a directional trigger: the library's script marks Long on +DI crossing above −DI with ADX over the threshold and Short on the reverse, which is a complete, if slow, trend system.
- As a filter for a crossover: the filter keeps a 9/21 EMA cross only when ADX is rising or above the threshold, which removes most of the range crosses that make crossovers lose.
- As an exhaustion warning: an ADX that turns down from above 40 while price still pushes on says the move is losing one-sidedness, and it is where exit rules scale out rather than add.
- As a breakout confirmation: a breakout from a range with ADX rising from below 20 is the start of a trend; a breakout with ADX still falling is more likely to fail.
- As a market ranking: compare ADX across a watchlist to find the instruments trending hardest on the day, and to leave the ranging ones to mean-reversion tools.
Where it goes wrong
- Reading ADX as direction. A high ADX in a downtrend is as high as in an uptrend; check which DI line is on top before deciding the side.
- Trading DI crosses in a range. Below 20 the lines cross constantly and each cross reverses the last; the library's script ignores them for that reason.
- Expecting ADX to time the turn. It lags by design, so a falling ADX says the trend is weakening, not that it has reversed; use price or a faster tool for the exit.
- Treating 25 as a law. On low timeframes and calm instruments ADX rarely reaches 25, so calibrate the threshold to the instrument's own history.
Compared with
Moving Average Slope. The slope filter reads the drift of one average, in ATR units, and carries its sign. ADX reads the balance of up and down range expansion with no direction and needs the DI lines for the side; it is less tied to a single length.
Average True Range (ATR). ATR measures how much price moves per bar, in price units, with no view on direction or persistence. ADX measures how one-sided those moves are; a market can have a high ATR and a low ADX, which is a volatile range.
SuperTrend. SuperTrend gives a direction at every bar and no strength; it flips in ranges as readily as in trends. ADX gives strength and, through the DI lines, direction, and it is the filter that keeps SuperTrend out of ranges.
MACD. MACD is a momentum reading built from the gap between 2 EMAs of price, with direction and a zero line. ADX is built from range expansion rather than price and has no zero line; it can rise while MACD falls, in a strong downtrend.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
What does an ADX value of 25 mean?
That the smoothed gap between the DI lines is a quarter of their sum, which on a 14 period length marks a market where one side has dominated for a few weeks of bars. It is the conventional line between a range and a trend. Below 20 is a range; between 20 and 25 is a transition; above 40 is a strong, mature trend.
Does ADX tell me the direction of the trend?
No. ADX rises in an uptrend and in a downtrend alike, because it measures how one-sided the bars are, not which side. The DI lines carry the direction: +DI above −DI means up moves dominate, −DI above +DI means down moves dominate. Read ADX for strength and the DI lines for side.
Why does ADX fall while price keeps rising?
Because the trend is becoming less one-sided, not because it has reversed. Later in a move the pullbacks grow and the up days no longer dominate the ranges as much, so the gap between the DI lines narrows and ADX turns down. It is an early warning of a range or a top, and a poor reason to short on its own.
What are the best ADX settings?
Wilder's 14 period length is the standard on every timeframe and there is little reason to change it. The threshold is the knob: 25 is the usual trend line, 20 a looser one for calm instruments and higher timeframes, 30 a strict one for choppy markets. Calibrate it to the instrument's own ADX history.
Is the DI cross a good entry trigger?
Only with the ADX filter. A +DI cross above −DI with ADX above 25 tends to land mid-trend and is the cross the library's script marks. Without the filter the lines cross every few bars in a range and the cross means little. Even with it, the entry is late, so many traders use the cross to permit trades and something faster to time them.
Does ADX repaint?
No. Directional movement, true range and the smoothing all use closed bars, and the library's script marks a cross only on a confirmed bar. The current bar's values move while the bar forms, which is why a DI cross seen mid-bar is not a signal until the close.