trailing stop not working as expected

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

    The attached shows a position I have open at the moment on a 1 hour TF.

    Opened at 8:00 15209 with the trail to start after a high of 0.56% (15294.8)

    In the first bar it hits a high of 15301 and at the start of the 9:00 bar it should have moved to breakeven … but it didn’t.

    This is the trail that I’m using (courtesy of Paul):

    once trailingstoptype1= 1
    if trailingstoptype1 then
    //====================
    trailingpercentlong  = 0.56 // %
    trailingpercentshort = 0.49 // %
    once acceleratorlong = 0.08 // [1] default; always > 0 (i.e. 0.5-3)
    once acceleratorshort= 0.09 // 1 = default; always > 0 (i.e. 0.5-3)
    ts2sensitivity  = 2 // 1 = close 2 = High/Low 3 = Low/High 4 = typicalprice (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
    elsif ts2sensitivity=4 then
    ts2sensitivitylong=typicalprice
    ts2sensitivityshort=typicalprice
    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

    Any thoughts on this?

    DAX-1-hr.jpg DAX-1-hr.jpg
    #173853 quote
    robertogozzi
    Moderator
    Master

    Because in lines 13, 48 and 51 (the same for the Short direction) you are using a reference to a price (trailingstartlong), but in lines 48 and 51 you added *PIPSIZE, which is not correct, both are prices so no conversion is needed.

    Simply remove it.

    #173857 quote
    nonetheless
    Participant
    Master

    Thanks Roberto, i’ll try that 👍👍👍

    #173858 quote
    nonetheless
    Participant
    Master

    Just to clarify, I should remove *pipsize from this entire section (8 occurrences) ?

    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
    #173859 quote
    robertogozzi
    Moderator
    Master

    Yes, since at the beginning all started with a PRICE, not PIPS.

    #173860 quote
    robertogozzi
    Moderator
    Master

    You can use GRAPH to monitor newsl, trailingstartlong, etc…

    Actually NEWSL can be better monitored with GRAPHONPRICE.

    thanked this post
    #173861 quote
    nonetheless
    Participant
    Master

    Ok, what about this one, should i remove *pointsize at lines 78, 80, 82 and 88, 90, 92 ?

    once trailingstopATR  = 0
    if trailingstopATR = 1 then
    //====================
    once tsincrements = 0 // set to 0 to ignore tsincrements
    once tsminatrdist = 0
    
    once tsatrperiod    = 14 // ts atr parameter
    once tsminstop      = 5 // ts minimum stop distance
    
    tssensitivity = 2 // 1 = close 2 = High/Low 3 = Low/High 4 = typicalprice (not use once)
    //====================
    if barindex=tradeindex then
    trailingstoplong     = 6 // ts atr distance
    trailingstopshort    = 6 // ts atr distance
    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))/1000
    //tsatr=averagetruerange[tsatrperiod]((close/1)) // (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
    mypositionpriceatr = 0
    endif
    positioncountatr = abs(countofposition)
    if tsnewsl > 0 then
    if positioncountatr > positioncountatr[1] then
    if longonmarket then
    tsnewsl = max(tsnewsl,positionprice * tsnewsl / mypositionpriceatr)
    else
    tsnewsl = min(tsnewsl,positionprice * tsnewsl / mypositionpriceatr)
    endif
    endif
    endif
    if tssensitivity=1 then
    tssensitivitylong=close
    tssensitivityshort=close
    elsif tssensitivity=2 then
    tssensitivitylong=high
    tssensitivityshort=low
    elsif tssensitivity=3 then
    tssensitivitylong=low
    tssensitivityshort=high
    elsif tssensitivity=4 then
    tssensitivitylong=typicalprice
    tssensitivityshort=typicalprice
    endif
    if longonmarket then
    tsmaxprice=max(tsmaxprice,tssensitivitylong)
    if tsmaxprice-positionprice>=tgl*pointsize then
    if tsmaxprice-positionprice>=tsminstop then
    tsnewsl=tsmaxprice-tgl*pointsize
    else
    tsnewsl=tsmaxprice-tsminstop*pointsize
    endif
    endif
    endif
    if shortonmarket then
    tsminprice=min(tsminprice,tssensitivityshort)
    if positionprice-tsminprice>=tgs*pointsize then
    if positionprice-tsminprice>=tsminstop then
    tsnewsl=tsminprice+tgs*pointsize
    else
    tsnewsl=tsminprice+tsminstop*pointsize
    endif
    endif
    endif
    if longonmarket then
    if tsnewsl>0 then
    sell at tsnewsl stop
    endif
    if tsnewsl>0 then
    if low crosses under tsnewsl then
    sell at market // when stop is rejected
    endif
    endif
    endif
    if shortonmarket then
    if tsnewsl>0 then
    exitshort at tsnewsl stop
    endif
    if tsnewsl>0 then
    if high crosses over tsnewsl then
    exitshort at market // when stop is rejected
    endif
    endif
    endif
    mypositionpriceatr = positionprice
    endif
    #173862 quote
    robertogozzi
    Moderator
    Master

    Yes, because tsatr is a price (range) as well as all the other variables.

    The code is based on PRICE, not on PIPS, so all that refers to pips is incorrect.

    Lines 13 and 14 are a bit subtle, they are set as prices (as from their comments), but that is fine for indices where the price-to-pips ratio is 1:1 (Dax, SP, ….) but when used with FX pirs it wouldn’t be correct. In This case you should multiply 6 by PIPSIZE to make sure the numeric constant is converted to a price (divide by PIPSIZE, or POINTSIZE, the other way round that is when converting a price to pips, instead).

    nonetheless thanked this post
    #173863 quote
    nonetheless
    Participant
    Master

    Thanks loads Roberto, I think that explains a lot of odd behaviour. I never trade Forex so I’ll leave lines 13, 14 as they are.

    😎

    #173870 quote
    robertogozzi
    Moderator
    Master

    Actually if you’re using it with Dax, PIPSIZE should make no difference.

    I need to test it more, to monitor HIGH and LOW more in depth.

    #173882 quote
    robertogozzi
    Moderator
    Master

    I think it’s because it takes one candle for any system status constant to be updated, so the HIGH at 08:00 could not be detected, while the high at 09:00 was.

    From your pic I cannot see any detail and I could not replicate your trades, but I think this can be the sole reason.

    I suggest that you add these lines to your code, to better debug it:

    graph        steppercentlong
    GraphOnPrice trailingstartlong + positionprice coloured(255,0,0,255)
    GraphOnPrice trailingstartlong + positionprice coloured(255,0,0,255)
    GraphOnPrice NewSL                             coloured(0,128,0,140)
    #173888 quote
    nonetheless
    Participant
    Master

    The high of 15301.5 was hit at 08:14 (having opened at 08:00).

    Running a backtest (having removed *pipsize) shows a loss for that trade, but it has the entry at 15215.7

    The graph shows “trailingstartlong + positionprice = 15300.9” which would be correct (15215.7 + .56%) and allowing for spread, the high of 15301.5 doesn’t clear it.

    But that doesn’t explain why my live trade (actually 15209.6) failed to breakeven after a high of 15294.7

    With or without *pipsize seems to make no difference to backtests.

    #173906 quote
    robertogozzi
    Moderator
    Master

    I could only guess, as I could not replicate your trades without the code.

    #173918 quote
    nonetheless
    Participant
    Master

    Non fa niente, I’ve decided not to continue with that algo anyway. The other places I’m using it seem to work fine – all shorter TFs, 2m, 5m etc, so may be just a blip with the 1 hour TF.

    Maybe by the time it got to the next candle it had ‘forgotten’ that the high had been reached 😁

    #173920 quote
    deleted23092025
    Participant
    New

    Actually this happend to me with the same trail code. I got the same algo on different accounts and the trailing did move on one account but not the other. The price went the profitable way and it still did not move. Then the position got held over the weekend and when markets  reopened on monday the algo updated its stop on both accounts. I dont think there was a visual bug (never happend before atleast?). It was with the *pointsize trailing.

    This has happend 2 times before (this time explained here and one more time). So if I remove *pointsize the issue should be solved?

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

trailing stop not working as expected


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 17 replies,
has 3 voices, and was last updated by deleted23092025
4 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/20/2021
Status: Active
Attachments: 1 files
Logo Logo
Loading...