Average Directional Index

Pine Script to help you visualize trend strength, spot trade signals, and filter out volatile markets.

The Average Directional Index (ADX) is a powerful tool for measuring trend strength, not direction.

//@version=6
indicator(title="Advanced ADX Trend Strength", shorttitle="AdvADX", overlay=false, format=format.price, precision=2)

// === User Inputs ===
len         = input.int(14, minval=1, title="DI Length")
lensig      = input.int(14, minval=1, maxval=50, title="ADX Smoothing")
adxThreshold= input.int(25, minval=1, maxval=100, title="ADX Threshold for Signals")
showSignals = input.bool(true, title="Show Buy/Sell Signals")

// === ADX & DI Calculation ===
[diplus, diminus, adx] = ta.dmi(len, lensig)

// === Plotting ===
plot(adx, color=color.red, title="ADX", linewidth=2)
plot(diplus, color=color.blue, title="+DI", linewidth=1)
plot(diminus, color=color.orange, title="-DI", linewidth=1)
hline(adxThreshold, "Trend Threshold", color=color.gray, linestyle=hline.style_dotted)

// === Signal Logic ===
bullCross = ta.crossover(diplus, diminus) and adx > adxThreshold
bearCross = ta.crossover(diminus, diplus) and adx > adxThreshold

// === Signal Plotting ===
plotshape(showSignals and bullCross, style=shape.triangleup, location=location.bottom, color=color.green, size=size.small, title="Buy Signal", text="BUY")
plotshape(showSignals and bearCross, style=shape.triangledown, location=location.top, color=color.red, size=size.small, title="Sell Signal", text="SELL")

Script Summary

1

DI Length and ADX Smoothing control indicator sensitivity

2

ADX Threshold filters out weak and volatile markets

3

Buy/Sell Signals are plotted only when the trend is strong and DI crossovers occur

Key Parameters Explained

1

DI Length (len)

The lookback period for calculating the Directional Indicators (+DI and -DI). A typical default is 14, but you can adjust this for sensitivity.

2

ADX Smoothing (lensig)

The smoothing factor for the ADX line itself. Higher values make the ADX less sensitive to short-term fluctuations.

3

IADX Threshold (adxthreshold):

The minimum ADX value to consider a trend “strong.” Commonly set at 20 or 25. Only signals above this level are considered valid.

4

Show Signals (showSignals)

Toggle to display buy/sell signals based on DI crossovers and ADX strength.

Optional Enhancements

1

Combine with RSI or Moving Averages:

Use ADX as a trend filter and RSI for overbought/oversold signals, or confirm trend direction with moving averages

2

Alerts:

Add alertcondition() for automated notifications when signals trigger.

3

Background Highlighting

Use bgcolor() to visually highlight strong trend periods.

4

Strategy Version:

Convert to a strategy script to backtest ADX-based entries and exits.

Last updated

Was this helpful?