L&S Volatility Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gabasco

//@version=5
indicator(title="L&S Volatility Index", shorttitle="LSVI", format=format.percent, precision=2, timeframe="", timeframe_gaps=true)

// Input
lengthInput = input.int(21, "Length", minval=2, tooltip="How many candles to lookback", group="LSVI Settings")
hviAnnualInput = input.int(252, "Days", minval=1, tooltip="Trading days in 1 year. Used for annualizing historical volatility.", group="LSVI Settings")

// Average Historical Volatility Index
hvi = ta.stdev(math.log(close / close[1]), lengthInput, false) * math.sqrt(hviAnnualInput) * 100
avgHvi = ta.sma(hvi, lengthInput)

// L&S Volatility Index
sma = ta.sma(close, lengthInput)
priceDeviation = math.abs(close - sma) / sma * 100
lsVolatility = priceDeviation / avgHvi * 100

// Drawings
threshold = hline(30, title="Threshold", color=color.new(#787B86, 0), linestyle=hline.style_dashed)
zero = hline(0, title="Zero", color=color.new(#787B86, 0), linestyle=hline.style_dashed, display=display.none)
lsvi = plot(lsVolatility, "LSVI", color.new(#7E57C2, 0))
fill(zero, threshold, color = color.rgb(126, 87, 194, 90), title="Background Fill")