This ProBuilder code snippet demonstrates how to implement a trailing stop strategy that retains a specified percentage of profit for both long and short positions in trading. The strategy starts to trail the stop loss once a certain amount of profit (in pips) is achieved, and it continues to secure a percentage of the maximum profit as the price moves favorably.
ONCE TrailStart = 30 //30 Start trailing profits from this point
ONCE ProfitPerCent = 0.666 //66.6% Profit to keep
IF Not OnMarket THEN
y1 = 0
y2 = 0
ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN //LONG
x1 = (close - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN //go ahead only if 30+ pips
y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit
ENDIF
IF y1 THEN //Place pending STOP order when y>0
SELL AT Tradeprice + (y1 * pipsize) STOP //convert pips to price
ENDIF
ELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN //SHORT
x2 = (tradeprice - close) / pipsize //convert price to pips
IF x2 >= TrailStart THEN //go ahead only if 30+ pips
y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit
ENDIF
IF y2 THEN //Place pending STOP order when y>0
EXITSHORT AT Tradeprice - (y2 * pipsize) STOP //convert pips to price
ENDIF
ENDIF
Explanation of the Code:
This code snippet is a practical example of how to manage risk and secure profits in trading by dynamically adjusting stop orders based on market movements and predefined parameters.