Pine Script Breakeven Stop
Moves the stop to entry once a trade is a configurable number of ticks in profit, cutting the worst case to roughly a scratch.
Pine Script
//@version=6
strategy("Target Filled - Breakeven Stop Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
trigTicks = input.int(30, "Ticks In Profit To Trigger", minval=1,
tooltip="Move the stop to entry once price is this many ticks ahead.")
lockTicks = input.int(2, "Ticks Locked Beyond Entry", minval=0,
tooltip="Lock in this many extra ticks once breakeven triggers.")
initTicks = input.int(40, "Initial Stop (ticks)", minval=1)
// --- Persistent breakeven flag -----------------------------------
var bool armed = false
if strategy.position_size == 0
armed := false
// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
if ta.crossover(fastMA, slowMA) and strategy.position_size == 0
strategy.entry("Long", strategy.long)
// --- Breakeven logic (long side shown; mirror for shorts) --------
entryPx = strategy.position_avg_price
tickSz = syminfo.mintick
if strategy.position_size > 0
if not armed and close >= entryPx + trigTicks * tickSz
armed := true
stopLevel = armed ? entryPx + lockTicks * tickSz : entryPx - initTicks * tickSz
strategy.exit("Long Exit", "Long", stop=stopLevel)
// Visual: teal background while breakeven is locked in
bgcolor(armed ? color.new(color.teal, 90) : na, title="Breakeven Armed") Settings
| Input | Default | Purpose |
|---|---|---|
| trigTicks | 30 | Profit in ticks required before the stop moves to breakeven. |
| lockTicks | 2 | Extra ticks beyond entry locked in once armed. |
| initTicks | 40 | Stop distance in ticks before breakeven triggers. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
- The
armedflag persists across bars, so once breakeven triggers the stop can't fall back to the wider initial level. - Mirror the long block with
strategy.position_size < 0and flipped signs to cover shorts. - Teal background on the chart means breakeven is currently active on the open trade.
Frequently Asked Questions
How do I move my stop to breakeven in Pine Script?
Keep a running reference to strategy.position_avg_price, and once close moves a set number of ticks past it, switch the stop price you hand to strategy.exit from the initial stop over to the entry price (plus an optional cushion). A persistent var bool records that the move has happened so the stop never slides back to the wider initial level once armed.
What is a breakeven stop and why use one?
It is a stop-loss that relocates to your entry price once a trade has moved a set distance in your favor. From that point the worst realistic outcome is close to a scratch trade — commissions and slippage aside — which matters on funded accounts where every avoidable loss eats into the drawdown cushion you have left.