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”
Check out this related content for more information:
https://www.prorealcode.com/topic/trailing-stop-with-steps/
Visit Link