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

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

    Thank you, great work.

  3. Big Hug • 245 days ago #

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

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

  5. Kenneth Kvistad • 173 days ago #

    Is this somewhat almost the same as mef stop? 

    Anywhay this is great that u have chared this with us

    • Nicolas • 172 days ago #

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

  6. Bin • 172 days ago #

    thanks

avatar
Register or

Top