Trailing Stop and Breakeven codes

Viewing 11 posts - 16 through 26 (of 26 total)
  • Author
    Posts
  • #145411 quote
    Paul
    Participant
    Master
    // trailing atr stop
    once trailingstoptype1 = 1 // trailing stop - 0 off, 1 on
    
    if trailingstoptype1 then
    //========================
    once tsatrdistlong  = 5 // ts atr distance
    once tsatrdistshort = 5 // ts atr distance
    
    once tsincrements = 0.05 // set to 0 to ignore tsincrements
    once tsminatrdist = 2
    
    once tsatrperiod    = 14 // ts atr parameter
    once tsminstop      = 10 // ts minimum stop distance
    
    once ts1sensitivity = 1  // [0]close;[1]high/low;[2]low;high
    //========================
    if barindex=tradeindex then
    trailingstoplong = tsatrdistlong
    trailingstopshort = tsatrdistshort
    else
    if longonmarket then
    if tsnewsl>0 then
    if trailingstoplong>tsminatrdist then
    if tsnewsl>tsnewsl[1] then
    trailingstoplong=trailingstoplong
    else
    trailingstoplong=trailingstoplong-tsincrements
    endif
    else
    trailingstoplong=tsminatrdist
    endif
    endif
    endif
    if shortonmarket then
    if tsnewsl>0 then
    if trailingstopshort>tsminatrdist then
    if tsnewsl<tsnewsl[1] then
    trailingstopshort=trailingstopshort
    else
    trailingstopshort=trailingstopshort-tsincrements
    endif
    else
    trailingstopshort=tsminatrdist
    endif
    endif
    endif
    endif
    tsatr=averagetruerange[tsatrperiod]((close/10)*pipsize)/1000
    //tsatr=averagetruerange[tsatrperiod]((close/1)*pipsize) // (forex)
    tgl=round(tsatr*trailingstoplong)
    tgs=round(tsatr*trailingstopshort)
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    tsmaxprice=0
    tsminprice=close
    tsnewsl=0
    endif
    if ts1sensitivity=1 then
    ts1sensitivitylong=high
    ts1sensitivityshort=low
    elsif ts1sensitivity=2 then
    ts1sensitivitylong=low
    ts1sensitivityshort=high
    else
    ts1sensitivitylong=close
    ts1sensitivityshort=close
    endif
    if longonmarket then
    tsmaxprice=max(tsmaxprice,ts1sensitivitylong)
    if tsmaxprice-tradeprice(1)>=tgl*pointsize then
    if tsmaxprice-tradeprice(1)>=tsminstop then
    tsnewsl=tsmaxprice-tgl*pointsize
    else
    tsnewsl=tsmaxprice-tsminstop*pointsize
    endif
    endif
    endif
    if shortonmarket then
    tsminprice=min(tsminprice,ts1sensitivityshort)
    if tradeprice(1)-tsminprice>=tgs*pointsize then
    if tradeprice(1)-tsminprice>=tsminstop then
    tsnewsl=tsminprice+tgs*pointsize
    else
    tsnewsl=tsminprice+tsminstop*pointsize
    endif
    endif
    endif
    if barindex-tradeindex>1 then
    if longonmarket then
    if tsnewsl>0 then
    sell at tsnewsl stop
    endif
    if tsnewsl>0 then
    if close crosses under tsnewsl then
    sell at market
    endif
    endif
    endif
    if shortonmarket then
    if tsnewsl>0 then
    exitshort at tsnewsl stop
    endif
    if tsnewsl>0 then
    if close crosses over tsnewsl then
    exitshort at market
    endif
    endif
    endif
    endif
    endif
    
    // trailing stop percentage
    once trailingstoptype2 = 1
    
    if trailingstoptype2 then
    //====================
    once trailingpercentlong  = 0.333 // %
    once trailingpercentshort = 0.333 // %
    once accelerator     = 1 // 1 = default; always > 0 (i.e. 0.5-3)
    once ts2sensitivity  = 1 // [0]close;[1]high/low;[2]low;high
    //====================
    once steppercentlong  = (trailingpercentlong/10)*accelerator
    once steppercentshort = (trailingpercentshort/10)*accelerator
    if onmarket then
    trailingstartlong = tradeprice(1)*(trailingpercentlong/100)
    trailingstartshort = tradeprice(1)*(trailingpercentshort/100)
    
    trailingsteplong = tradeprice(1)*(steppercentlong/100)
    trailingstepshort = tradeprice(1)*(steppercentshort/100)
    endif
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    newsl=0
    endif
    if ts2sensitivity=1 then
    ts2sensitivitylong=high
    ts2sensitivityshort=low
    elsif ts2sensitivity=2 then
    ts2sensitivitylong=low
    ts2sensitivityshort=high
    else
    ts2sensitivitylong=close
    ts2sensitivityshort=close
    endif
    if longonmarket then
    if newsl=0 and ts2sensitivitylong-tradeprice(1)>=trailingstartlong then
    newsl = tradeprice(1)+trailingsteplong
    endif
    if newsl>0 and ts2sensitivitylong-newsl>trailingsteplong then
    newsl = newsl+trailingsteplong
    endif
    endif
    if shortonmarket then
    if newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort then
    newsl = tradeprice(1)-trailingstepshort
    endif
    if newsl>0 and newsl-ts2sensitivityshort>trailingstepshort then
    newsl = newsl-trailingstepshort
    endif
    endif
    if barindex-tradeindex>1 then
    if longonmarket then
    if newsl>0 then
    sell at newsl stop
    endif
    if newsl>0 then
    if close crosses under newsl then
    sell at market
    endif
    endif
    endif
    if shortonmarket then
    if newsl>0 then
    exitshort at newsl stop
    endif
    if newsl>0 then
    if close crosses over newsl then
    exitshort at market
    endif
    endif
    endif
    endif
    endif
    eckaw and christofferR thanked this post
    #161799 quote
    minhbui
    Participant
    New

    Hello,

    Can you explain me how to use this in prorealtime?

    I import the itf file to prorealtime and how can apply the trailing stop to my trade?

    Thank you

    #179812 quote
    coh123
    Participant
    New

    Hi everyone,

    I’ve spent hours reading through all this and I’m still finding it tricky to understand to be honest. Why does it seem so difficult to add a breakeven line of code? I’m very new to all this so do give me some patience haha.

    I am just practicing and getting the hang of coding bits so to keep it simple I have just created a code to buy when the RSI gets to 30 and have set a trailing stop too but what I want to do is simply add something like “breakeven level 10 pips above entry, exit the market if triggered ”

    Is this possible? All I see on here is 100 lines of code for something that seems so simple!

    Any help would be greatly appreciated, thanks guys

    #179813 quote
    robertogozzi
    Moderator
    Master

    This is the code for breakeven from https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/:

    trailingstart = 30    //breakeven will be set at this profit level
    points2keep   = 10    //points to keep at breakeven
    //reset the stoploss value
    IF NOT ONMARKET THEN
     newSL=0
    ENDIF
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
     IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
      newSL = tradeprice(1)+points2keep*pipsize
     ENDIF
    ENDIF
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
     IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
      newSL = tradeprice(1)-points2keep*pipsize
     ENDIF
    ENDIF
    //stop order to exit the positions
    IF newSL>0 THEN
     SELL AT newSL STOP
     EXITSHORT AT newSL STOP
    ENDIF
    Bodaris thanked this post
    #179818 quote
    robertogozzi
    Moderator
    Master

    The above code is 1/4 of 100 lines!

    Is it still too much much?

    There you go with just 4 lines:

    If (positionprice * PositionPerf / PipSize) >= 30 * PipSize then
       Sell at tradeprice + 10*pipsize STOP
       Exitshort at tradeprice - 10*pipsize STOP
    Endif

    That’s pretty short!
    Add this to your code and you will soon report that it will not always exit at the expected entry price + 10 pips.

    That’s because pending orders expire each bar, so when your profit is 30+ pips they will be placed, but if they are not triggered and the next bar your profit is, say, 29 pips, they will not be placed again and you may suffer an unexpected big loss! How can this be dealt with? By adding a few lines to RECORD that your trigger level has been reached, so the stop orders are to be placed again, next bar, even if by that time your profit is lower than that level:

    Once NewSL = 0
    If (positionprice * PositionPerf / PipSize) >= 30 * PipSize then
       If LongOnMarket then
          NewSL = tradeprice + 10*pipsize
       ElsIf ShortOnMarket then
          NewSL = tradeprice - 10*pipsize
    Endif
    If NewSL > 0 then
       Sell at NeWSL STOP
       Exitshort at NewSL STOP
    Endif

    as you can see, a few more lines have been added, but they are still not so many!

    Well… there’s still a problem. Next trade NewSL will still retain the previous exit price and it may exit at an incorrect price,

    We need to add a few more lines to prevent this from happening:

    Once NewSL = 0
    If not OnMarket then
       NewSL = 0
    Endif
    If (positionprice * PositionPerf / PipSize) >= 30 * PipSize then
       If LongOnMarket then
          NewSL = tradeprice + 10*pipsize
       ElsIf ShortOnMarket then
          NewSL = tradeprice - 10*pipsize
    Endif
    If NewSL > 0 then
       Sell at NeWSL STOP
       Exitshort at NewSL STOP
    Endif

    it’s no more just a few lines (and I did not add comments to make code easier to read),  it it can’t be done with less than that!

    100+ lines can easily be reached to add more sophisticated features.

    It’s like starting a car, you don’t need too much engineering, but then you have to add support for speeding up, then slowing down, then braking, then steering, then accommodating people, etc…

    GraHal and Bodaris thanked this post
    #179819 quote
    coh123
    Participant
    New

    That’s great thank you so much for the help. I really appreciate it!

    #184196 quote
    Khaled
    Participant
    Veteran

    Hi Paul, thank you for sharing your hard work. I was trying your latest code based on (%) and I keep getting the same values in backtest while varying “trailingpercentlong” and “trailingpercentshort” from 0.1 to 1 with an increment of 0.1. I kept accelerator and sensitivity fixed.

    Is there something I’m missing?

    Thanks

    // trailing stop percentage
    once trailingstoptype2 = 1
     
    if trailingstoptype2 then
    //====================
    once trailingpercentlong  = 0.333 // %
    once trailingpercentshort = 0.333 // %
    once accelerator     = 1 // 1 = default; always > 0 (i.e. 0.5-3)
    once ts2sensitivity  = 1 // [0]close;[1]high/low;[2]low;high
    //====================
    once steppercentlong  = (trailingpercentlong/10)*accelerator
    once steppercentshort = (trailingpercentshort/10)*accelerator
    if onmarket then
    trailingstartlong = tradeprice(1)*(trailingpercentlong/100)
    trailingstartshort = tradeprice(1)*(trailingpercentshort/100)
     
    trailingsteplong = tradeprice(1)*(steppercentlong/100)
    trailingstepshort = tradeprice(1)*(steppercentshort/100)
    endif
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    newsl=0
    endif
    if ts2sensitivity=1 then
    ts2sensitivitylong=high
    ts2sensitivityshort=low
    elsif ts2sensitivity=2 then
    ts2sensitivitylong=low
    ts2sensitivityshort=high
    else
    ts2sensitivitylong=close
    ts2sensitivityshort=close
    endif
    if longonmarket then
    if newsl=0 and ts2sensitivitylong-tradeprice(1)>=trailingstartlong then
    newsl = tradeprice(1)+trailingsteplong
    endif
    if newsl>0 and ts2sensitivitylong-newsl>trailingsteplong then
    newsl = newsl+trailingsteplong
    endif
    endif
    if shortonmarket then
    if newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort then
    newsl = tradeprice(1)-trailingstepshort
    endif
    if newsl>0 and newsl-ts2sensitivityshort>trailingstepshort then
    newsl = newsl-trailingstepshort
    endif
    endif
    if barindex-tradeindex>1 then
    if longonmarket then
    if newsl>0 then
    sell at newsl stop
    endif
    if newsl>0 then
    if close crosses under newsl then
    sell at market
    endif
    endif
    endif
    if shortonmarket then
    if newsl>0 then
    exitshort at newsl stop
    endif
    if newsl>0 then
    if close crosses over newsl then
    exitshort at market
    endif
    endif
    endif
    endif
    endif
    Capture-décran-2021-12-31-à-12.49.03.png Capture-décran-2021-12-31-à-12.49.03.png
    #184215 quote
    PeterSt
    Participant
    Master

    Try to make it way more extreme. Say up to +10%, starting even at a minus percentage. It is quite easy to have the same trade results with different trailing because of other factors (I did not check out that code).

    Obviously you did not forget to comment out the setting of the variables you now let vary in optimising, right ?

    Khaled thanked this post
    #184220 quote
    Khaled
    Participant
    Veteran

    Thanks Peter, just found out that this trailing module doesn’t react the same way in MTF. Say you’re executing a 5min Algo on a 1 min chart. If you leave everything under TIMEFRAME(5 minutes), this module doesn’t change. If you insert TIMEFRAME(DEFAULT) just before this module, it works fine.

    #213001 quote
    ZeroCafeine
    Participant
    Senior

    hi every one, I hope your are fine,

    I discovered this code a few days ago that I’m trying to understand, I understood the first two parts well, the I’m going to concentrate on the MFE, however I don’t understand the logic of wanting to disable the trailing stop and the BE?

    Can someone explain to me why you want to deactivate these two options, because anyway if the trade doesn’t go our way there’s no point in giving it more chance or more room to manoeuvre, because in this case you don’t stick to your plan and you end up hoping, and that’s really not good for trading

    Translated with http://www.DeepL.com/Translator (free version)

    #213025 quote
    Madrosat
    Participant
    Master

    Khaled

    What kind of crossing hull do you use ???

Viewing 11 posts - 16 through 26 (of 26 total)
  • You must be logged in to reply to this topic.

Trailing Stop and Breakeven codes


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Gianluca @altares Participant
Summary

This topic contains 25 replies,
has 10 voices, and was last updated by Madrosat
2 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/10/2019
Status: Active
Attachments: 1 files
Logo Logo
Loading...