Skip to content

Supertrend follow ​

Long while Supertrend points up, flat while it points down.

Kind strategy · Resolution 1h

Follows Supertrend's direction: long while it points up, flat while it points down.

Entry and exit. ta.supertrend returns a line and a direction (−1 up, 1 down). The strategy turns the flips into signals with ta.crossunder(st.direction, 0) (turned up) and ta.crossover(st.direction, 0) (turned down): buy on the first, close on the second.

What to try. A higher Factor makes the trend slower to flip: fewer trades, later exits.

Try it ​

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

Inputs ​

InputTypeDefault
Factorfloat3
ATR lengthint10
Size % equityfloat50

Source ​

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

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

const factor = input.float("Factor", 3, { min: 1, max: 6, step: 0.5 });
const atrLen = input.int("ATR length", 10, { min: 3, max: 50 });
const sizePct = input.float("Size % equity", 50, { min: 1, max: 100 });

const st = ta.supertrend(factor, atrLen);
const flipUp   = ta.crossunder(st.direction, 0);
const flipDown = ta.crossover(st.direction, 0);

plot(st.line, { title: "supertrend", color: "#f2b544", width: 2 });
plotShape(flipUp,   { shape: "arrowUp",   location: "belowBar", color: "#2bb889" });
plotShape(flipDown, { shape: "arrowDown", location: "aboveBar", color: "#e35c70" });

event.onCandle(() => {
  const pos = account.position();
  if (flipUp.value && pos.alpha === 0)
    trade.buy({ tao: account.equity() * sizePct / 100, maxPrice: pool.price() * 1.015, ttlBlocks: 4, reason: "trend-up" });
  if (flipDown.value && pos.alpha > 0)
    trade.closePosition({ reason: "trend-down" });
});

AlphaScript language v0.1 · runtime 0.2.0