Donchian Volatility Indicator - Adaptive Channel Width
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("Donchian Volatility Indicator - Adaptive Channel Width", overlay=false)

// Calculation Inputs
length = input.int(50, "Lookback Period for Donchian Channel")
atrPeriod = input.int(14, "ATR Period")
atrMultiplier = input.float(2.0, "ATR Multiplier")
lookbackPeriod = input.int(250, "Lookback Period for Extremes")
signalPeriod = input.int(50, "Length of Signal Line")

// Calculate ATR
atr = ta.atr(length = atrPeriod)

// Calculate Upper and Lower Channels
upperChannel = ta.highest(high, length) + atr * atrMultiplier
lowerChannel = ta.lowest(low, length) - atr * atrMultiplier

// Calculate Channel Width
channelWidth = upperChannel - lowerChannel

// Signal Line
signalLine = ta.sma(channelWidth, signalPeriod)

// Plotting
plot(channelWidth, color=color.maroon, linewidth=4, title="Channel Width")
plot(signalLine, color=color.aqua, linewidth=4, title="Signal Line")

// Bar Color
barC = channelWidth > signalLine ? color.lime : color.fuchsia
barcolor(barC)

// Background Color
bgcolor(barC, transp=80)
