This code snippet demonstrates how to implement various trailing stop strategies using the ProBuilder language. Trailing stops are a type of stop-loss order that adjusts automatically to the price movements of a stock. The code includes four different trailing stop mechanisms: a basic trailing stop, a breakeven stop, a maximum favorable excursion (MFE) based trailing stop, and a logic-based trailing stop.
//1/TRAILING STOP//////////////////////////////////////////////////////
once trailinstop= 0 //1 on - 0 off
trailingstart = 140 //trailing will start @trailinstart points profit
trailingstep = 10 //trailing step to move the "stoploss"
///2 BREAKEAVEN///////////
once breakeaven = 0 //1 on - 0 off
startBreakeven = 30 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
//3 MFE excursion //trailing stop
ONCE MFE=0
TRAILINGMFE = 20
//4/// logic trailing ale
ONCE logictrailing=0
TGL =22
TGS=21
//************************************************************************
//1 trailing stop function
if trailinstop>0 then
//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)>=trailingstart*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>=trailingstart*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
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif
///////////////////////////
///2///////////////////////////////////////////////
//reset the breakevenLevel when no trade are on market
if breakeaven>0 then
IF NOT ONMARKET THEN breakevenLevel=0 ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
endif
//3// MFE EXCURSION////
if MFE>0 then
//resetting variables when no trades are on market
if not onmarket then
MAXPRICEMFE = 0
MINPRICEMFE = close
priceexitMFE = 0
endif
//case SHORT order
if shortonmarket then
MINPRICE = MIN(MINPRICEMFE,close)
//saving the MFE of the current trade
if tradeprice(1)-MINPRICEMFE>=TRAILINGMFE*pointsize then
//if the MFE is higher than the trailingstop then
priceexitMFE = MINPRICEMFE+TRAILINGMFE*pointsize
//set the exit price at the MFE + trailing stop price level
endif
endif
//case LONG order
if longonmarket then
MAXPRICEMFE = MAX(MAXPRICEMFE,close)
//saving the MFE of the current trade
if MAXPRICEMFE-tradeprice(1)>=TRAILINGMFE*pointsize then
//if the MFE is higher than the trailingstop then
priceexitMFE = MAXPRICEMFE-TRAILINGMFE*pointsize
//set the exit price at the MFE - trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexitMFE>0 then
EXITSHORT AT priceexitMFE STOP
SELL AT priceexitMFE STOP
endif
ENDIF
///4 Logic trailing ale
// LOGIC TRAILING STOP
//RESET
if logictrailing>0 then
IF NOT ONMARKET THEN
MAXPRICE = 0
MINPRICE = CLOSE
PREZZOUSCITA = 0
ENDIF
//SE LONG ALLORA:
IF LONGONMARKET THEN
MAXPRICE = MAX(MAXPRICE,CLOSE)
//SAVING THE MFE OF THE CURRENT TRADE
IF MAXPRICE-TRADEPRICE(1)>=TGL*POINTSIZE THEN
//IF THE MFE IS HIGHER THAN THE TRAILINGSTOP THEN
PREZZOUSCITA = MAXPRICE-TGL*POINTSIZE
//SET THE EXIT PRICE AT THE MFE - TRAILING STOP PRICE LEVEL
ENDIF
ENDIF
IF SHORTONMARKET THEN
MINPRICE = MIN(MINPRICE,CLOSE)
//SAVING THE MFE OF THE CURRENT TRADE
IF TRADEPRICE(1)-MINPRICE>=TGS*POINTSIZE THEN
//IF THE MFE IS HIGHER THAN THE TRAILINGSTOP THEN
PREZZOUSCITA = MINPRICE+TGS*POINTSIZE
//SET THE EXIT PRICE AT THE MFE + TRAILING STOP PRICE LEVEL
ENDIF
ENDIF
//EXIT ON TRAILING STOP PRICE LEVELS
IF ONMARKET AND PREZZOUSCITA>0 THEN
EXITSHORT AT PREZZOUSCITA STOP
SELL AT PREZZOUSCITA STOP
ENDIF
endif
Explanation of the Code:
Check out this related content for more information:
https://www.prorealcode.com/topic/trailing-stop-and-breakeven-codes/#post-93285
Visit Link