Normalized Volume Rate of Change
// 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("Normalized Volume Rate of Change", overlay=false)

// Calculation Inputs
length = input.int(14, "VROC Length")
maLength = input.int(9, "Moving Average Length")

// Calculate Volume Rate of Change (VROC)
vroc = ta.change(volume, length) / ta.sma(volume, length) * 100

// Normalize VROC to -1 to +1 range
vrocNormalized = (vroc - ta.lowest(vroc, length)) / (ta.highest(vroc, length) - ta.lowest(vroc, length)) * 2 - 1

// Calculate Moving Averages
ma1 = ta.sma(vrocNormalized, maLength)
ma2 = ta.sma(vrocNormalized, maLength * 2)

// Moving Average Crossover
crossoverUp = ta.crossover(ma1, ma2)
crossoverDown = ta.crossunder(ma1, ma2)

// Bar and Background Colors
bColor = vrocNormalized > ma1 and vrocNormalized < ma2 ? color.yellow : vrocNormalized > ma1 or vrocNormalized > ma2 ? color.lime : color.fuchsia
barcolor(bColor)
bgcolor(bColor, transp=90)

// Plotting
plot(vrocNormalized, color=bColor, style=plot.style_columns, linewidth=2, title="Normalized VROC")
plot(ma1, color=color.aqua, linewidth=4, title="MA1")
plot(ma2, color=color.orange, linewidth=4, title="MA2")

// Arrows on Moving Average Crossover
plotshape(crossoverUp, "Moving Average Crossover Upwards", style = shape.arrowup, location = location.bottom, color=color.lime, size=size.normal)
plotshape(crossoverDown, "Moving Average Crossover Downwards", style = shape.arrowdown, location = location.top, color=color.fuchsia, size=size.normal)

// Hline Boundaries
hline(0.75, "Upper Boundary", color=color.new(color=color.gray, transp=50), linewidth=1, linestyle=hline.style_dashed)
hline(-0.75, "Lower Boundary", color=color.new(color=color.gray, transp=50), linewidth=1, linestyle=hline.style_dashed)
hline(0.0, "Zero Line", color=color.new(color=color.gray, transp=50), linewidth=1, linestyle=hline.style_dashed)

