Average Directional Index Pine Script — Complete TradingView Guide
The Average Directional Index (ADX) is a momentum oscillator built into Pine Script v6 that measures trend strength on a scale of 0 to 100, regardless of whether price is moving up or down. Developed by J. Welles Wilder Jr. in 1978, the ADX is the industry-standard filter for distinguishing strong trending markets from sideways, choppy conditions. An ADX reading above 25 confirms that a tradeable trend is in place; readings below 20 signal a ranging market where trend-following strategies are unreliable. This complete guide covers the full Pine Script v6 implementation using ta.rma() and Directional Movement calculations, every configurable parameter with recommended ranges, three concrete ADX trading strategies including ADX + EMA crossover filtering, plus a five-step guide to generating ADX scripts instantly with Pineify.
What Is the Average Directional Index?
The Average Directional Index (ADX) is a trend-strength oscillator that quantifies how strong the current trend is on a scale of 0 to 100, used to filter trend-following signals and avoid false entries in sideways markets. Unlike indicators such as RSI or Stochastic that measure price momentum relative to highs and lows, the ADX is directionally neutral — a rising ADX value means the trend (up or down) is strengthening, not that price is necessarily rising.
Developed by J. Welles Wilder Jr. in 1978 and first published in his landmark book New Concepts in Technical Trading Systems, the ADX was originally designed for commodity futures markets but is now widely applied across stocks, crypto, forex, and futures. Wilder introduced the ADX as the third component of the Directional Movement System, alongside the Positive Directional Indicator (+DI) and Negative Directional Indicator (-DI). The ADX line is derived by smoothing the ratio of the difference between +DI and -DI relative to their sum.
Core formula: ADX = 100 × RMA(|+DI − -DI| ÷ (+DI + -DI), adxLen), where +DI = 100 × RMA(+DM, dilen) ÷ RMA(TR, dilen) and -DI = 100 × RMA(-DM, dilen) ÷ RMA(TR, dilen). Here, +DM is the positive directional movement (upward price change when larger than downward), -DM is the negative directional movement, TR is the True Range, RMA is Wilder's smoothing method (equivalent to an EMA with alpha = 1/n), and dilen is the DI period (default 14 bars). In Pine Script v6, this calculation is implemented with ta.rma() and ta.tr.
The ADX applies equally well across all liquid asset classes: stocks, crypto, forex, and futures. Best timeframes are 1H charts and above — the ADX on daily and 4H charts provides the most reliable trend-strength readings. On sub-hourly charts (5m, 15m), the ADX generates more noise and should be used with wider threshold levels (e.g., use 30 instead of 25 as the trend-confirmation threshold).
Best Markets
Stocks · Crypto · Forex · Futures
Best Timeframes
1H, 4H, Daily, Weekly
Overlay
No — separate oscillator panel
ADX Pine Script Code Example
The code below calculates and plots the ADX indicator in a separate panel below the price chart using Pine Script v6. The implementation uses Wilder's Directional Movement formulas with the standard 14-bar period for both the DI calculation and ADX smoothing. To add it to TradingView, open the Pine Script editor with Alt+P, paste the code, and click Add to chart — the ADX line will appear in a new panel where values above 25 indicate a strong trend.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//@version=6
indicator(title="Average Directional Index", overlay=false, max_labels_count=500)
// Directional Movement helper — calculates +DI and -DI
p_ta_dirmov(len) =>
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
[plus, minus]
// ADX — measures trend strength (not direction)
p_ta_adx(simple int dilen, simple int adxlen) =>
[plus, minus] = p_ta_dirmov(dilen)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
// Calculate ADX with default periods: DI length = 14, ADX smoothing = 14
p_ind_1 = p_ta_adx(14, 14) // ADX
// Plot the ADX line — value above 25 signals a strong trend
plot(p_ind_1, "ADX", color.rgb(255, 82, 82, 0), 1)Chart Preview

