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.
The code is missing.
The above lines are just the settings and calculations, not the instructions that use them.
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
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.
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?
PaulParticipant
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
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.
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?
Also, can this be used both with or without cumulative orders ?
PaulParticipant
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!
Yes, with Paul‘s update it works in both cases.
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.
PaulParticipant
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!
@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
PaulParticipant
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.