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.
once enablets = 1 // trailing stop
once displayts = 1 // trailing stop
ts1=0.35
ts2=0.30
ts3=0.20
switch =ts3+ts2
switch2=ts2+ts1
underlaying=100
if enablets then
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
trailingstop1 = (tradeprice(1)/100)*ts1
trailingstop2 = (tradeprice(1)/100)*ts2
trailingstop3 = (tradeprice(1)/100)*ts3
endif
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice1=0
minprice1=close
priceexit1=0
maxprice2=0
minprice2=close
priceexit2=0
maxprice3=0
minprice3=close
priceexit3=0
a1=0
a2=0
a3=0
pp=0
endif
if longonmarket then
pp=((close/tradeprice(1))-1)*100
if pp>=ts1 then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif shortonmarket then
pp=((close/tradeprice(1))-1)*-100
if pp>=ts1 then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
endif
//first leg long
if longonmarket then
maxprice1=max(maxprice1,close)
if a1 then
if maxprice1-tradeprice(1)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
//first leg short
if shortonmarket then
minprice1=min(minprice1,close)
if a1 then
if tradeprice(1)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
//2nd leg long
if longonmarket then
maxprice2=max(maxprice2,high)
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
//2nd leg short
if shortonmarket then
minprice2=min(minprice2,low)
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
//3rd leg long
if longonmarket then
maxprice3=max(maxprice3,high)
if a3 then
if maxprice3-tradeprice(1)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
//3rd leg short
if shortonmarket then
minprice3=min(minprice3,low)
if a3 then
if tradeprice(1)-minprice3>=(trailingstop3) then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
//first leg exit
if longonmarket and priceexit1>0then
sell at priceexit1 stop
endif
if shortonmarket and priceexit1>0then
exitshort at priceexit1 stop
endif
//2nd leg exit
if longonmarket and priceexit2>0then
sell at priceexit2 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
//3rd leg exit
if longonmarket and priceexit3>0then
sell at priceexit3 stop
endif
if shortonmarket and priceexit3>0 then
exitshort at priceexit3 stop
endif
if displayts then
graphonprice 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"
endif
endif
Any 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?