This code snippet demonstrates how to implement a dynamic trailing stop strategy in ProBuilder language, starting from an initial stop loss position and adjusting the stop loss as the market price moves favorably. The strategy also includes a take profit condition to close trades under certain conditions.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = Average[20](close)
indicator2 = Average[100](close)
c1 = (indicator1 CROSSES under indicator2)
IF c1 AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
NewSL = 0
TSON = 0
ENDIF
// Conditions to exit long positions
indicator3 = Average[20](close)
indicator4 = Average[100](close)
c2 = (indicator3 crosses over indicator4)
IF OnMarket AND Not OnMarket[1] THEN
NewSL = TradePrice - (A46 * PipSize)
ENDIF
If C2 AND OnMarket Then
TSON = 1
Endif
If TSON Then
// Stops and targets
trailingstart = A21 //10 //trailing will start @trailinstart points profit
trailingstep = A22 //30 //trailing step to move the "stoploss"
//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*pipsize THEN
//newSL = tradeprice(1)+trailingstep*pipsize
//ENDIF
//next moves
//IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
IF close>=NewSL+(trailingstep*pipsize*2) THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
Endif
TSOUT = Close < newSL AND C2 = 1
//GRAPH Close < newSL
SET STOP pLOSS A46 //500
set target pProfit A46*A99
//GRAPH C2
//GRAPH TSOUT
graphonprice NewSL coloured(0,0,255,255)
graphonprice tradeprice
Explanation of the Code:
Check out this related content for more information:
https://www.prorealcode.com/topic/trigger-trailing-sl-if-target-is-met/page/4/#post-198551
Visit Link