Since it is also a common request on forums, you’ll find below some examples of breakeven function to be included in any automated trading strategy.
The breakeven is the point where your current trades on market will not make gain or loss ; or at least a small gain. It is largely used to secure trade(s) (with either none or small profit) when the current trade(s) have already achieved some gain.
For example:
If your current trade on market have already move favourably of 10 points, you can move your stoploss to the entry price. That way, you are safe and even if the price retrace to your entry point, you make no loss at all. You could have even put your breakeven level to your price entry with a small profit secure: 2 or 3 points above your entry price (in case of a buy order) or below it for a sell order.
(picture taken from forum’s user : Big Hug, thanks to him).
First we need to set the parameters of the function: when to start the breakeven function and where to put our new breakeven price level.
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)
In this example, we’ll wait for the price to have move favourably in the right direction of the trade of 30 points. When the price has reached this profit, we calculate the new “stoploss” or ‘breakevenLevel’ in our code and place it on market:
Case of long positions only:
// --- 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
Of course, because when no open positions exist we need to reset the breakevenLevel to 0, otherwise the code will try to set a stop breakeven order at the previously calculated one for the next orders even if they do have not reached the ‘startBreakeven’ threshold.
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
Here is a dummy strategy with RSI level 50 cross to test our new breakeven function!
defparam cumulateorders = false
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)
c1 = RSI[14] crosses over 50
if c1 then
BUY 1 LOT AT MARKET
SET STOP PLOSS 50 //first stoploss
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 ---
Of course breakeven can make your strategy safer, but it can also cut a lot your profit because you don’t let them run when price retrace a lot. While we can trigger the breakeven to be set when a price reach some gains, we could also trigger it with specific conditions, for examples : price retrace more than 50% from the last trend, a new moving average crossover have occurred, etc.. Everything’s possible and your imagination is the limit.
You’ll find the ITF file of the example downloadable at the end of the article.