trailing stop not working as expected
Forums › ProRealTime English forum › ProOrder support › trailing stop not working as expected
- This topic has 17 replies, 3 voices, and was last updated 3 years ago by Franro.
Tagged: atr, atr trailing stop
-
-
07/20/2021 at 9:26 AM #173848
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):
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586once trailingstoptype1= 1if 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)*acceleratorlongonce steppercentshort = (trailingpercentshort/10)*acceleratorshortif onmarket thentrailingstartlong = positionprice*(trailingpercentlong/100)trailingstartshort = positionprice*(trailingpercentshort/100)trailingsteplong = positionprice*(steppercentlong/100)trailingstepshort = positionprice*(steppercentshort/100)endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl = 0mypositionprice = 0endifpositioncount = abs(countofposition)if newsl > 0 thenif positioncount > positioncount[1] thenif longonmarket thennewsl = max(newsl,positionprice * newsl / mypositionprice)elsenewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifendifif ts2sensitivity=1 thents2sensitivitylong=closets2sensitivityshort=closeelsif ts2sensitivity=2 thents2sensitivitylong=hights2sensitivityshort=lowelsif ts2sensitivity=3 thents2sensitivitylong=lowts2sensitivityshort=highelsif ts2sensitivity=4 thents2sensitivitylong=typicalpricets2sensitivityshort=typicalpriceendifif longonmarket thenif newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong*pipsize thennewsl = positionprice+trailingsteplong*pipsizeendifif newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong*pipsize thennewsl = newsl+trailingsteplong*pipsizeendifendifif shortonmarket thenif newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort*pipsize thennewsl = positionprice-trailingstepshort*pipsizeendifif newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort*pipsize thennewsl = newsl-trailingstepshort*pipsizeendifendifif barindex-tradeindex>1 thenif longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif low crosses under newsl thensell at marketendifendifendifif shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif high crosses over newsl thenexitshort at marketendifendifendifendifmypositionprice = positionpriceendifAny thoughts on this?
07/20/2021 at 10:06 AM #173853Because 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.
07/20/2021 at 10:14 AM #173857Thanks Roberto, i’ll try that 👍👍👍
07/20/2021 at 10:21 AM #173858Just to clarify, I should remove *pipsize from this entire section (8 occurrences) ?
1234567891011121314if longonmarket thenif newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong*pipsize thennewsl = positionprice+trailingsteplong*pipsizeendifif newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong*pipsize thennewsl = newsl+trailingsteplong*pipsizeendifendifif shortonmarket thenif newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort*pipsize thennewsl = positionprice-trailingstepshort*pipsizeendifif newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort*pipsize thennewsl = newsl-trailingstepshort*pipsize07/20/2021 at 10:34 AM #173859Yes, since at the beginning all started with a PRICE, not PIPS.
07/20/2021 at 10:36 AM #173860You can use GRAPH to monitor newsl, trailingstartlong, etc…
Actually NEWSL can be better monitored with GRAPHONPRICE.
1 user thanked author for this post.
07/20/2021 at 10:45 AM #173861Ok, what about this one, should i remove *pointsize at lines 78, 80, 82 and 88, 90, 92 ?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117once trailingstopATR = 0if trailingstopATR = 1 then//====================once tsincrements = 0 // set to 0 to ignore tsincrementsonce tsminatrdist = 0once tsatrperiod = 14 // ts atr parameteronce tsminstop = 5 // ts minimum stop distancetssensitivity = 2 // 1 = close 2 = High/Low 3 = Low/High 4 = typicalprice (not use once)//====================if barindex=tradeindex thentrailingstoplong = 6 // ts atr distancetrailingstopshort = 6 // ts atr distanceelseif longonmarket thenif tsnewsl>0 thenif trailingstoplong>tsminatrdist thenif tsnewsl>tsnewsl[1] thentrailingstoplong=trailingstoplongelsetrailingstoplong=trailingstoplong-tsincrementsendifelsetrailingstoplong=tsminatrdistendifendifendifif shortonmarket thenif tsnewsl>0 thenif trailingstopshort>tsminatrdist thenif tsnewsl<tsnewsl[1] thentrailingstopshort=trailingstopshortelsetrailingstopshort=trailingstopshort-tsincrementsendifelsetrailingstopshort=tsminatrdistendifendifendifendiftsatr=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)) thentsmaxprice=0tsminprice=closetsnewsl=0mypositionpriceatr = 0endifpositioncountatr = abs(countofposition)if tsnewsl > 0 thenif positioncountatr > positioncountatr[1] thenif longonmarket thentsnewsl = max(tsnewsl,positionprice * tsnewsl / mypositionpriceatr)elsetsnewsl = min(tsnewsl,positionprice * tsnewsl / mypositionpriceatr)endifendifendifif tssensitivity=1 thentssensitivitylong=closetssensitivityshort=closeelsif tssensitivity=2 thentssensitivitylong=hightssensitivityshort=lowelsif tssensitivity=3 thentssensitivitylong=lowtssensitivityshort=highelsif tssensitivity=4 thentssensitivitylong=typicalpricetssensitivityshort=typicalpriceendifif longonmarket thentsmaxprice=max(tsmaxprice,tssensitivitylong)if tsmaxprice-positionprice>=tgl*pointsize thenif tsmaxprice-positionprice>=tsminstop thentsnewsl=tsmaxprice-tgl*pointsizeelsetsnewsl=tsmaxprice-tsminstop*pointsizeendifendifendifif shortonmarket thentsminprice=min(tsminprice,tssensitivityshort)if positionprice-tsminprice>=tgs*pointsize thenif positionprice-tsminprice>=tsminstop thentsnewsl=tsminprice+tgs*pointsizeelsetsnewsl=tsminprice+tsminstop*pointsizeendifendifendifif longonmarket thenif tsnewsl>0 thensell at tsnewsl stopendifif tsnewsl>0 thenif low crosses under tsnewsl thensell at market // when stop is rejectedendifendifendifif shortonmarket thenif tsnewsl>0 thenexitshort at tsnewsl stopendifif tsnewsl>0 thenif high crosses over tsnewsl thenexitshort at market // when stop is rejectedendifendifendifmypositionpriceatr = positionpriceendif07/20/2021 at 11:14 AM #173862Yes, 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).
1 user thanked author for this post.
07/20/2021 at 11:26 AM #173863Thanks 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.
😎
07/20/2021 at 12:51 PM #173870Actually 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.
07/20/2021 at 3:29 PM #173882I 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:
1234graph steppercentlongGraphOnPrice trailingstartlong + positionprice coloured(255,0,0,255)GraphOnPrice trailingstartlong + positionprice coloured(255,0,0,255)GraphOnPrice NewSL coloured(0,128,0,140)07/20/2021 at 4:02 PM #173888The 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.
07/20/2021 at 6:10 PM #173906I could only guess, as I could not replicate your trades without the code.
07/20/2021 at 7:26 PM #173918Non 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 😁
07/20/2021 at 7:50 PM #173920Actually 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?
-
AuthorPosts
Find exclusive trading pro-tools on