Skip to content
Open RaamTrade

SuperTrend

An ATR band that ratchets one way and flips side when price closes through it. .line is the trailing stop level; .direction is +1 (line below price) or −1 (line above). The ratchet is the whole indicator — without it the band would whipsaw every bar.

Signature

SuperTrend(period, multiplier, on=<source>)
Argument Type Required Notes
period int Param or literal Yes ATR length. Default 10, bounds 1–100.
multiplier float Param or literal Yes Band width in ATRs. Default 3.0, bounds 0.5–10.0.
on= symbol No Source symbol. Defaults to the trade symbol. Reads high, low, close.

Formula

$$ \text{upper} = \tfrac{H+L}{2} + m,\text{ATR}, \quad \text{lower} = \tfrac{H+L}{2} - m,\text{ATR} $$

The bands ratchet (upper can only fall while the previous close is under it; lower can only rise while the previous close is above it). Direction flips to +1 on a close above the final upper band, to −1 on a close below the final lower band; .line is the band on the active side.

Warmup

period bars for the ATR seed (cumulative average, then Wilder smoothing). The first bar returns the lower band with direction +1.

Reading the value

st.line
st.direction
st.direction == 1 and self.trade.close > st.line

Multi-value: cannot be chained onto or crossed.

Example

class SuperTrendFollower(Strategy):
"""Long while SuperTrend is below price; exit on the flip."""
period = Param(10, min=1, max=100)
mult = Param(3.0, min=0.5, max=10.0)
trade = Symbol()
sizing = FixedCapital()
def execute(self):
st = SuperTrend(self.period, self.mult, on=self.trade)
if st.direction == 1 and self.trade.is_flat:
self.buy(self.trade, reason=f"SuperTrend long, line {st.line:.2f}")
if st.direction == -1 and self.trade.is_long:
self.sell(self.trade, self.trade.position, reason="SuperTrend flipped")

Notes

  • Olivier Seban’s construction; ATR uses Wilder smoothing seeded by a cumulative average.
  • Naming: the catalog sketched .value/.direction, but value is a reserved output key in RaamCode, so the level ships as .line.
  • Proven by an assert-via-trade run on SPY daily (line and direction agreed with an independent strategy-side replication, checked with a 3-bar persistence rule to tolerate flip-timing transients; zero alarm trades).
  • Direction starts at +1 by convention; the first flip makes the state meaningful.
  • The line is bit-identical to pandas_ta’s supertrend(10, 3.0) away from trend flips (median difference −2e-13); it flips one or two bars later than the reference because the ATR seed differs.