// ADX indicator
//
// Input configuration
// - adxLengthInput: 14 days; how many periods are used when calculating the TR, +DM, and -DM
// - adxSmoothingLengthInput: 14 days; how many DX values are smoothed to create the ADX line
//
// Output:
// - RSI solid line
// - Overbought dotted line
// - Oversold dotted line
//@version=5
indicator("Average Directional Index (ADX)", overlay=false, timeframe="D", timeframe_gaps=true)
// Input for ADX period
int adxLengthInput = input.int(14, title="ADX Period", group = "ADX Settings")
int adxSmoothingLengthInput = input.int(14, title="ADX Smoothing Period", group = "ADX Settings")
directionalMovement(simple int length) =>
float up = ta.change(high)
float down = -ta.change(low)
float truerange = ta.rma(ta.tr, length)
float plusDI = fixnan(100 * ta.rma(up > down and up > 0 ? up : 0, length) / truerange)
float minusDI = fixnan(100 * ta.rma(down > up and down > 0 ? down : 0, length) / truerange)
[plusDI, minusDI]
adx(simple int directionLength, simple int adxLength) =>
[plusDI, minusDI] = directionalMovement(directionLength)
float sum = plusDI + minusDI
float adx = 100 * ta.rma(math.abs(plusDI - minusDI) / (sum == 0 ? 1 : sum), adxLength)
[adx, plusDI, minusDI]
[adx, plusDI, minusDI] = adx(adxSmoothingLengthInput, adxLengthInput)
// Plot the ADX line
plot(adx, title="ADX", color=color.blue)
// Plot +DI and -DI lines (for reference)
plot(plusDI, title="+DI", color=color.green)
plot(minusDI, title="-DI", color=color.red)
// Plot levels for better visualization
hline(20, "Low Level", color=color.gray)
hline(40, "High Level", color=color.gray)