ADX Parameters
| Parameter | Default Value | Description | Recommended Range |
|---|---|---|---|
| dilen | 14 | Period for calculating the Positive (+DI) and Negative (-DI) Directional Indicators. Controls how many bars are used in the True Range and Directional Movement smoothing via ta.rma(). | 7–20 |
| adxlen | 14 | Smoothing period applied to the DX value (the normalized directional difference) to produce the final ADX line. Larger values produce a smoother, more lagged ADX; smaller values react faster with more noise. | 7–20 |
| source (high/low) | high, low | ADX uses bar highs and lows (not close prices) to calculate directional movement. The True Range also incorporates the previous close. These are hardcoded in the standard ADX formula and are not typically user-adjustable. | Fixed (high/low/close) |
Tuning Guide by Trading Style
- Scalping (5m–15m charts): dilen 7, adxlen 7 — faster but noisier; use threshold of 30 instead of 25
- Day trading (15m–1H charts): dilen 10, adxlen 10 — slightly faster than default; use 25 threshold
- Swing trading (4H–Daily charts): dilen 14, adxlen 14 (Wilder default) — the most tested and reliable setting
- Position/Macro (Daily–Weekly charts): dilen 20, adxlen 20 — smoother signal; use threshold of 20 for trend confirmation
ADX Trading Strategies
The ADX is most powerful as a trend-strength filter — it does not generate buy/sell signals on its own but dramatically improves the reliability of other indicators by ensuring signals only fire when the market is actually trending. Below are three concrete strategies with specific entry and exit conditions.
Strategy 1 — ADX Trend Filter + EMA Crossover
Market environment: trending markets only · Best timeframe: 1H, 4H, Daily
Pairing ADX (14,14) with an EMA 20/50 crossover eliminates false crossover signals in sideways markets. Only act on EMA crossovers when ADX is above 25, confirming a trend is in place. This combination is highly effective on 4H and Daily charts for stocks and crypto.
- Calculate trend filter:
adxValue = p_ta_adx(14, 14) - Calculate fast and slow EMAs:
ema20 = ta.ema(close, 20)andema50 = ta.ema(close, 50) - Long entry: ADX > 25 AND
ta.crossover(ema20, ema50) - Short entry: ADX > 25 AND
ta.crossunder(ema20, ema50) - Exit: ADX drops below 20 (trend weakening) OR opposing EMA crossover occurs
Strategy 2 — ADX Breakout Confirmation
Market environment: breakouts from consolidation · Best timeframe: Daily, 4H
When price breaks out of a range, ADX rising from below 20 to above 25 confirms the breakout has genuine trend momentum. This prevents chasing false breakouts that immediately reverse back into the range. Combine with a Bollinger Band squeeze or Donchian Channel breakout for maximum precision.
- Calculate ADX:
adxValue = p_ta_adx(14, 14) - Identify breakout: price closes above a defined resistance level or the upper Bollinger Band
- Condition (trend confirmation): ADX was below 20 in the prior 5–10 bars (consolidation), then rises above 25 on the breakout bar
- Long entry: breakout candle closes AND ADX > 25 — enter on next bar open
- Stop loss: below the breakout candle low; hold as long as ADX continues rising above 25
Strategy 3 — ADX + RSI Momentum Entry
Market environment: strong uptrends with pullbacks · Best timeframe: 4H, Daily
Combine ADX (14,14) with RSI (14) to enter pullbacks within a confirmed trend. ADX above 25 confirms the trend is strong; RSI dipping to the 40–50 zone (rather than oversold 30) identifies a healthy pullback in an uptrend rather than a trend reversal. This approach is widely used in momentum trading for stocks and crypto.
- Calculate trend strength:
adxValue = p_ta_adx(14, 14) - Calculate momentum:
rsiValue = ta.rsi(close, 14) - Trend condition: ADX > 25 AND close above SMA 50 (macro uptrend confirmed)
- Long entry: RSI pulls back to 40–50 range then crosses back above 50 — enter on the RSI recovery cross above 50
- Exit: ADX drops below 20 OR RSI closes below 40 on two consecutive bars
Disclaimer: The strategies above are for educational purposes only and do not constitute investment advice. Past performance does not guarantee future results. Always apply proper risk management and position sizing.
How to Generate the ADX Indicator in Pineify
- 1
Open Pineify
Go to pineify.app and sign in — a free account is sufficient to generate ADX indicators with full Pine Script v6 output.
- 2
Click "New Indicator"
Select "Indicator" as the script type from the creation menu on the dashboard to start the AI Coding Agent.
- 3
Describe the ADX configuration you want
Type a prompt such as: "Create an ADX indicator with period 14, plot the ADX line in a separate panel, and add a horizontal line at 25 to mark the trend-strength threshold." Pineify's AI generates the complete Pine Script v6 code in seconds.
- 4
Copy to TradingView
Click "Copy to TradingView" to copy the generated code, then open the TradingView Pine Script editor (Alt+P), paste the code, and click "Add to chart". The ADX panel appears automatically below the price chart.
- 5
Adjust the period and thresholds
In the TradingView indicator settings panel, change the DI length and ADX smoothing (default 14 each) to match your timeframe — use 7 for scalping or 20 for position trading. No code editing required.
Frequently Asked Questions
Related Pine Script Indicators
Build Your ADX Indicator in Seconds
Skip the manual coding. Pineify's AI Coding Agent generates complete, ready-to-use Pine Script indicators — including ADX with EMA crossover filtering and RSI confirmation — instantly for free.
Try Pineify Free