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 2 years ago by murre87.
-
-
10/28/2020 at 2:46 PM #148790
Actually I’ve got another problem with that TS (as posted above). It moves to breakeven then stops, doesn’t trail with the price movement. Obviously it’s working correctly in backtest, but in live trading it gets stuck. Have you seen this as well?
10/28/2020 at 3:25 PM #148793no not yet, it never stops 😉 Your dj 5m mod v5 stopped suddenly with a strange message but I think unrelated to this.
On the 10s frame I use 2 trailingstops in demo and they seem to work alright, but because of using 2 I don’t know which was triggered. I will check.
Can you provide a pic?
10/28/2020 at 3:30 PM #14879510/28/2020 at 3:41 PM #148796I haven’t got a pic, when I realised it was stuck I closed the position manually. That was with NAS 5m Mod v4S
10/28/2020 at 3:57 PM #14879810/28/2020 at 6:22 PM #148806yeah, I’ll def check it out when I get a minute, it’s either that or go back to the old %TS I had been using. It’s a bit basic but does the job. The big advantage to yours is the separate values for long/short, so maybe I can build that in to the other one (of Nicolas)
10/28/2020 at 6:37 PM #148809Thought of another way if you take that trailingstop (it’s all based on the original)
Duplicate the code and make sure both not overlap each other.
1st one for long and remove all short code, and the second one for short with all long removed.
Should be the same effect (besides the accelerator and sensitivity) but still the advantage for separate values.
1 user thanked author for this post.
10/28/2020 at 6:52 PM #148811My thoughts exactly. Does this look right to you? Seems to test ok
12345678910111213141516171819202122232425262728293031323334353637383940414243444546//%trailing stop functiontrailingpercentlong = tst // %trailingpercentshort = tss // %stepPercentlong = ststepPercentshort = st2if onmarket thentrailingstartlong = tradeprice(1)*(trailingpercentlong/100) //trailing will start @trailingstart points profittrailingstartshort = tradeprice(1)*(trailingpercentshort/100) //trailing will start @trailingstart points profittrailingsteplong = tradeprice(1)*(stepPercentlong/100) //% step to move the stoplosstrailingstepshort = tradeprice(1)*(stepPercentshort/100) //% step to move the stoplossendif//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstartlong THENnewSL = tradeprice(1)+trailingsteplongENDIF//next movesIF newSL>0 AND close-newSL>trailingsteplong THENnewSL = newSL+trailingsteplongENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstartshort THENnewSL = tradeprice(1)-trailingstepshortENDIF//next movesIF newSL>0 AND newSL-close>trailingstepshort THENnewSL = newSL-trailingstepshortENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFNot sure about tradeprice(1), whereas you have positionprice. I know the difference, it’s more the (1) I’m unsure of
1 user thanked author for this post.
10/28/2020 at 7:03 PM #148814looks good. Tradeprice as written is correct. Only the reset is off when reversing a position, but most of the time the difference is smal to nothing.
2 users thanked author for this post.
10/28/2020 at 7:26 PM #148821Thanks Paul. I added
1sensitivity = (low+high+close)/3instead of close and the performance is back to where it was using your TS – at least in backtest it is. Let’s see how it runs live.
1 user thanked author for this post.
11/11/2020 at 6:31 PM #150172Hi @Paul, I’ve got more problems with this cumulative orders TS. This is the code I’m using:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889// %trailing stop function incl. cumulative positionsonce trailingstoptype = 1if trailingstoptype then//====================trailingpercentlong = 0.26 // %trailingpercentshort = 0.26 // %once acceleratorlong = 0.003 // [1] default; always > 0 (i.e. 0.5-3)once acceleratorshort= 0.023 // 1 = default; always > 0 (i.e. 0.5-3)ts2sensitivity = 4 // [1] default [2] hl [3] lh [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=(typicalprice)ts2sensitivityshort=(typicalprice)endifif 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 = positionpriceendifIt goes to breakeven exactly as expected but then stops, doesn’t trail. In the attached image, the breakeven was at the circled candle. 6 candles later the stop is still at 11835.2. the step is .003% so it should move .35 per candle = 11837. Any thoughts on this?
11/11/2020 at 6:57 PM #150176Ok, never mind – I just realized that acceleratorlong = .003 does not mean .003%
I forgot because the other TS I use has a straight % value for trailingstep
This one does trail but sooooo slowly – effectively just holds the position open with the hope of getting to the target. With a setting of .003 I might as well just use the breakeven code.
11/11/2020 at 7:29 PM #150180Hi Nonetheless good you found the reason.
I might still mention a few things.
Probably it misses *pointsize at the end, this is the part I changed
1234trailingstartlong = (tradeprice(countpos)*(trailingpercentlong/100))*pointsizetrailingstartshort = (tradeprice(countpos)*(trailingpercentshort/100))*pointsizetrailingsteplong = (tradeprice(countpos)*(steppercentlong/10000))*pointsizetrailingstepshort = (tradeprice(countpos)*(steppercentshort/10000))*pointsizeI also changed /100 to /10000 so steps are better manageable. Doesn’t change results, but the *pointsize might give different results on i.e 1 euro or 2 dollar DJ charts. (the countpos needs to be changed).
In the past I noticed that using if newsl>0 without defining longonmarket or shortonmarket sometimes, but not always, gave a few strange results, that why I at the end of the code had it specified.
Anyway for this part you could test 2 variations. Should have the same results.
12345678910if (shortonmarket and newsl > 0) or (longonmarket and newsl>0) thenif positioncount > positioncount[1] thenif longonmarket thennewsl = max(newsl,positionprice * newsl / mypositionprice)endifif shortonmarket thennewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifendifor
12345678910111213positioncount = abs(countofposition)if longonmarket and newsl > 0 thenif positioncount > positioncount[1] thennewsl = max(newsl,positionprice * newsl / mypositionprice)endifendifif shortonmarket and newsl > 0 thenif positioncount > positioncount[1] thennewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifMaybe you can keep above as backup if you still find strange things trading live.
1 user thanked author for this post.
11/11/2020 at 8:32 PM #150189That’s great, I’ll try both those options – thanks again!
09/17/2021 at 6:04 PM #177823 -
AuthorPosts
Find exclusive trading pro-tools on