Strategy Orders
Entries, exits, brackets, position sizing, cost assumptions and the fill model.
strategy.entry opens or reverses a position. It takes an id, a side of side.long or side.short, an optional when condition and an optional limit or stop price. Calling entry with an existing id while a position is open in the opposite direction reverses it in a single fill rather than stacking, which is usually what a signal flip means.
strategy.exit closes a position by id and is where brackets live. Give it a stop, a target, a trail, or any combination; the engine keeps the bracket alive across bars and cancels the remaining legs when one fills. strategy.close closes an id at market on the current bar, and strategy.close.all flattens everything, which is the standard way to enforce a no-overnight rule.
Sizing is set once in the strategy declaration. size.percent allocates a percentage of current equity, size.fixed a fixed quantity, size.cash a fixed notional, and size.risk sizes from the distance to your stop so every trade risks the same fraction of the account. The last one is the one that makes an equity curve behave, because it stops a wide-stop trade from being twice the position of a tight-stop one.
Cost assumptions belong in the declaration too: commission as bps or a per-contract amount, and slippage in basis points or ticks. Both default to zero, and leaving them at zero is the single most common reason a backtest looks brilliant and trades badly. A high-frequency rule that survives five basis points of round-trip cost is worth studying; one that does not, is not.
The fill model is deliberately conservative. Market orders fill at the next bar open by default rather than at the signal bar close, stops fill at the stop price with slippage applied against you, and limit orders require the bar to trade through the level rather than merely touch it. You can loosen these settings, but every loosening moves the result further from something you could reproduce with real money.
Example
AlgoBeamScript — Ribbon Breakout strategy
//@version=2
strategy("Ribbon Breakout",
overlay = true,
capital = 25000,
sizing = size.percent(15),
commission = bps(5),
slippage = bps(2))
lookback = input.int(40, "Breakout lookback", minval = 5)
atrLen = input.int(14, "ATR length", minval = 2)
riskAtr = input.float(1.8, "Stop distance in ATR", step = 0.1)
rr = input.float(2.4, "Reward to risk", step = 0.1)
window = input.time("09:30-15:45", "Trading window")
series hh = ta.highest(high, lookback)[1]
series ll = ta.lowest(low, lookback)[1]
series atr = ta.atr(atrLen)
series trend = ta.ema(close, 200)
fn stopFor(bool isLong) {
return isLong ? close - riskAtr * atr : close + riskAtr * atr
}
longSignal = close > hh and close > trend and time.within(window)
shortSignal = close < ll and close < trend and time.within(window)
// Bar-persistent state. A var runs its initialiser once, on the first bar,
// and then keeps whatever the previous bar left in it; := is how it is
// reassigned. That is what lets the stop ratchet: it may tighten toward
// price on any bar, but it never gives ground until a fresh signal resets it.
var longStop = stopFor(true)
var shortStop = stopFor(false)
longStop := longSignal ? stopFor(true) : math.max(longStop, stopFor(true))
shortStop := shortSignal ? stopFor(false) : math.min(shortStop, stopFor(false))
strategy.entry("RB Long", side.long, when = longSignal, comment = "breakout")
strategy.exit("RB Long",
stop = longStop,
target = close + riskAtr * atr * rr)
strategy.entry("RB Short", side.short, when = shortSignal, comment = "breakdown")
strategy.exit("RB Short",
stop = shortStop,
target = close - riskAtr * atr * rr)
strategy.close.all(when = time.crossed(window.end), comment = "flat overnight")
plot(hh, title = "Upper band", color = color.bull, style = plot.step)
plot(ll, title = "Lower band", color = color.bear, style = plot.step)
plot(longStop, title = "Long stop", color = color.fg_subtle)Copy it, change one input, and run it again — the numbers are deterministic, so a difference in the output is always a difference you made.