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.
What is breakeven?
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).
Breakeven code for ProRealTime
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.
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
// --- 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.
1 2 3 4 |
//reset the breakevenLevel when no trade are on market IF NOT ONMARKET THEN breakevenLevel=0 ENDIF |
A trading strategy with breakeven functionality
Here is a dummy strategy with RSI level 50 cross to test our new breakeven function!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 --- |
Make your trading strategy more profitable
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.
Hello Nicolas.
Thank you for the code.
I have been working in short market. Is it ok?
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
Nice, seems OK to me.
Thank you, great work.
Well done Eduardo – I was making a mess of this and then noticed your post.
That’s great.
Thank you for putting it up.
Is it possible add another sell term , e.g. RSI crosses under 80?
Is this somewhat almost the same as mef stop?
Anywhay this is great that u have chared this with us
This code only move your stoploss to breakeven price level once. That’s all.
thanks
Hello Nicolas, thank you for your blog post. I have included this in my strategy but noticed when I have more than one trade open it will move both stop loss to the same point. This can mean the second trade actually has the stop loss moved further away than intended and more risk is taken on the trade. I need it to treat each trade individually. I can’t figure out a way to do this. can you help please? Thanks
You could try with this code, with IG each order is treat individually, so the stoploss:
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SET STOP LOSS abs(tradeprice-breakevenlevel)
ENDIF
Thanks for the reply, Nicolas. I am using IG – in backtesting it works perfectly but in live trading it moves the stop loss’ together. Could this be because cumulate orders = true?
Not possible to move stop loss with different levels for individual orders.
how add a variable to this
if points moved is Z
points keep is .3*Z
limit is 2*Z
Bonjour Nicolas.
Grand merci pour ce code qui fonction à merveille. Serait-il possible de transformer le code afin que la mise à break even ne se fasse pas à la clôture de la bougie, mais directement lorsque les prix atteignent le nombre de points fixés ?
En te remerciant d’avance pour ta réponse.
Hi, Is it possible to tweak this to move the stop loss when a certain % gain instead of pips?
awesome strategy could you post please the same but for short trades ?
try looking in the comments here or in forums.
topic tagged with “breakeven” word: https://www.prorealcode.com/topics-tag/breakeven/
this is really a helpful snippet of code – thanks to all of you!
Bonjour à tous, merci Nicolas pour ce code bien utile
Super ! Un grand Merci 🙂
Bonjour Nicolas, J’ai le même code que toi mais il y a des fois, à la clôture d’une bougie que le stop est recalculé mais à la baisse par exemple si je suis en long. As tu déjà eu ce problème?
Hello everyone, below code that I used on one of mine TS. Problem is that software effectively moved stop after entry +5 but unfortunately since it didn’t take target and market went down during this movement stop returned where it was put at begininng. Who knows why?
Startbreakeven=50
Pointstokeep=5
If NOT Onmarket then
breakevenlevel=0
endif
If longonmarket and close-tradeprice(1) >= startbreakeven*1 then //1 should be the size referred to traded contract. Is it right?
breakevenlevel = tradeprice(1)+Pointstokeep*1
If breakevenlevel>0 then
sell at breakevenlevel stop
endif
endif
If shortonmarket and tradeprice(1)-close>= startbreakeven*1 then//indica un euro a punto per i contratti grandi va modificato
breakevenlevel= tradeprice(1)-Pointstokeep*1
If breakevenlevel>0 then
exitshort at breakevenlevel stop
endif
endif
Hi Nicolas, can you send me the code for SELL-SIDE and can I use BUY-SIDE and SELL-SIDE in the same algo? reagards, Roger
efparam 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 —