This code snippet demonstrates how to implement three different types of trailing stop strategies in ProBuilder language. Trailing stops are dynamic stop orders that adjust as the market price moves, helping to protect profits while allowing a trade to remain open as long as the price is moving favorably.
//%trailing stop function
trailingPercent = tst
stepPercent = st
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN newSL=0 ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN newSL = tradeprice(1)+trailingstep ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN newSL = newSL+trailingstep ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN newSL = tradeprice(1)-trailingstep ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN newSL = newSL-trailingstep ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
Explanation of the Code:
This snippet is a practical example of how to manage risk dynamically in trading strategies using ProBuilder language, focusing on trailing stops to lock in profits while allowing room for further gains.
Check out this related content for more information:
https://www.prorealcode.com/topic/moving-stop-loss/#post-136960
Visit Link