YES, you can go ahead and paste the “trailing stop” code.
The result is different to the objetive, so i would like to ask you if you suggest a better prompt.
Trailing stop Code is the next one for 2 min TF:
// — 0. Variables
pp = (positionperf*100)
// — 1. Trailing stop
once enablets = 1 // activar trailing stop
// trailing stop
if shortonmarket then
ts1= 0.5 // es mayor que este porcentaje, entonces utiliza trailingstop ts1
switch= 0.55 // es mayor que este porcentaje, entonces utiliza trailingstop ts2
ts2= 0.35
switch2= 0.7 // es mayor que este porcentaje, entonces utiliza trailingstop ts3
ts3= 0.3
endif
if longonmarket then
ts1= 0.48//0.55//0.48 // es mayor que este porcentaje, entonces utiliza trailingstop ts1
switch=1.03//1.05//0.4 // es mayor que este porcentaje, entonces utiliza trailingstop ts2
ts2=0.35//0.85
switch2= 1.3//1.75 // es mayor que este porcentaje, entonces utiliza trailingstop ts3
ts3=0.25//0.2
endif
pp = (positionperf*100)
if enablets then
trailingstop1 = (tradeprice(1)/100)*ts1
trailingstop2 = (tradeprice(1)/100)*ts2
trailingstop3 = (tradeprice(1)/100)*ts3
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 onmarket then
if pp >= ts1 then
a1=1
endif
if pp >= switch then
a2=1
endif
if pp >= switch2 then
a3=1
endif
endif
// setup long
if longonmarket then
maxprice1=max(maxprice1,close)
maxprice2=max(maxprice2,high)
maxprice3=max(maxprice3,high)
if a1 then
if maxprice1-tradeprice(1)>=(trailingstop1)*pointsize then
priceexit1=maxprice1-(trailingstop1)*pointsize
endif
endif
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2)*pointsize then
priceexit2=maxprice2-(trailingstop2)*pointsize
endif
endif
if a3 then
if maxprice3-tradeprice(1)>=(trailingstop3)*pointsize then
priceexit3=maxprice3-(trailingstop3)*pointsize
endif
endif
endif
// setup short
if shortonmarket then
minprice1=min(minprice1,close)
minprice2=min(minprice2,low)
minprice3=min(minprice3,low)
if a1 then
if tradeprice(1)-minprice1>=(trailingstop1)*pointsize then
priceexit1=minprice1+(trailingstop1)*pointsize
endif
endif
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2)*pointsize then
priceexit2=minprice2+(trailingstop2)*pointsize
endif
endif
if a3 then
if tradeprice(1)-minprice3>=(trailingstop3)*pointsize then
priceexit3=minprice3+(trailingstop3)*pointsize
endif
endif
endif
// exit long
if longonmarket and priceexit1>0 then
sell at priceexit1 stop
endif
if longonmarket and priceexit2>0 then
sell at priceexit2 stop
endif
if longonmarket and priceexit3>0 then
sell at priceexit3 stop
endif
// exit short
if shortonmarket and priceexit1>0 then
exitshort at priceexit1 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
if shortonmarket and priceexit3>0 then
exitshort at priceexit3 stop
endif
endif
Many thanks.
Alfonso