Pine Script Consecutive Loss Stop
Halts new entries after N losing trades in a row — a tilt guardrail written directly into the strategy rather than left to willpower.
Pine Script
//@version=6
strategy("Target Filled - Consecutive Loss Stop Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
maxLosses = input.int(3, "Halt After N Losses", minval=1)
resetDaily = input.bool(true, "Reset Streak Each Session")
// --- Loss-streak tracking -----------------------------------------
var int streak = 0
var bool halted = false
if resetDaily and session.isfirstbar_regular
streak := 0
halted := false
if strategy.closedtrades > strategy.closedtrades[1]
tradeResult = strategy.closedtrades.profit(strategy.closedtrades - 1)
streak := tradeResult < 0 ? streak + 1 : 0
if streak >= maxLosses
halted := true
// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
canLong = ta.crossover(fastMA, slowMA) and not halted
canShort = ta.crossunder(fastMA, slowMA) and not halted
if canLong
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if canShort
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visual: red background while the halt is active
bgcolor(halted ? color.new(color.red, 88) : na, title="Loss Halt Active") Settings
| Input | Default | Purpose |
|---|---|---|
| maxLosses | 3 | Consecutive losing trades that trigger the halt. |
| resetDaily | on | Reset the streak and halt at the start of each session. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
- With
resetDailyswitched off, a halt carries over into the next session until a win would have cleared the streak — stricter, and closer to "stop and go review the trades". - A winning trade resets the streak to zero; a flat trade (exactly $0) also resets it.
- Pair this with the daily kill switch — this one catches losing streaks by count, the kill switch catches total dollars lost.
Frequently Asked Questions
How do I stop trading after 3 losses in a row in Pine Script?
Watch for each newly closed trade by comparing strategy.closedtrades against its value on the previous bar, read that trade's result with strategy.closedtrades.profit(), and bump a streak counter on every loss while resetting it on any win. Once the streak reaches your configured max, set a halt flag and check it in every entry condition.
How do I detect a losing streak in Pine Script?
Each time strategy.closedtrades ticks up, pull the newest trade with strategy.closedtrades.profit(strategy.closedtrades - 1). A negative number is a loss and adds one to the streak; zero or positive resets it back to zero. Storing the streak in a var int keeps the count alive across bars instead of resetting every tick.