This code snippet demonstrates how to implement a trailing stop strategy based on candle highs using the ProBuilder programming language. The strategy includes conditions for entering trades, managing monthly losses, and dynamically adjusting stop levels based on price movements.
DEFPARAM CUMULATEORDERS = false
PositionSize=1
//=== Max Monthly Loss ===
ONCE MyProfit = 0
ONCE TradeON = 1
IF Month <> Month[1] THEN
MyProfit = STRATEGYPROFIT //store profits/losses at the beginning of each month
TradeON = 1 //enable trading each new month
ENDIF
IF (STRATEGYPROFIT - MyProfit) < -3500 THEN
TradeON = 0 //disable trading when losing > 200 currency units
ENDIF
//=== Entry Filter ===
//Filter 1
indicator1=average[75,7]
indicator2=average[125,7]
indicator3=average[150,7]
F1 = indicator1>indicator2
F2 = indicator2>indicator3
//=== Entry Criteria ===
//Entry Criteria 1
entrylen = 5
LowVol = round(volume[4])
E1 = volume < LowVol
bullish = close>lowest[entrylen](low)
IF E1 AND bullish AND F1 AND F2 AND opendayofweek <> 3 AND opendayofweek <> 4 AND opendayofweek <> 0 AND opendayofweek <> 6 AND TradeON THEN
BUY PositionSize CONTRACTS AT MARKET
SET STOP %LOSS 1
set target %profit 2
ENDIF
// trailingstopRTS=1
IF trailingstopRTS then
IF Not OnMarket THEN
PriceDistance = 10 * PipSize //PriceDistance from price as required by the broker
StopDistance = 30 * PipSize //Distance from HIGH
SellPrice = close - StopDistance //The first bar StopDistance is from CLOSE
ELSE
SellPrice = max(SellPrice,high - StopDistance)
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
ENDIF
// graphonprice
SellPrice coloured(0,128,0,150)
Explanation of the Code:
Check out this related content for more information:
https://www.prorealcode.com/topic/robertogozzi-trail-stops-trailing/#post-178202
Visit Link