# Volatility Regime Canvas

> Volatility Regime Canvas is an indicator drawn in its own pane.

Source: https://algobarsx.com/docs/ex-30-volatility-regime-canvas/

```algobarsx
indicator "Volatility Regime Canvas"
    pane: new

enum Regime: calm, normal, stormy

input fast_vol = 20
input slow_vol = 100

state regime = Regime.normal

ratio = stdev(returns(close), fast_vol) / stdev(returns(close), slow_vol)

if ratio < 0.8:
    regime = Regime.calm
elif ratio > 1.3:
    regime = Regime.stormy
else:
    regime = Regime.normal

plot ratio as vol_ratio, color: white
hline 1, style: dotted, color: gray

on 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)
```
What this script says

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.
