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?
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.
Thanks Roberto, i’ll try that 👍👍👍
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
Yes, since at the beginning all started with a PRICE, not PIPS.
You can use GRAPH to monitor newsl, trailingstartlong, etc…
Actually NEWSL can be better monitored with GRAPHONPRICE.
thanked this post
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
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).
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.
😎
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.
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)
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.
I could only guess, as I could not replicate your trades without the code.
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 😁
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?