Volume-Weighted RSI with Adaptive Smoothing
// 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("Volume-Weighted RSI with Adaptive Smoothing", overlay=false)

length = input.int(14, minval=1, title="Length")

// Calculate RSI
rsi_source = ta.change(close)
up = ta.rma(math.max(rsi_source, 0), length)
down = ta.rma(-math.min(rsi_source, 0), length)
rsi = 100 - (100 / (1 + up / down))

// Calculate Volume-Weighted RSI
vwap = ta.sma(close * volume, length) / ta.sma(volume, length)
vwap_source = ta.change(vwap)
vwap_up = ta.rma(math.max(vwap_source, 0), length)
vwap_down = ta.rma(-math.min(vwap_source, 0), length)
vwap_rsi = 100 - (100 / (1 + vwap_up / vwap_down))

// Adaptive Smoothing
adaptive_smoothing = input.bool(true, title="Adaptive Smoothing")
smoothing_length = input.int(5, minval=1, title="Smoothing Length")

smooth_vwap_rsi = adaptive_smoothing ? ta.sma(vwap_rsi, smoothing_length) : vwap_rsi

// Coloration
rsiColor = rsi >= smooth_vwap_rsi ? color.lime : color.fuchsia
backColor = smooth_vwap_rsi >= 70 ? color.new(color.lime, 80) : smooth_vwap_rsi <= 30 ? color.new(color.fuchsia, 80) : color.new(color.yellow, 80)
barC = smooth_vwap_rsi >= 70 ? color.lime : smooth_vwap_rsi <= 30 ? color.fuchsia : color.yellow

// Plotting
plot(rsi, color=rsiColor, linewidth=4, title="RSI")
plot(smooth_vwap_rsi, color=color.maroon, linewidth=1, style=plot.style_histogram, title="Volume-Weighted RSI Histogram")
plot(smooth_vwap_rsi, color=color.maroon, linewidth=1, title='Volume-Weighted RSI Line')
bgcolor(backColor)
barcolor(barC)
hline(70, "Overbought", color=color.red)
hline(30, "Oversold", color=color.green)
hline(50)
