What is the Average Directional Index (ADX)?
The Average Directional Index (ADX) is a technical indicator used to measure the strength of a trend. It doesn’t indicate trend direction, only its intensity. Typically, an ADX above 25 suggests a strong trend, while a value below 20 indicates a weak or absent trend.
Why use Pine Script v5 for ADX implementation?
Pine Script v5 offers enhanced features and syntax improvements over previous versions, making it more efficient and readable for developing custom indicators like the ADX. Its strong typing and improved security features are crucial for reliable trading strategies.
Key Concepts: +DI, -DI, and True Range
The ADX calculation relies on three main components:
- +DI (Positive Directional Indicator): Measures the strength of the uptrend.
- -DI (Negative Directional Indicator): Measures the strength of the downtrend.
- True Range (TR): Measures the market volatility.
Implementing ADX in Pine Script v5: Step-by-Step
Calculating True Range (TR)
The True Range is the greatest of the following:
- Current high minus the current low.
- Absolute value of the current high minus the previous close.
- Absolute value of the current low minus the previous close.
In Pine Script v5, this can be implemented as:
tr = math.max(high - low, math.abs(high - close[1]), math.abs(low - close[1]))
Calculating +DI and -DI
First, we need to calculate the UpMove and DownMove:
upMove = high - high[1]
downMove = low[1] - low
Then, derive the +DI and -DI values:
diP = (upMove > downMove and upMove > 0) ? upMove : 0
diN = (downMove > upMove and downMove > 0) ? downMove : 0
Smoothing the DI values
Smoothing is typically done using a moving average. Here, we use the Wilder’s Smoothing Method, which is an Exponential Moving Average (EMA) with a specific alpha calculation:
dip_smooth = ta.rma(diP, length)
din_smooth = ta.rma(diN, length)
Where length is the period for the moving average.
Calculating the ADX
The ADX is calculated as follows:
- Calculate the Directional Movement Index (DX):
DX = 100 * math.abs((dip_smooth - din_smooth) / (dip_smooth + din_smooth)) - Smooth the DX to get the ADX:
adx = ta.rma(DX, length)
Pine Script Code Example: Complete ADX Implementation
Full Pine Script v5 code for ADX
//@version=5
indicator(title="ADX", shorttitle="ADX", overlay=false)
length = input.int(14, title="ADX Length")
smoothing = input.string("RMA", title="Smoothing Method", options=["RMA", "SMA", "EMA", "WMA"])
// True Range
tr = math.max(high - low, math.abs(high - close[1]), math.abs(low - close[1]))
// Directional Movement
upMove = high - high[1]
downMove = low[1] - low
diP = (upMove > downMove and upMove > 0) ? upMove : 0
diN = (downMove > upMove and downMove > 0) ? downMove : 0
// Smoothing
dip_smooth = ta.rma(diP, length)
din_smooth = ta.rma(diN, length)
// Directional Index
dx = 100 * math.abs((dip_smooth - din_smooth) / (dip_smooth + din_smooth))
adx = ta.rma(dx, length)
plot(adx, color=color.blue, title="ADX")
hline(25, "Threshold", color=color.gray)
Code Explanation: Variables and Functions
indicator(title="ADX", shorttitle="ADX", overlay=false): Defines the script as an indicator and sets its title.length = input.int(14, title="ADX Length"): Creates an input option for the ADX length (default 14).ta.rma(source, length): Calculates the Wilder’s Smoothing Method (RMA) of the source.plot(adx, color=color.blue, title="ADX"): Plots the ADX value on the chart.hline(25, "Threshold", color=color.gray): Draws a horizontal line at 25, often used as a threshold for trend strength.
Adding Input Options: Length and Smoothing
The code includes an input option for the ADX length, allowing users to customize the indicator’s sensitivity. A smoothing option is included, allowing selection between different moving average types.
smoothing = input.string("RMA", title="Smoothing Method", options=["RMA", "SMA", "EMA", "WMA"])
//Smoothing calculation
getSmoothing(source, length) =>
switch smoothing
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"WMA" => ta.wma(source, length)
=> ta.rma(source, length) //RMA is the default
dip_smooth = getSmoothing(diP, length)
din_smooth = getSmoothing(diN, length)
adx = getSmoothing(dx, length)
Using the ADX Indicator in TradingView
Adding the script to your TradingView chart
- Open TradingView and navigate to the chart of the instrument you want to analyze.
- Open the Pine Editor at the bottom of the screen.
- Paste the Pine Script code into the editor.
- Click “Add to Chart” to apply the indicator.
Interpreting ADX values: Trend Strength
- ADX above 25: Strong trend.
- ADX below 20: Weak or no trend.
- Rising ADX: Trend is strengthening.
- Falling ADX: Trend is weakening.
ADX in combination with other indicators
The ADX is best used in conjunction with other indicators to determine both trend strength and direction. Common combinations include:
- Moving Averages: Use moving averages to identify the trend direction and ADX to confirm its strength.
- RSI/Stochastic: Combine with oscillators to identify overbought/oversold conditions within a strong trend.
Advanced ADX Strategies and Customization
ADX Crossovers and Divergences
- +DI/-DI Crossovers: A +DI crossing above -DI suggests a potential uptrend, while the opposite indicates a potential downtrend. Confirm with ADX above 25.
- ADX Divergence: When price makes new highs but ADX fails to make new highs, it may signal a weakening trend.
Customizing the ADX with alerts
You can add alerts to your ADX script to notify you of specific conditions, such as:
- ADX crossing above 25.
- +DI/-DI crossover.
Example:
if ta.crossover(adx, 25)
alert("ADX crossing above 25", alert.freq_once_per_bar_close)
Optimizing ADX parameters for different markets
The optimal ADX length can vary depending on the market and timeframe. Experiment with different lengths to find the settings that work best for your trading style and the specific instruments you trade. Backtesting different ADX lengths can greatly aid in parameter optimization.