How to Build a Step-Based Trailing Stop That Moves to Break-Even First

28 Jan 2026
0 comment
1 attachment

This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit thresholds. It also includes a break-even (or small-profit) transition to reduce risk before the trailing logic begins.

// ===========================
// Trail Stop steps
// ===========================
triggerDistanceProfit = 20
triggerDistanceLoss = 15
MinimumProfit=20
MaxLoss=100

if not onmarket then
MoveToBreakEven=0
endif
// Example entry condition
IF NOT ONMARKET AND close CROSSES OVER Average[20](close) THEN
BUY 1 CONTRACT AT MARKET
stoploss = close-maxloss-(triggerDistanceLoss *pointsize)
newStopLevel = close + (triggerDistanceProfit * pointsize)
set stop price stoploss
ENDIF

// Management of open position
IF LONGONMARKET THEN
// Check if profit is greater or equal to trigger distance
IF close >= (newStopLevel + MinimumProfit) and MoveToBreakEven=0 THEN
MoveToBreakEven=1
stoploss=positionprice + Minimumprofit //Stoploss with small profit
newStopLevel = newStopLevel+triggerDistanceProfit*pointsize
set stop price stoploss
ENDIF

if close >= newStopLevel + (triggerDistanceProfit * pointsize) and MoveToBreakEven=1 then
stoploss = newStopLevel
newStopLevel = newStopLevel+triggerDistanceProfit*pointsize
set stop price stoploss
endif
ENDIF

 

IF NOT ONMARKET AND close CROSSES under Average[50](close) THEN
SELLSHORT 1 CONTRACT AT MARKET
stoploss = close+maxloss+(triggerDistanceLoss *pointsize)
newStopLevel = close – (triggerDistanceProfit * pointsize)
set stop price stoploss
ENDIF

// Management of open position
IF shortonmarket THEN
// Check if profit is greater or equal to trigger distance
IF close <= (newStopLevel – MinimumProfit) and MoveToBreakEven=0 THEN
MoveToBreakEven=1
stoploss=positionprice – Minimumprofit //Stoploss with small profit
newStopLevel = newStopLevel-triggerDistanceProfit*pointsize
set stop price stoploss
ENDIF

if close <= newStopLevel – (triggerDistanceProfit * pointsize) and MoveToBreakEven=1 then
stoploss = newStopLevel
newStopLevel = newStopLevel-triggerDistanceProfit*pointsize
set stop price stoploss
endif
ENDIF
// ===========================
// Graphic
// ===========================
// Trailing Stop and Next Level
graphonprice newStopLevel coloured(0, 0, 255) AS “NextLevel”
graphonprice stoploss coloured(255, 0, 0) AS “Trail Stop”
// Moving Average
graphonprice Average[20](close) coloured(255, 165, 0) AS “SMA20”
graphonprice Average[50](close) coloured(0, 165, 0) AS “SMA50”

Code Logic

  • triggerDistanceProfit: Step size (in points) used to define the next profit milestone and each subsequent trailing increment.
  • triggerDistanceLoss: Extra distance (in points) added to the initial stop placement beyond the MaxLoss offset.
  • MinimumProfit: Minimum favorable move required before switching to break-even/small-profit mode.
  • MaxLoss: Base maximum loss (in points) used to compute the initial stop from the entry context.
  • MoveToBreakEven: State flag; reset to 0 when flat, set to 1 once the break-even (small profit) stop has been activated.
  • Average[20](close) / Average[50](close): Example entry triggers; long entries occur on a cross above SMA20, short entries occur on a cross below SMA50.
  • stoploss: The active stop level sent via set stop price; initially wide, then tightened to small profit, then trailed in steps.
  • newStopLevel: The next milestone level; once price passes it by the required amount, it is advanced by triggerDistanceProfit and becomes the next reference.
  • LONGONMARKET block: When price reaches newStopLevel + MinimumProfit and MoveToBreakEven=0, the stop jumps to positionprice + MinimumProfit and step trailing is armed.
  • LONG step trailing: When price reaches newStopLevel + triggerDistanceProfit*pointsize and MoveToBreakEven=1, the stop is set to newStopLevel and newStopLevel advances by one step.
  • SHORTONMARKET block: Mirrors the long logic; break-even/small-profit triggers at newStopLevel – MinimumProfit, then the stop trails downward in fixed steps.
  • pointsize: Converts point-based inputs into instrument price increments to keep the logic compatible across markets.
  • graphonprice: Plots newStopLevel (blue) and stoploss (red) on the chart, plus SMA20 and SMA50 for visual validation.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/trailing-stop-with-steps/

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
DeathMetal New
Code artist, my biography is a blank page waiting to be scripted. Imagine a bio so awesome it hasn't been coded yet.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
33
indicator
132
strategy
171

Recent Snippets

How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...