This ProBuilder code snippet demonstrates how to implement a dynamic trailing stop strategy based on higher lows for long positions and lower highs for short positions. The trailing stop adjusts as the market price moves, providing a method to potentially protect gains or minimize losses in trading scenarios.
IF Not OnMarket THEN
MySLl = 0
MySLs = 999999
ENDIF
IF LONGONMARKET AND (close > low[1]) THEN
MySLl = max(low[1],MySLl)
ENDIF
IF MySLl > 0 THEN
SELL AT MySLl STOP
ENDIF
IF SHORTONMARKET AND (close < high[1]) THEN
MySLs = min(MySLs,high[1])
ENDIF
IF MySLs > 0 THEN
BUY AT MySLs STOP
ENDIF
Explanation of the Code:
Not OnMarket), the long stop loss (MySLl) is set to 0 and the short stop loss (MySLs) is set to a very high value (999999). This setup ensures that the stop values are reset when a new trade is initiated.LONGONMARKET) and the current closing price is greater than the previous low (close > low[1]), the long stop loss (MySLl) is updated to the maximum of the previous low or the existing stop loss value. This logic helps in raising the stop loss as the price makes new highs, securing profits on a move upwards.MySLl) is greater than 0, a sell order is placed at the stop loss level, allowing the position to exit when the price drops to this level.SHORTONMARKET), if the closing price is below the previous high (close < high[1]), the short stop loss (MySLs) is updated to the minimum of the previous high or the existing stop loss value. This adjustment lowers the stop loss as the price makes new lows, protecting gains in a downward moving market.MySLs) is greater than 0, a buy order is placed at this stop loss level to cover the short position when the price rises to this level.This strategy is useful for traders looking to automate their stop loss adjustments based on market movements, potentially enhancing their trading strategy's effectiveness by locking in profits and reducing losses.
Check out this related content for more information:
https://www.prorealcode.com/topic/trailing-stop-based-on-higher-lows/#post-197711
Visit Link