This code snippet demonstrates how to implement a trading strategy in ProBuilder that adjusts the stop loss to breakeven after a certain profit is achieved and exits positions based on specific candlestick patterns when the breakeven is set. This strategy is useful for managing risk and securing profits in volatile markets.
defparam cumulateorders = false
startBreakeven = 20 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 1 //how much pips/points to keep in profit above or below our entry price when the breakeven is activated (beware of spread)
//exit long order on red candle (if breakeven is set)
if longonmarket and close0 then
sell at market
endif
//exit short order on green candle (if breakeven is set)
if shortonmarket and close>open and breakevenLevel>0 then
exitshort at market
endif
//dummy strategy
c1 = RSI[14] crosses over 50
if c1 then
BUY 1 LOT AT MARKET
SET STOP PLOSS 50
endif
//reset the breakevenLevel when no trade are on market
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 ---
// --- SELL SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
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
// --- end of SELL SIDE ---
The code is structured into several key sections:
This example provides a practical approach to managing trades using breakeven adjustments and conditional exits based on market conditions, which can be particularly useful in strategies aiming to minimize risk and protect gains.