Volatility Regime Canvas

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

canvasstatefunctions and types

volatility-regime-canvas.abx
1indicator "Volatility Regime Canvas"
2pane: new
3
4enum Regime: calm, normal, stormy
5
6input fast_vol = 20
7input slow_vol = 100
8
9state regime = Regime.normal
10
11ratio = stdev(returns(close), fast_vol) / stdev(returns(close), slow_vol)
12
13if ratio < 0.8:
14regime = Regime.calm
15elif ratio > 1.3:
16regime = Regime.stormy
17else:
18regime = Regime.normal
19
20plot ratio as vol_ratio, color: white
21hline 1, style: dotted, color: gray
22
23on render(canvas):
24tint = if regime == Regime.stormy then red.fade(80) else if regime == Regime.calm then green.fade(80) else gray.fade(90)
25canvas.rect(from: (canvas.visible_from, canvas.price_max), to: (canvas.last_bar, canvas.price_min), fill: tint)
26match regime:
27Regime.calm: canvas.text("calm", at: (canvas.last_bar, canvas.price_max), align: right, color: green)
28Regime.normal: canvas.text("normal", at: (canvas.last_bar, canvas.price_max), align: right)
29Regime.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.

Try this in the Terminal. AlgoBars is free: $0 a month, no card needed.

Create my free account