Volatility Regime Canvas
Volatility Regime Canvas is an indicator drawn in its own pane.
canvasstatefunctions and types
indicator "Volatility Regime Canvas"pane: newenum Regime: calm, normal, stormyinput fast_vol = 20input slow_vol = 100state regime = Regime.normalratio = stdev(returns(close), fast_vol) / stdev(returns(close), slow_vol)if ratio < 0.8:regime = Regime.calmelif ratio > 1.3:regime = Regime.stormyelse:regime = Regime.normalplot ratio as vol_ratio, color: whitehline 1, style: dotted, color: grayon render(canvas):tint = if regime == Regime.stormy then red.fade(80) else if regime == Regime.calm then green.fade(80) else gray.fade(90)canvas.rect(from: (canvas.visible_from, canvas.price_max), to: (canvas.last_bar, canvas.price_min), fill: tint)match regime:Regime.calm: canvas.text("calm", at: (canvas.last_bar, canvas.price_max), align: right, color: green)Regime.normal: canvas.text("normal", at: (canvas.last_bar, canvas.price_max), align: right)Regime.stormy: canvas.text("stormy", at: (canvas.last_bar, canvas.price_max), align: right, color: red)Volatility Regime Canvas is an indicator drawn in its own pane.
You can change 2 inputs: fast_vol (default 20) and slow_vol (default 100).
It remembers regime from one bar to the next.
It calculates ratio as the standard deviation of the returns of the close over fast_vol bars divided by the standard deviation of the returns of the close over slow_vol bars.
Whenever the chart is drawn, it sets tint to red at 80% transparency when regime is stormy, otherwise green at 80% transparency when regime is calm, otherwise gray at 90% transparency; it also draws a rectangle; it also checks regime: for calm it writes "calm" on the chart; for normal it writes "normal" on the chart; for stormy it writes "stormy" on the chart.
On each bar, it checks whether ratio is below 0.8 and, if so, sets regime to calm; otherwise, if ratio is above 1.3, sets regime to stormy; otherwise sets regime to normal.
On the chart, it plots ratio as vol_ratio and draws a horizontal line at 1.