Trailing stop fails to move

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #148516 quote
    nonetheless
    Participant
    Master

    In the attached image a short position has opened at 27971.8

    The TS should move to ~ break even after a close below .24%, or 27904.6

    //%trailing stop function
    once trailingpercentlong  = 0.21 // %
    once trailingpercentshort = 0.24 // %
    once accelerator     = 0.05 // 1 = default; always > 0 (i.e. 0.5-3)
    once accelerator2     = 0.01 // 1 = default; always > 0 (i.e. 0.5-3)
    once ts2sensitivity  = 2 // [0]close;[1]high/low;[2]low;high
    //====================
    once steppercentlong  = (trailingpercentlong/10)*accelerator
    once steppercentshort = (trailingpercentshort/10)*accelerator2
    if onmarket then
    trailingstartlong = tradeprice(1)*(trailingpercentlong/100)
    trailingstartshort = tradeprice(1)*(trailingpercentshort/100)

    At 13:45 the candle closes at 27898.6 but the stop doesn’t move. I’ve never had this problem before using this TS code.

    MoD-sl.jpg MoD-sl.jpg
    #148518 quote
    robertogozzi
    Moderator
    Master

    The code is missing.

    The above lines are just the settings and calculations, not the instructions that use them.

    #148524 quote
    nonetheless
    Participant
    Master

    this is the full TS code. do you need to see the whole algo?

    once trailingpercentlong  = .21 // %
    once trailingpercentshort = .24 // %
    once accelerator     = .05 // 1 = default; always > 0 (i.e. 0.5-3)
    once accelerator2     = .01// 1 = default; always > 0 (i.e. 0.5-3)
    once ts2sensitivity  = 2 // [0]close;[1]high/low;[2]low;high
    //====================
    once steppercentlong  = (trailingpercentlong/10)*accelerator
    once steppercentshort = (trailingpercentshort/10)*accelerator2
    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 low 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 high crosses over newsl then
    exitshort at market
    endif
    endif
    endif
    endif
    #148531 quote
    robertogozzi
    Moderator
    Master

    It works fine for me, I think it can be due to your setting:

    ts2sensitivity  = 2

    which sets HIGH for short trades and LOW for long trades to compute profits, which is rather uncommon , 1 is the settings that perfectly repliucates manual trading, many times 0 is also used.

    Another issue could be if you accumulate orders, then lines 10-14 would change everytime a new positio is added because TRADEPRICE(1) refers to the last entry price.

    #148538 quote
    nonetheless
    Participant
    Master

    Thanks Roberto, it doesn’t accumulate orders so that’s not an issue.

    But are you saying that with ts2sensitivity  = 2 you would not expect the stop to move after a close below .24% ? and if not, at what point would it initiate? (I closed the position manually so I don’t know how it would have developed)

    What exactly is the effect of these lines? How does it change when the trailingstart begins?

    if shortonmarket then
    if newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort then
    newsl = tradeprice(1)-trailingstepshort
    endif

    If ts2sensitivityshort=high what does this pertain to? the high of what?

    #148540 quote
    Paul
    Participant
    Master

    That sensitivity had issues with the breakevenstop, “once” had to be removed and I made it different. Can you test this plz. Updated also the others. Or just remove it and use the close.

    The result is the same for positionprice & tradeprice(1) (old one)

    Using high/low low/high or close just made a small difference when the trailingstop activates.

    // %trailing stop function incl. cumulative positions
    once trailingstoptype1= 1
     
    if trailingstoptype1 then
    //====================
    trailingpercentlong  = 0.75*sl // %
    trailingpercentshort = 0.75*sl // %
    once acceleratorlong = 0.1 // [1] default; always > 0 (i.e. 0.5-3)
    once acceleratorshort= 0.1 // 1 = default; always > 0 (i.e. 0.5-3)
    ts2sensitivity  = 3 // [1] default [2] hl [3] lh (not use once)
    //====================
    once steppercentlong  = (trailingpercentlong/10)*acceleratorlong
    once steppercentshort = (trailingpercentshort/10)*acceleratorshort
    if onmarket then
    trailingstartlong = positionprice*(trailingpercentlong/100)
    trailingstartshort = positionprice*(trailingpercentshort/100)
     
    trailingsteplong = positionprice*(steppercentlong/100)
    trailingstepshort = positionprice*(steppercentshort/100)
    endif
     
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    newsl           = 0
    mypositionprice = 0
    endif
    positioncount = abs(countofposition)
    if newsl > 0 then
    if positioncount > positioncount[1] then
    if longonmarket then
    newsl = max(newsl,positionprice * newsl / mypositionprice)
    else
    newsl = min(newsl,positionprice * newsl / mypositionprice)
    endif
    endif
    endif
    if ts2sensitivity=1 then
    ts2sensitivitylong=close
    ts2sensitivityshort=close
    elsif ts2sensitivity=2 then
    ts2sensitivitylong=high
    ts2sensitivityshort=low
    elsif ts2sensitivity=3 then
    ts2sensitivitylong=low
    ts2sensitivityshort=high
    endif
    if longonmarket then
    if newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong*pipsize then
    newsl = positionprice+trailingsteplong*pipsize
    endif
    if newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong*pipsize then
    newsl = newsl+trailingsteplong*pipsize
    endif
    endif
    if shortonmarket then
    if newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort*pipsize then
    newsl = positionprice-trailingstepshort*pipsize
    endif
    if newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort*pipsize then
    newsl = newsl-trailingstepshort*pipsize
    endif
    endif
    if barindex-tradeindex>1 then
    if longonmarket then
    if newsl>0 then
    sell at newsl stop
    endif
    if newsl>0 then
    if low 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 high crosses over newsl then
    exitshort at market
    endif
    endif
    endif
    endif
    mypositionprice = positionprice
    endif
    nonetheless thanked this post
    #148545 quote
    robertogozzi
    Moderator
    Master

    Paul solved the issue in case of accumulation of positions.

    As to sensitivity try using 0 with the old code and 1 with the updated one.

    If you are Long CLOSE-TRADEPRICE (or better POSITIONPRICE) could trigger a trailing stop, while LOW-TRADEPRICE could not, especially when a long body shows.

    nonetheless and Paul thanked this post
    #148550 quote
    nonetheless
    Participant
    Master

    That sensitivity had issues with the breakevenstop

    Thanks Paul, I think I had misunderstood how the sensitivity worked. The new code you posted above gives exactly the same backtest results as what I had so I’ll just have to watch how it behaves in live trading.👍

    I didn’t use  *sl though, just the values that I had been using. Do you set your TS as a % of the stoploss?

    #148552 quote
    nonetheless
    Participant
    Master

    Also, can this be used both with or without cumulative orders ?

    #148559 quote
    Paul
    Participant
    Master

    robertogozzi that could mean that variation (ts=3) cannot be used reliable? but explains that gains are sometimes higher in a backtest. I will have a closer look.

    nonetheless I ‘ve tested it with the old code tradeprice(1) and modifications for positionprice and the results are identical so it seems that this version is allround, for normale use & with cumulative orders.

    About your other question about % for the SL, yes in another strategy. I will post it. Hope you find it interesting and have some new ideas!

    nonetheless thanked this post
    #148560 quote
    robertogozzi
    Moderator
    Master

    Yes, with Paul‘s update it works in both cases.

    nonetheless thanked this post
    #148562 quote
    robertogozzi
    Moderator
    Master

    Well Paul, it can be used but it seems contrary to the principles of a trailing stop which is locking in some profits. If you are Long, computing the difference between CLOSE (or even HIGH) and POSITIONSIZE grants the highest profit is taken into consideration, while using LOW will compute the lowest profit reached (in some cases it may not be enough to trigger the start of the trailing).

    Option 3 may actually yield better gains, but this can be accomplished using CLOSE (or HIGH) with a higher TrailingStart, which makes more sense in my opinion, rather than setting a lower trailing start but using LOW to compute a smaller gain.

    nonetheless thanked this post
    #148574 quote
    Paul
    Participant
    Master

    I agree with all said. It’s an another parameter and not necessarily a very useful one, but the close can always be used. Thanks for the input!

    #148775 quote
    nonetheless
    Participant
    Master

    @Paul, what do you think of this as a typicalprice sensitivity option? I only tried it on one algo and after re-optimizing was able to get a slightly better result. Seems logical as that is the calculation I mostly use for MAs, so it should best represent the point where price has crossed a certain level.

    elsif ts2sensitivity = 4 then
    ts2sensitivitylong=(low+high+close)/3
    ts2sensitivityshort=(low+high+close)/3

    Could be a useful compromise in some cases … or maybe its just too many options for the sake of a few €  ???

    I didn’t try (low+high+close+open)/4

    #148789 quote
    Paul
    Participant
    Master

    It started with sometimes there was a long spike which closed much lower (long position) and the trailingstop wasn’t triggered and the market reversed. That’s why I added the option high for long/low for short instead of close, later I thought it couldn’t  hurt to add the opposite. In your case I should test it, and maybe you can test also long (high+high+close/3) but I think it’s a bit too much. If you plot the the trailingstop, you want to see quickly if it’s triggered or not and using such combinations it makes it a bit harder.

Viewing 15 posts - 1 through 15 (of 30 total)
  • You must be logged in to reply to this topic.

Trailing stop fails to move


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 29 replies,
has 4 voices, and was last updated by murre87
4 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/26/2020
Status: Active
Attachments: 3 files
Logo Logo
Loading...