This ProBuilder code snippet demonstrates how to implement a trailing stop loss strategy that adjusts based on the high or low prices from three bars ago. The strategy is designed for both long and short positions in trading.
Not tested:
IF MyLongConditions AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
NewSL = low[3]-1*pipsize
SELL AT NewSL STOP
ENDIF
IF MyShortConditions AND Not OnMarket THEN
SELLSHORT 1 CONTRACT AT MARKET
NewSL = high[3]+1*pipsize
EXITSHORT AT NewSL STOP
ENDIF
IF OnMarket THEN
IF PositionPerf > 0 THEN
IF LongOnMarket THEN
NewSL = max(newSL,low[3]-1*pipsize)
ELSE
NewSL = min(newSL,high[3]+1*pipsize)
ENDIF
ENDIF
IF NewSL > 0 THEN
SELL AT NewSL STOP
EXITSHORT AT NewSL STOP
ENDIF
ENDIF
Explanation of the Code:
This code snippet is useful for traders looking to protect their profits by adjusting their stop loss levels based on recent market movements, specifically targeting the highs and lows from three bars prior.
Check out this related content for more information:
https://www.prorealcode.com/topic/stop-loss-3-candles/#post-150870
Visit Link