HomePine Script Snippets › Breakeven Stop

Pine Script Breakeven Stop

Moves your stop to entry once a trade is a set number of ticks in profit, cutting the worst case to roughly a scratch.

//@version=6
strategy("Breakeven Stop", overlay=true, calc_on_every_tick=false)

// ── Inputs ──────────────────────────────────────────────
triggerTicks = input.int(30, "Breakeven Trigger (ticks)", minval=1,
     tooltip="Move stop to entry once price is this many ticks in profit.")
offsetTicks  = input.int(2, "Breakeven Offset (ticks)", minval=0,
     tooltip="Lock in this many ticks beyond entry when the stop moves.")
initialStop  = input.int(40, "Initial Stop (ticks)", minval=1)

// ── State ────────────────────────────────────────────────
var bool beActive = false
if strategy.position_size == 0
    beActive := false

// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
if longCondition and strategy.position_size == 0
    strategy.entry("Long", strategy.long)

// ── Breakeven logic (long side) ──────────────────────────
avg  = strategy.position_avg_price
tick = syminfo.mintick

if strategy.position_size > 0
    if not beActive and close >= avg + triggerTicks * tick
        beActive := true
    stopPrice = beActive ? avg + offsetTicks * tick : avg - initialStop * tick
    strategy.exit("Long Exit", "Long", stop=stopPrice)

// Visual
bgcolor(beActive ? color.new(color.teal, 90) : na, title="Breakeven Active")

Settings

How to use

Frequently Asked Questions

How do I move my stop to breakeven in Pine Script?

Track strategy.position_avg_price, and once close is a set number of ticks beyond it, switch the stop price you pass to strategy.exit from the initial stop to the entry price (plus an optional offset). A persistent var bool records that breakeven is active so the stop never drops back to the initial level.

What is a breakeven stop?

A breakeven stop is a stop-loss moved to your entry price after the trade reaches a profit trigger. From that point the worst case is roughly a scratch — commissions and slippage aside — which matters on prop accounts where every avoidable loss eats trailing drawdown.

Related

Want a complete eval-ready strategy with stop management done?

This snippet is a building block. Our paid scripts are complete strategies — entries, breakeven logic, kill switch, session filter, and EOD flatten pre-wired and tested on 3 years of MES/NQ data. Instant delivery from $50.

View Plans — From $50