Pine Script Consecutive Loss Stop
Halts new entries after N losing trades in a row — the code-level version of walking away from the desk.
//@version=6
strategy("Consecutive Loss Stop", overlay=true, calc_on_every_tick=false)
// ── Inputs ──────────────────────────────────────────────
maxLosses = input.int(3, "Max Consecutive Losses", minval=1)
resetDaily = input.bool(true, "Reset Streak at Session Open")
// ── Loss streak tracking ─────────────────────────────────
var int lossStreak = 0
var bool haltActive = false
if resetDaily and session.isfirstbar_regular
lossStreak := 0
haltActive := false
if strategy.closedtrades > strategy.closedtrades[1]
lastProfit = strategy.closedtrades.profit(strategy.closedtrades - 1)
lossStreak := lastProfit < 0 ? lossStreak + 1 : 0
if lossStreak >= maxLosses
haltActive := true
// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) and not haltActive
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)) and not haltActive
if longCondition
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if shortCondition
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visual
bgcolor(haltActive ? color.new(color.red, 88) : na, title="Loss Halt Active")
Settings
maxLosses— losing trades in a row that trigger the halt. Default 3.resetDaily— reset the streak and halt at each session open. Default on.
How to use
- Paste into the Pine Editor and replace the EMA-crossover placeholder with your own entry conditions.
- With
resetDailyoff, the halt persists across sessions until a winning trade would have reset the streak — stricter, but closer to "stop and review". - Wins reset the streak to zero; scratches (exactly $0) also reset it.
- Pair with the daily kill switch — the loss stop catches streaks, the kill switch catches total dollars.
Frequently Asked Questions
How do I stop trading after 3 losses in Pine Script?
Detect each newly closed trade by comparing strategy.closedtrades to its previous value, read its P&L with strategy.closedtrades.profit(), and increment a streak counter on losses (resetting on wins). When the streak reaches your max, set a halt flag that every entry condition checks.
How do I detect a losing streak in Pine Script?
Each time strategy.closedtrades increases, look up the newest trade with strategy.closedtrades.profit(strategy.closedtrades - 1). Negative means a loss: add one to the streak. Zero or positive resets it. The streak lives in a var int so it survives across bars.
Related
Want a complete eval-ready strategy with tilt protection built in?
This snippet is a building block. Our paid scripts are complete strategies — entries, loss streak halts, kill switch, session filter, and EOD flatten tested on 3 years of MES/NQ data. Instant delivery from $50.
View Plans — From $50