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!



 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
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP

  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 #


  7. TOM D • 258 days ago #

    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 • 258 days ago #

      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)

  8. TOM D • 257 days ago #

    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 • 257 days ago #

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

  9. rama • 104 days ago #

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

Register or