Skip to content

Bollinger breakout ​

Buys a volume-confirmed close above the upper Bollinger band, exits on an ATR trail or the basis.

Kind strategy · Resolution 15m

Buys a breakout above the upper Bollinger band when volume confirms it, then rides it with an ATR trailing stop.

Entry. The close crosses above the upper band on a bar whose alpha volume is at least Volume x avg times its average. The strategy buys 40 % of its equity with a 1 % price limit.

Exit. A trailing level starts ATR stop x ATRs below the entry close and only moves up. The position closes when a bar closes below the trail, or below the middle band, whichever comes first.

State. The trail is a plain top-level variable (let trail), updated in onCandle: the AlphaScript equivalent of Pine Script's var.

What to try. Raise Volume x avg to keep only the strongest breakouts; a tighter ATR stop x locks in profit sooner but exits more often on noise.

Try it ​

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

Inputs ​

InputTypeDefault
BB lengthint20
BB multfloat2
Volume x avgfloat1.5
ATR stop xfloat2.5

Source ​

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

js
strategy({ name: "Bollinger breakout", version: "0.1", market: "ALPHA:61", resolution: "15m",
           capital: { tao: 100 }, actOn: "confirmed", risk: { maxPriceImpactPct: 0.4, maxOrderTao: 40 } });

const len   = input.int("BB length", 20, { min: 5, max: 200 });
const mult  = input.float("BB mult", 2, { min: 0.5, max: 4, step: 0.1 });
const volX  = input.float("Volume x avg", 1.5, { min: 0.5, max: 5, step: 0.1 });
const atrK  = input.float("ATR stop x", 2.5, { min: 0.5, max: 6, step: 0.5 });

const bb     = ta.bbands(candle.close, len, mult);
const volAvg = ta.sma(candle.volume, len);
const atr    = ta.atr(14);
const breakout = ta.crossover(candle.close, bb.upper);

plot(bb.upper,  { title: "upper",  color: "#5c6878" });
plot(bb.middle, { title: "middle", color: "#8b98a8" });
plot(bb.lower,  { title: "lower",  color: "#5c6878" });
plotShape(breakout, { shape: "triangleUp", location: "belowBar", color: "#f2b544", text: "BO" });

let trail = NaN;

event.onCandle((bar) => {
  const pos = account.position();
  if (pos.alpha === 0) {
    if (breakout.value && candle.volume.value > volAvg.value * volX) {
      trade.buy({ tao: account.equity() * 0.4, maxPrice: pool.price() * 1.01, ttlBlocks: 3, reason: "bb-breakout" });
      trail = bar.close - atrK * atr.value;
    }
    return;
  }
  trail = math.max(trail, bar.close - atrK * atr.value);
  if (bar.close < trail) trade.closePosition({ reason: "atr-trail" });
  else if (bar.close < bb.middle.value) trade.closePosition({ reason: "lost-midline" });
});
event.onPositionClosed((c) => log("closed, realized", c.realizedTao.toFixed(4), "τ"));

AlphaScript language v0.1 · runtime 0.2.0