Realtime trailing-stop not consistent to backtest
Forums › ProRealTime English forum › ProOrder support › Realtime trailing-stop not consistent to backtest
- This topic has 17 replies, 4 voices, and was last updated 4 years ago by stefou102.
-
-
05/16/2019 at 11:53 AM #98603
The trailing stop I use is not consistent in real trading. I can’t figure out the reason, because sometimes it is and sometimes it isn’t! The backtest shows it correct.
The cause is the trailing stop and there’s no profit-target.
The question is, why is it selling at 0.35% sharp while that’s the level the trailing-stop should kick in? It works beautifully in the backtest, but it has to work realtime the same, all the time!
The “underlaying” only purpose is to make this code adjustable for other markets.
On 13 and 14th it worked correctly.
In the screenshots there are 2 different strategies and both executed at the same time or about.
1234567891011once enablets = 1 // trailing stoponce displayts = 1 // trailing stopts1=0.35ts2=0.30ts3=0.20switch =ts3+ts2switch2=ts2+ts1underlaying=100123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139if enablets thenif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentrailingstop1 = (tradeprice(1)/100)*ts1trailingstop2 = (tradeprice(1)/100)*ts2trailingstop3 = (tradeprice(1)/100)*ts3endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice1=0minprice1=closepriceexit1=0maxprice2=0minprice2=closepriceexit2=0maxprice3=0minprice3=closepriceexit3=0a1=0a2=0a3=0pp=0endifif longonmarket thenpp=((close/tradeprice(1))-1)*100if pp>=ts1 thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif shortonmarket thenpp=((close/tradeprice(1))-1)*-100if pp>=ts1 thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifendif//first leg longif longonmarket thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendif//first leg shortif shortonmarket thenminprice1=min(minprice1,close)if a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendif//2nd leg longif longonmarket thenmaxprice2=max(maxprice2,high)if a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendif//2nd leg shortif shortonmarket thenminprice2=min(minprice2,low)if a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendif//3rd leg longif longonmarket thenmaxprice3=max(maxprice3,high)if a3 thenif maxprice3-tradeprice(1)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendif//3rd leg shortif shortonmarket thenminprice3=min(minprice3,low)if a3 thenif tradeprice(1)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifendif//first leg exitif longonmarket and priceexit1>0thensell at priceexit1 stopendifif shortonmarket and priceexit1>0thenexitshort at priceexit1 stopendif//2nd leg exitif longonmarket and priceexit2>0thensell at priceexit2 stopendifif shortonmarket and priceexit2>0 thenexitshort at priceexit2 stopendif//3rd leg exitif longonmarket and priceexit3>0thensell at priceexit3 stopendifif shortonmarket and priceexit3>0 thenexitshort at priceexit3 stopendifif displayts thengraphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"graphonprice priceexit3 coloured(0,0,255,255) as "trailingstop3"endifendifAny idea’s?
wait… could the cause be there’s no space between > 0 and then?
priceexit1>0then
that the backtest still works correctly, but realtime doesn’t?
05/16/2019 at 12:17 PM #98608priceexit1>0then
Whenever I make that error (??then with no space) PRT flags up a syntax error message when I try to run backtest.
Also an ! box shows in the left hand margin next to the line with the error?
EDIT / PS
Ha … strangely I don’t get a syntax error or the ! box … I just tried your code above on my platform.
05/16/2019 at 12:27 PM #9861405/16/2019 at 12:43 PM #98616find & replace …same here! That’s strange indeed! and no error in backtest or setting it up live.
Most likely that’s the cause and at the moment i’am just a bit annoyed with myself for letting that happen! An error notice would’ve been nice in the backtest.
Strangly I can look so many times to a code and then only to notice it when posting here 🙂
05/16/2019 at 12:50 PM #98618Strangly I can look so many times to a code and then only to notice it when posting here
Yeah same here … for me it is partly because the code appears with much more clarity / contrast on the PRC webpage and I always feel like I have catteracts / cloudy vision on the PRT coding window.
All these failings will be sorted in version 11 ! ??
05/16/2019 at 12:58 PM #9861905/17/2019 at 8:02 AM #9869205/21/2019 at 11:30 AM #98986fixed the issue above, but again realtime is not consistent with the backtest for the trailing stop!
2 positions, different strategies about same time/level entry.
One strategy get’s out at the wrong level, but both have same trailing-stop.
In the backtest I have the same wrong result as realtime when I set settings to show only 25 units (everything lower than 10000 units ).
Only from 10000 bars or higher it shows the trailing-stop as intended.
Both strategy’s have
1defparam preloadbars = 10000Could it be that I’ve to increase that level?
05/21/2019 at 11:34 AM #9898805/21/2019 at 12:27 PM #9899905/21/2019 at 12:45 PM #99005yeah though the market continued to go up after 0.35% gain but it sold at 0.35% give or take (because of slippage/positionprice/spread).
I reworked the trailing stop a bit (same results in backtest), set the preloadbars to 50000 and post back after live results.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140// trailing stoponce enablets = 1 // trailing stoponce displayts = 1 // trailing stopts1=0.35ts2=0.30ts3=0.20switch =ts2+ts3switch2=ts1+ts2underlaying=100// underlaying security / index / forex// profittargets and stoploss have to match the lines// not to be optimized// 0.01 forex [i.e. gbpusd=0.01]// 1.00 securities [i.e. aapl=1 ;// 100.00 indexes [i.e. dax=100]// 100=xauusd// 100=cl us crudeif enablets thenif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentrailingstop1 = (tradeprice(1)/100)*ts1trailingstop2 = (tradeprice(1)/100)*ts2trailingstop3 = (tradeprice(1)/100)*ts3endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice1=0minprice1=closepriceexit1=0maxprice2=0minprice2=closepriceexit2=0maxprice3=0minprice3=closepriceexit3=0a1=0a2=0a3=0pp=0endifif onmarket thenpp=(positionperf*100)if pp>=ts1 thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifendif// setup longif longonmarket thenmaxprice1=max(maxprice1,close)maxprice2=max(maxprice2,high)maxprice3=max(maxprice3,high)if a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifif a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifif a3 thenif maxprice3-tradeprice(1)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendif// setup shortif shortonmarket thenminprice1=min(minprice1,close)minprice2=min(minprice2,low)minprice3=min(minprice3,low)if a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifif a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifif a3 thenif tradeprice(1)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifendif// exit longif longonmarket thenif priceexit1>0 thensell at priceexit1 stopendifif priceexit2>0 thensell at priceexit2 stopendifif priceexit3>0 thensell at priceexit3 stopendifendif// exit shortif shortonmarket thenif priceexit1>0 thenexitshort at priceexit1 stopendifif priceexit2>0 thenexitshort at priceexit2 stopendifif priceexit3>0 thenexitshort at priceexit3 stopendifendifif displayts thengraphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"graphonprice priceexit3 coloured(0,0,255,255) as "trailingstop3"endifendif05/22/2019 at 1:19 PM #99159Happend again and I don’t know how to fix. Backtest does it correct, live-trading does not.
Running two separate strategies version v1 and v2 which are identical besides exit-times on Friday and trailing-stop.
adjusted preloadbars to 50000.
v1 uses trailing-top 0.35% then 0.25% then 0.20% (switch to lower trailing-stop when gain > 0.45% and again when gain > 0.55%)
v2 uses trailing-top 0.40% then 0.30% then 0.20% (switch to lower trailing-stop when gain > 0.55% and again when gain > 0.60%)
I adjusted the entry-price in the backtest to match the price in live trading so the real trade performance is correct.
v1 exits when position-performance > 0.35% (time 11.26) and is sold at the close of next bar at time 11.27
v2 exits when position-performance > 0.40% (time 11.26) and is sold at the close of next bar at time 11.27 (this version had positive slippage that’s why it reached 0.40% at the same time as v1)
So one thing for sure. It’s directly related to the trailing stop.
Is this a coding issue or is IG handeling of the stops incorrect? Because I can’t see the reason why it exits immediately when the first performance gains criteria are reached!
05/24/2019 at 11:57 AM #9935205/28/2019 at 5:51 PM #99616Here’s the correction I made.
12345if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentrailingstop1 = (tradeprice(1)/100)*ts1trailingstop2 = (tradeprice(1)/100)*ts2trailingstop3 = (tradeprice(1)/100)*ts3endifis replaced by (or changed back to the original)
123trailingstop1 = (tradeprice(1)/100)*ts1trailingstop2 = (tradeprice(1)/100)*ts2trailingstop3 = (tradeprice(1)/100)*ts3The difference in the backtest is the handling of the first trade but only when that trade uses a trailing-stop!
The error became visible in live trading, the trade didn’t activate the proper trailing-stop but exited on the level the trailing-stop should start.
Besides this, it was missing some pointsize reference as below
1234if maxprice1-tradeprice(1)>=(trailingstop1)*pointsize thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifHopefully it’s all good from now.
05/28/2019 at 6:28 PM #99618 -
AuthorPosts
Find exclusive trading pro-tools on