This code snippet demonstrates how to implement trailing stops for both long and short trading positions in the ProBuilder language, with conditional logic to decide whether to exit the trade using a stop order, a limit order, or at the market price based on the current price’s relation to a defined distance.
//************************************************************************
//trailing stop function
trailingstartL = 20 //LONG trailing will start @trailinstart points profit
trailingstartS = 20 //SHORT trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
Distance = 6 * PipSize //reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstartL*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstartS*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
IF LongOnMarket THEN
IF (close - Distance) > newSL THEN
SELL AT newSL STOP
ELSIF (close + Distance) < newSL THEN
SELL AT newSL LIMIT
ELSE
SELL AT Market
ENDIF
ELSIF ShortOnMarket THEN
IF (close - Distance) > newSL THEN
EXITSHORT AT newSL LIMIT
ELSIF (close + Distance) < newSL THEN
EXITSHORT AT newSL STOP
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
ENDIF
//************************************************************************
This code snippet is structured to manage trading positions by adjusting the stop loss dynamically as the trade moves into profit. Here's a breakdown of its functionality:
This example is useful for understanding how to implement advanced trading strategies programmatically, focusing on maximizing profits and minimizing losses through dynamic stop loss adjustments.
Check out this related content for more information:
https://www.prorealcode.com/topic/breakeven-stop-code-long-short/#post-172745
Visit Link