Trailing stop fails to move
Forums › ProRealTime English forum › ProOrder support › Trailing stop fails to move
- This topic has 29 replies, 4 voices, and was last updated 4 years ago by
murre87.
-
-
10/26/2020 at 3:09 PM #148516
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
123456789101112//%trailing stop functiononce 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)*acceleratoronce steppercentshort = (trailingpercentshort/10)*accelerator2if onmarket thentrailingstartlong = 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.
10/26/2020 at 3:19 PM #148518The code is missing.
The above lines are just the settings and calculations, not the instructions that use them.
10/26/2020 at 3:41 PM #148524this is the full TS code. do you need to see the whole algo?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566once 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)*acceleratoronce steppercentshort = (trailingpercentshort/10)*accelerator2if onmarket thentrailingstartlong = tradeprice(1)*(trailingpercentlong/100)trailingstartshort = tradeprice(1)*(trailingpercentshort/100)trailingsteplong = tradeprice(1)*(steppercentlong/100)trailingstepshort = tradeprice(1)*(steppercentshort/100)endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif ts2sensitivity=1 thents2sensitivitylong=hights2sensitivityshort=lowelsif ts2sensitivity=2 thents2sensitivitylong=lowts2sensitivityshort=highelsets2sensitivitylong=closets2sensitivityshort=closeendifif longonmarket thenif newsl=0 and ts2sensitivitylong-tradeprice(1)>=trailingstartlong thennewsl = tradeprice(1)+trailingsteplongendifif newsl>0 and ts2sensitivitylong-newsl>trailingsteplong thennewsl = newsl+trailingsteplongendifendifif shortonmarket thenif newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort thennewsl = tradeprice(1)-trailingstepshortendifif newsl>0 and newsl-ts2sensitivityshort>trailingstepshort thennewsl = newsl-trailingstepshortendifendifif 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 marketendifendifendifendif10/26/2020 at 4:17 PM #148531It works fine for me, I think it can be due to your setting:
1ts2sensitivity = 2which 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.
10/26/2020 at 4:50 PM #148538Thanks 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?
1234if shortonmarket thenif newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort thennewsl = tradeprice(1)-trailingstepshortendifIf ts2sensitivityshort=high what does this pertain to? the high of what?
10/26/2020 at 4:58 PM #148540That 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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485// %trailing stop function incl. cumulative positionsonce trailingstoptype1= 1if 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)*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=highendifif 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 = positionpriceendif1 user thanked author for this post.
10/26/2020 at 5:18 PM #148545Paul 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.
2 users thanked author for this post.
10/26/2020 at 6:02 PM #148550That 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?
10/26/2020 at 6:15 PM #148552Also, can this be used both with or without cumulative orders ?
10/26/2020 at 6:29 PM #148559robertogozzi 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!
1 user thanked author for this post.
10/26/2020 at 6:30 PM #14856010/26/2020 at 6:38 PM #148562Well 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.
1 user thanked author for this post.
10/26/2020 at 8:11 PM #14857410/28/2020 at 1:24 PM #148775@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.
123elsif ts2sensitivity = 4 thents2sensitivitylong=(low+high+close)/3ts2sensitivityshort=(low+high+close)/3Could 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
10/28/2020 at 2:42 PM #148789It 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.
-
AuthorPosts
Find exclusive trading pro-tools on