//@version=5
indicator("Bollinger Bands with Weighted Volatility", overlay=false)

// Input Variables
length = input.int(title="Length", defval=20, minval=1)
deviations = input.float(title="Deviations", defval=2.0, minval=0.1, step=0.1)
volatility_length = input.int(title="Volatility Length", defval=20, minval=1)

// Calculate Bollinger Bands
source = close
basis = ta.sma(source, length)
dev = deviations * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev

// Calculate Volatility Weighted by Volume
volatility = ta.stdev(change(source), volatility_length)
volatility_weighted = volatility * ta.volume

// Plot Bollinger Bands and Volatility Weighted by Volume
plot(basis, color=color.blue, title="Basis")
fill(upper, lower, color=color.blue, title="Bollinger Bands")
plot(volatility_weighted, color=color.yellow, title="Volatility Weighted by Volume", style=plot.style_columns)

// Set Y-Axis Scale for Volatility Window
volatility_scale = ta.scalediff(high, low) * 2
volatility_scale := volatility_scale == 0? 1 : volatility_scale

// Plot Volatility Window
plot(volatility_weighted, color=color.yellow, title="Volatility Weighted by Volume", style=plot.style_columns, secondary_y=true, scale=scale.inherit, min=0, max=volatility_scale)