This code snippet demonstrates how to dynamically adjust stop loss levels based on market structure, specifically identifying swing highs and lows over a specified lookback period. It is designed for use in trading strategies to help manage risk and protect profits.
ONCE SigHI1 = 0
ONCE SigHI2 = 0
ONCE SigLO1 = 0
ONCE SigLO2 = 0
ONCE LookBack = 10
IF Not OnMarket Then
MySL = 0
ENDIF
MaxHI = max(open,close)
MinLO = min(open,close)
Bullish = close > open
Bearish = close < open
SwingHI = highest[LookBack](MaxHI)
SwingLO = lowest[LookBack](MinLO)
SigHI = SwingHI AND Bearish AND Bullish[1]
SigLO = SwingLO AND Bullish AND Bearish[1]
IF LongOnMarket AND SigHI > 0 AND SigHI > SigHI1 THEN
SigHI2 = SigHI1
SigHI1 = SigHI
IF SigHI1 > SigLO1 THEN
MySL = max(TradePrice,max(MySL,SigLO1))
ENDIF
ELSIF ShortOnMarket AND SigLO > 0 AND SigLO < SigLO1 THEN
SigLO2 = SigLO1
SigLO1 = SigLO
IF SigLO1 < SigHI1 THEN
MySL = min(TradePrice,min(MySL,SigLO1))
ENDIF
ENDIF
IF OnMarket AND MySL <> 0 THEN
SELL AT MySL Stop
EXITSHORT AT MySL Stop
ENDIF
Explanation of the Code:
This snippet is a foundational example of how to use market highs and lows to manage trade exits dynamically. Adjust the LookBack value to fit different trading styles or market conditions.
Check out this related content for more information:
https://www.prorealcode.com/topic/move-to-be-on-confirmation-of-market-structure/#post-137288
Visit Link