Breakeven code for your automated trading strategy

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.

move-stop-to-breakeven-prorealtime(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.

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:

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.

 

A trading strategy with breakeven functionality

Here is a dummy strategy with RSI level 50 cross to test our new breakeven function!

breakeven-examples

 

 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.

Share this

  1. Eduardo M • 09/21/2016 #

    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
     

  2. Eduardo M • 09/21/2016 #

    Thank you, great work.

  3. Big Hug • 09/21/2016 #

    Well done Eduardo – I was making a mess of this and then noticed your post.
    That’s great.
    Thank you for putting it up.

  4. Robin Chan • 09/21/2016 #

    Is it possible add another sell term , e.g. RSI crosses under 80?

  5. Kenneth Kvistad • 09/21/2016 #

    Is this somewhat almost the same as mef stop? 
    Anywhay this is great that u have chared this with us

    • Nicolas • 09/21/2016 #

      This code only move your stoploss to breakeven price level once. That’s all.

  6. Bin • 09/21/2016 #

    thanks

  7. TOM D • 09/21/2016 #

    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

    • Nicolas • 09/21/2016 #

      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

  8. TOM D • 09/21/2016 #

    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?

    • Nicolas • 09/21/2016 #

      Not possible to move stop loss with different levels for individual orders.

  9. rama • 09/21/2016 #

    how add a variable to this
    if points moved is Z
    points keep is .3*Z
    limit is 2*Z

  10. lannoogeoffrey • 09/21/2016 #

    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.

  11. Nick Chan • 09/21/2016 #

    Hi, Is it possible to tweak this to move the stop loss when a certain % gain instead of pips?

  12. rmhandel • 09/21/2016 #

    awesome strategy could you post please the same but for short trades ?

    • Nicolas • 09/21/2016 #

      try looking in the comments here or in forums.
      topic tagged with “breakeven” word: https://www.prorealcode.com/topics-tag/breakeven/

  13. mr blue • 09/21/2016 #

    this is really a helpful snippet of code – thanks to all of you!

  14. Dom • 09/21/2016 #

    Bonjour à tous, merci Nicolas pour ce code bien utile

  15. sophia_etoille83 • 09/21/2016 #

    Super ! Un grand Merci 🙂

  16. bubu85 • 09/21/2016 #

    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?

  17. elfuego77 • 09/21/2016 #

    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

  18. RogerIG • 09/21/2016 #

    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 —

avatar
Register or

Top