Skip to content

Regime switch ​

Switches between mean-reversion and trend rules by volatility (ATR / price).

Kind strategy · Resolution 1h

Uses two playbooks and picks one by volatility: mean reversion in calm markets, trend following in volatile ones.

Regime. On each closed bar, ATR as a percentage of price decides: below Calm if ATR/price below % is calm, above is volatile. Mode can force either regime.

Calm: mean reversion. Buy when the low pierces the lower Bollinger band; exit when the close crosses back above the middle band, or falls 3 % below the lower band.

Volatile: trend. Buy when Supertrend turns up; exit when it turns down.

State. The script remembers which regime opened the position (entryRegime), so a trade entered as a reversion trade is exited by the reversion rules even if the regime changes meanwhile.

Risk. maxDrawdownPct: 30 stops the whole run if equity falls 30 % below its peak.

Try it ​

On the chart, press /, open Strategies and choose Regime switch. Or copy the source below into the AlphaScript editor and click Add to chart.

Inputs ​

InputTypeDefault
Modeselect"auto"
Calm if ATR/price below %float1.5
BB lengthint20
BB multfloat2
Supertrend factorfloat3
Size % of equityfloat50

Source ​

From packages/alphascript/examples/regime_switch.as.js.

js
strategy({ name: "Regime switch", version: "0.1", market: "ALPHA:61", resolution: "1h",
           capital: { tao: 100 }, actOn: "confirmed", risk: { maxPriceImpactPct: 0.5, maxDrawdownPct: 30 } });

const mode     = input.select("Mode", "auto", ["auto", "revert", "trend"]);
const calmPct  = input.float("Calm if ATR/price below %", 1.5, { min: 0.1, max: 10, step: 0.1 });
const bbLen    = input.int("BB length", 20, { min: 5, max: 100 });
const bbMult   = input.float("BB mult", 2, { min: 0.5, max: 4, step: 0.1 });
const stFactor = input.float("Supertrend factor", 3, { min: 1, max: 6, step: 0.5 });
const sizePct  = input.float("Size % of equity", 50, { min: 1, max: 100 });

const atr = ta.atr(14);
const bb  = ta.bbands(candle.close, bbLen, bbMult);
const st  = ta.supertrend(stFactor, 10);
const touchLower = ta.crossunder(candle.low, bb.lower);
const backToMid  = ta.crossover(candle.close, bb.middle);
const trendUp    = ta.crossunder(st.direction, 0);
const trendDown  = ta.crossover(st.direction, 0);

plot(bb.upper, { title: "bb upper", color: "#5c6878" });
plot(bb.lower, { title: "bb lower", color: "#5c6878" });
plot(st.line,  { title: "supertrend", color: "#f2b544" });
plot(atr,      { title: "ATR", color: "#8b98a8", pane: 1 });

let regime = "calm";
let entryRegime = "";

event.onCandle((bar) => {
  const volPct = (atr.value / bar.close) * 100;
  regime = mode === "revert" ? "calm" : mode === "trend" ? "volatile" : volPct < calmPct ? "calm" : "volatile";
  const pos = account.position();
  const size = account.equity() * sizePct / 100;
  if (pos.alpha === 0) {
    if (regime === "calm" && touchLower.value) {
      trade.buy({ tao: size, maxPrice: pool.price() * 1.01, ttlBlocks: 3, reason: "bb lower" }); entryRegime = "calm";
    } else if (regime === "volatile" && trendUp.value) {
      trade.buy({ tao: size, maxPrice: pool.price() * 1.015, ttlBlocks: 4, reason: "trend up" }); entryRegime = "volatile";
    }
    return;
  }
  if (entryRegime === "calm" && (backToMid.value || bar.close < bb.lower.value * 0.97)) trade.closePosition({ reason: "mid / stop" });
  if (entryRegime === "volatile" && trendDown.value) trade.closePosition({ reason: "trend down" });
});

event.onPositionClosed((c) => log(entryRegime, "trade closed:", c.realizedTao.toFixed(4), "τ"));

AlphaScript language v0.1 · runtime 0.2.0