ProRealCode - Trading & Coding with ProRealTime™
Use breakeven- en trailingstop on different securities & indexes & forex
I want it to work well in every scenario, regardless of forex, shares etc.
So i’am testing one variable extra and called it underlaying.
It seems I got it to work for SL & PT & BE.
Underlaying1 is not meant to optimize. It’s either 0.01 0.1 1 10 100 depending on forex, index etc.
With the right value the lines match the SL and PT. If that’s correct the BE value is correct too.
i.e. use for gdpusd underlaying1=0.01 but for the dax underlaying1=100 and for shares 1.
And so far no need to touch underlaying2
I’am open for suggestions, for a cleaner code and a solution for the TS.
// test trailingstop and breakeven on the dax
defparam cumulateorders = false
once enableSL = 1 // stop loss
once enablePT = 1 // profit target
once enableTS = 0 // trailing stop
once enableBE = 1 // breakeven stop
once displaySL = 1 // stop loss
once displayPT = 1 // profit target
once displayTS = 0 // trailing stop
once displayBE = 1 // breakeven stop
SL = 0.75 // % stop loss
PT = 0.50 // % profit target
TS = 0.35 // % trailing stop
BESG = 0.25 // % break even stop gain
BESL = 0.00 // % break even stop level
// underlaying security / index / forex
// profittargets and stoploss have to match the lines
// 0.01 FOREX [i.e. GBPUSD=0.01]
// 1.00 SECURITIES [i.e. aapl=1 ;
// 100.00 INDEXES [i.e. dax=100]
// XAUUSD = 100
// CL US Crude = 100
underlaying1=1 // SL & PT
underlaying2=underlaying1/100 // BE [ don't change]
//underlaying3=1
// reset at start
if intradaybarindex=0 then
longtradecounter=0
shorttradecounter=0
endif
pclong = longtradecounter<1
pcshort = shorttradecounter<1
shortma = average[sm](close)
longma = average[lm](close)
// conditions to enter long positions
l1 = (shortma crosses over longma)
// conditions to enter short
s1 = (shortma crosses under longma)
if pclong and l1 then
buy 1 contract at market
longtradecounter=longtradecounter+1
endif
if pcshort and s1 then
sellshort 1 contract at market
shorttradecounter=shorttradecounter+1
endif
// trailing stop
if enableTS then
trailingstop = (tradeprice/100)*TS*pointsize
if not onmarket then
maxprice=0
minprice=close
priceexit=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice=0
minprice=close
priceexit=0
endif
if longonmarket then
maxprice=max(maxprice,close)
if maxprice-tradeprice(1)>=trailingstop*pointsize then
priceexit=maxprice-trailingstop*pointsize
endif
endif
if shortonmarket then
minprice=min(minprice,close)
if tradeprice(1)-minprice>=trailingstop*pointsize then
priceexit=minprice+trailingstop*pointsize
endif
endif
if longonmarket and priceexit>0 then
sell at priceexit stop
endif
if shortonmarket and priceexit>0 then
exitshort at priceexit stop
endif
if displayTS then
graphonprice priceexit coloured(0,0,255,255) as "trailingstop"
endif
endif
// break even stop
if enableBE then
if not onmarket then
newsl=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl=0
endif
if longonmarket then
if close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/underlaying2)*pointsize then
newsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/underlaying2)*pointsize
endif
endif
if shortonmarket then
if tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/underlaying2)*pointsize then
newsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/underlaying2)*pointsize
endif
endif
if longonmarket and newsl>0 then
sell at newsl stop
endif
if shortonmarket and newsl>0 then
exitshort at newsl stop
endif
if displayBE then
graphonprice newsl coloured(244,102,27,255) as "breakevenstop"
endif
endif
// to set & display profittarget
if enablePT then
set target %profit PT
if displaypt then
if not onmarket then
ptarget=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
ptarget=0
endif
if onmarket then
if longonmarket then
ptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying1)*pointsize
endif
if shortonmarket then
ptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying1)*pointsize
endif
endif
graphonprice ptarget coloured(121,141,35,255) as "profittarget"
endif
endif
// to set & display stoploss
if enableSL then
set stop %loss SL
if displaysl then
if not onmarket then
sloss=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
sloss=0
endif
if onmarket then
if longonmarket then
sloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying1)*pointsize
endif
if shortonmarket then
sloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying1)*pointsize
endif
endif
graphonprice sloss coloured(255,0,0,255) as "stoploss"
endif
endif
graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"
Finished code! Case closed
// test trailingstop and breakeven on the dax
defparam cumulateorders = false
once enableSL = 1 // stop loss
once enablePT = 1 // profit target
once enableTS = 1 // trailing stop
once enableBE = 1 // breakeven stop
once displaySL = 1 // stop loss
once displayPT = 1 // profit target
once displayTS = 1 // trailing stop
once displayBE = 1 // breakeven stop
SL = 0.75 // % stop loss
PT = 1.50 // % profit target
TS = 0.25 // % trailing stop
BESG = 0.25 // % break even stop gain
BESL = 0.00 // % break even stop level
// underlaying security / index / forex
// profittargets and stoploss have to match the lines
// 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 Crude
// DAX=100
underlaying=100
// reset at start
if intradaybarindex=0 then
longtradecounter=0
shorttradecounter=0
endif
pclong = longtradecounter<1
pcshort = shorttradecounter<1
shortma = average[sm](close)
longma = average[lm](close)
// conditions to enter long positions
l1 = (shortma crosses over longma)
// conditions to enter short
s1 = (shortma crosses under longma)
if pclong and l1 then
buy 1 contract at market
longtradecounter=longtradecounter+1
endif
if pcshort and s1 then
sellshort 1 contract at market
shorttradecounter=shorttradecounter+1
endif
// trailing stop
if enableTS then
trailingstop = (tradeprice/100)*TS
if not onmarket then
maxprice=0
minprice=close
priceexit=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice=0
minprice=close
priceexit=0
endif
if longonmarket then
maxprice=max(maxprice,close)
if maxprice-tradeprice(1)>=(trailingstop) then
priceexit=maxprice-(trailingstop/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
minprice=min(minprice,close)
if tradeprice(1)-minprice>=(trailingstop) then
priceexit=minprice+(trailingstop/(underlaying/100))*pointsize
endif
endif
if longonmarket and priceexit>0 then
sell at priceexit stop
endif
if shortonmarket and priceexit>0 then
exitshort at priceexit stop
endif
if displayTS then
graphonprice priceexit coloured(0,0,255,255) as "trailingstop"
endif
endif
// break even stop
if enableBE then
if not onmarket then
newsl=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl=0
endif
if longonmarket then
if close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then
newsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
if tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then
newsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize
endif
endif
if longonmarket and newsl>0 then
sell at newsl stop
endif
if shortonmarket and newsl>0 then
exitshort at newsl stop
endif
if displayBE then
graphonprice newsl coloured(244,102,27,255) as "breakevenstop"
endif
endif
// to set & display profittarget
if enablePT then
set target %profit PT
if displaypt then
if not onmarket then
ptarget=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
ptarget=0
endif
if onmarket then
if longonmarket then
ptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying)*pointsize
endif
if shortonmarket then
ptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying)*pointsize
endif
endif
graphonprice ptarget coloured(121,141,35,255) as "profittarget"
endif
endif
// to set & display stoploss
if enableSL then
set stop %loss SL
if displaysl then
if not onmarket then
sloss=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
sloss=0
endif
if onmarket then
if longonmarket then
sloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying)*pointsize
endif
if shortonmarket then
sloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying)*pointsize
endif
endif
graphonprice sloss coloured(255,0,0,255) as "stoploss"
endif
endif
graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"
Just an addition.
// trailing stop
if enablets then
ts1=0.33
ts2=0.16
switch=0.45
// i.e. from 0.33% to 0.45% trade performance it uses a 0.33% trailing stop
// from 0.45% and higher it uses a 0.16% trailing stop
trailingstop1 = (tradeprice/100)*ts1
trailingstop2 = (tradeprice/100)*ts2
if not onmarket then
maxprice1=0
minprice1=close
priceexit1=0
maxprice2=0
minprice2=close
priceexit2=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice1=0
minprice1=close
priceexit1=0
maxprice2=0
minprice2=close
priceexit2=0
endif
a1 = (positionperf*100)<=switch
a2 = (positionperf*100)>switch
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
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
if longonmarket then
maxprice2=max(maxprice2,close)
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
if shortonmarket then
minprice2=min(minprice2,close)
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
If longonmarket and priceexit1>0 then
sell at priceexit1 stop
endif
if shortonmarket and priceexit1>0 then
exitshort at priceexit1 stop
endif
If longonmarket and priceexit2>0 then
sell at priceexit2 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
if displayts then
graphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"
graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"
endif
endif
Too much work went into this so better store it here.
3 Level trailing stop and manage 2 positions in the same direction.
// trailing stop
if enablets then
ts1=0.35
ts2=0.25
ts3=0.20
switch =ts3+ts2 //0.45-0.60 switch to 25% trailing stop
switch2=ts2+ts1 //0.60 and higher switch to 20% trailing stop
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
elsif (COUNTOFLONGSHARES=2 or COUNTOFSHORTSHARES=2) then
trailingstop1 = (tradeprice(2)/100)*ts1
trailingstop2 = (tradeprice(2)/100)*ts2
trailingstop3 = (tradeprice(2)/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
pp1=0
pp2=0
endif
if COUNTOFLONGSHARES=1 then
pp1=((close/tradeprice(1))-1)*100
if pp1<switch then
a1=1
endif
if pp1>=switch then
a2=1
endif
if pp1>=switch2 then
a3=1
endif
elsif COUNTOFLONGSHARES=2 then
pp1=((close/tradeprice(2))-1)*100
pp2=((close/tradeprice(1))-1)*100
if pp1<switch then
a1=1
endif
if pp1>=switch then
a2=1
endif
if pp1>=switch2 then
a3=1
endif
endif
if COUNTOFSHORTSHARES=1 then
pp1=((close/tradeprice(1))-1)*-100
if pp1<switch then
a1=1
endif
if pp1>=switch then
a2=1
endif
if pp1>=switch2 then
a3=1
endif
elsif COUNTOFSHORTSHARES=2 then
pp1=((close/tradeprice(2))-1)*-100
pp2=((close/tradeprice(1))-1)*-100
if pp1<switch then
a1=1
endif
if pp1>=switch then
a2=1
endif
if pp1>=switch2 then
a3=1
endif
endif
//first leg
if longonmarket then
maxprice1=max(maxprice1,close)
if COUNTOFLONGSHARES=1 and a1 then
if maxprice1-tradeprice(1)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
If countoflongshares=2 and a1 then
if maxprice1-tradeprice(2)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
If shortonmarket then
minprice1=min(minprice1,close)
if countofshortshares=1 and a1 then
if tradeprice(1)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
if countofshortshares=2 and a1 then
if tradeprice(2)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
//2nd leg
if longonmarket then
maxprice2=max(maxprice2,high)
if COUNTOFLONGSHARES=1 and a2 then
if maxprice2-tradeprice(1)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
if countoflongshares=2 and a2 then
if maxprice2-tradeprice(2)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
If shortonmarket then
minprice2=min(minprice2,low)
if countofshortshares=1 and a2 then
if tradeprice(1)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
If countofshortshares=2 and a2 then
if tradeprice(2)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
//3rd leg
if longonmarket then
maxprice3=max(maxprice3,high)
if COUNTOFLONGSHARES=1 and a3 then
if maxprice3-tradeprice(1)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
If countoflongshares=2 and a3 then
if maxprice3-tradeprice(2)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
If shortonmarket then
minprice3=min(minprice3,low)
if countofshortshares=1 and a3 then
if tradeprice(1)-minprice3>=(trailingstop3) then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
if countofshortshares=2 and a3 then
if tradeprice(2)-minprice3>=(trailingstop3) then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
If longonmarket and priceexit1>0 then
sell at priceexit1 stop
endif
if shortonmarket and priceexit1>0 then
exitshort at priceexit1 stop
endif
If longonmarket and priceexit2>0 then
sell at priceexit2 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
If longonmarket and priceexit3>0 then
sell at priceexit3 stop
endif
if shortonmarket and priceexit3>0 then
exitshort at priceexit3 stop
endif
endif
Just a bit more info on the above code;
If the first position (long i.e.) goes down and a second position is added, it has the same exit trailing stop strategy as the first trade.
Resulting in a smaller loss when hitting stoploss (not included here) and a greater result when the 1st position reaches the trailing stop criteria.
1st leg 0.35% gain to 0.45% and trailing-stop 0.35% is activated and uses default close
2nd leg 0.45% gain to 0.60% and trailing-stop 0.25% is activated and uses high/low instead of close for better results
3rd leg 0.60% gain or higher and trailing-stop 0.20% is activated and uses high/low instead of close for better results
the switch levels are calculated from the trailing-stop values to reduce parameters.
update trailing-stop for 2 positions
// trailing stop
if enablets then
ts1=0.35
ts2=0.30
ts3=0.20
switch =ts3+ts2 //0.50-0.65 switch to 30% trailing stop
switch2=ts2+ts1 //0.65 and higher switch to 20% trailing stop
underlaying=100
tn=1 //use trailing stop on x tradenumber
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
elsif (COUNTOFLONGSHARES=2 or COUNTOFSHORTSHARES=2) then
trailingstop1 = (tradeprice(tn)/100)*ts1
trailingstop2 = (tradeprice(tn)/100)*ts2
trailingstop3 = (tradeprice(tn)/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 COUNTOFLONGSHARES=1 then
pp=((close/tradeprice(1))-1)*100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif COUNTOFSHORTSHARES=1 then
pp=((close/tradeprice(1))-1)*-100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif COUNTOFLONGSHARES=2 then
pp=((close/tradeprice(tn))-1)*100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif COUNTOFSHORTSHARES=1 then
pp=((close/tradeprice(1))-1)*-100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif COUNTOFSHORTSHARES=2 then
pp=((close/tradeprice(tn))-1)*-100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
endif
//first leg
if COUNTOFLONGSHARES=1 then
maxprice1=max(maxprice1,close)
if a1 then
if maxprice1-tradeprice(1)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
If countoflongshares=2 then
maxprice1=max(maxprice1,close)
if a1 then
if maxprice1-tradeprice(tn)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
If countofshortshares=1 then
minprice1=min(minprice1,close)
if a1 then
if tradeprice(1)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
if countofshortshares=2 then
minprice1=min(minprice1,close)
if a1 then
if tradeprice(tn)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
//2nd leg
if COUNTOFLONGSHARES=1 then
maxprice2=max(maxprice2,high)
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
if countoflongshares=2 then
maxprice2=max(maxprice2,high)
if a2 then
if maxprice2-tradeprice(tn)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
if countofshortshares=1 then
minprice2=min(minprice2,low)
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
If countofshortshares=2 then
minprice2=min(minprice2,low)
if a2 then
if tradeprice(tn)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
//3rd leg
if countoflongshares=1 then
maxprice3=max(maxprice3,high)
if a3 then
if maxprice3-tradeprice(1)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
If countoflongshares=2 then
maxprice3=max(maxprice3,high)
if a3 then
if maxprice3-tradeprice(tn)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
If countofshortshares=1 then
minprice3=min(minprice3,low)
if a3 then
if tradeprice(1)-minprice3>=(trailingstop3) then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
if countofshortshares=2 then
minprice3=min(minprice3,low)
if a3 then
if tradeprice(tn)-minprice3>=(trailingstop3) then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
If longonmarket and priceexit1>0 then
sell at priceexit1 stop
endif
if shortonmarket and priceexit1>0 then
exitshort at priceexit1 stop
endif
If longonmarket and priceexit2>0 then
sell at priceexit2 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
If longonmarket and priceexit3>0 then
sell at priceexit3 stop
endif
if shortonmarket and priceexit3>0 then
exitshort at priceexit3 stop
endif
endif
update stoploss for 2 positions
// set stoploss
if enablesl then
sl=1.00 //max loss in %
underlaying=100
if not onmarket then
sloss=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
sloss=0
elsif countoflongshares>1 or countofshortshares>1 then
sloss=0
endif
if countoflongshares=1 then
sloss=tradeprice(1)-((tradeprice(1)*sl)/underlaying)*pointsize
elsif countofshortshares=1 then
sloss=tradeprice(1)+((tradeprice(1)*sl)/underlaying)*pointsize
elsif countoflongshares=2 then
sloss=tradeprice(2)-((tradeprice(2)*sl)/underlaying)*pointsize
elsif countofshortshares=2 then
sloss=tradeprice(2)+((tradeprice(2)*sl)/underlaying)*pointsize
endif
if countoflongshares=1 then
if low <= sloss then
sell at sloss stop
endif
elsif countofshortshares=1 then
if high >= sloss then
exitshort at sloss stop
endif
elsif countoflongshares=2 then
if low <= sloss then
sell at sloss stop
endif
elsif countofshortshares=2 then
if high >= sloss then
exitshort at sloss stop
endif
endif
if displaysl then
graphonprice sloss coloured(255,0,0,255) as "stoploss"
endif
endif
1st
once enablesl = 1 // stop loss
once enablept = 0 // profit target
once enablets = 1 // trailing stop
once displaysl = 1 // stop loss
once displaypt = 1 // profit target
once displayts = 1 // trailing stop
2nd
sl = 1 // % stoploss
pt = 1 // % profit target
ts1=0.35
ts2=0.30
ts3=0.20
// use trailingstop / profittarget / stoploss based on tradenumber [1=lastest / 2=first trade]
tnt=1
tnp=1
tns=2 //first trade !
switch =ts3+ts2 //0.50-0.65 switch to 30% trailing stop
switch2=ts2+ts1 //0.65 and higher switch to 20% trailing stop
underlaying=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 crude
3rd
// trailing stop
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
elsif (countoflongshares=2 or countofshortshares=2) then
trailingstop1 = (tradeprice(tnt)/100)*ts1
trailingstop2 = (tradeprice(tnt)/100)*ts2
trailingstop3 = (tradeprice(tnt)/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 countoflongshares=1 then
pp=((close/tradeprice(1))-1)*100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif countofshortshares=1 then
pp=((close/tradeprice(1))-1)*-100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif countoflongshares=2 then
pp=((close/tradeprice(tnt))-1)*100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
elsif countofshortshares=2 then
pp=((close/tradeprice(tnt))-1)*-100
if pp>=ts1 and pp<switch then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
endif
//first leg long
if countoflongshares=1 then
maxprice1=max(maxprice1,close)
if a1 then
if maxprice1-tradeprice(1)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
if countoflongshares=2 then
maxprice1=max(maxprice1,close)
if a1 then
if maxprice1-tradeprice(tnt)>=(trailingstop1) then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
//first leg short
if countofshortshares=1 then
minprice1=min(minprice1,close)
if a1 then
if tradeprice(1)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
if countofshortshares=2 then
minprice1=min(minprice1,close)
if a1 then
if tradeprice(tnt)-minprice1>=(trailingstop1) then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
endif
//2nd leg long
if countoflongshares=1 then
maxprice2=max(maxprice2,high)
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
if countoflongshares=2 then
maxprice2=max(maxprice2,high)
if a2 then
if maxprice2-tradeprice(tnt)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
//2nd leg short
if countofshortshares=1 then
minprice2=min(minprice2,low)
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
if countofshortshares=2 then
minprice2=min(minprice2,low)
if a2 then
if tradeprice(tnt)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
//3rd leg long
if countoflongshares=1 then
maxprice3=max(maxprice3,high)
if a3 then
if maxprice3-tradeprice(1)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
if countoflongshares=2 then
maxprice3=max(maxprice3,high)
if a3 then
if maxprice3-tradeprice(tnt)>=(trailingstop3) then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
//3rd leg short
if countofshortshares=1 then
minprice3=min(minprice3,low)
if a3 then
if tradeprice(1)-minprice3>=(trailingstop3) then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
if countofshortshares=2 then
minprice3=min(minprice3,low)
if a3 then
if tradeprice(tnt)-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
// set stoploss
if enablesl then
if not onmarket then
sloss=0
elsif countoflongshares=1 then
sloss=tradeprice(1)-((tradeprice(1)*sl)/underlaying)*pointsize
elsif countofshortshares=1 then
sloss=tradeprice(1)+((tradeprice(1)*sl)/underlaying)*pointsize
elsif countoflongshares=2 then
sloss=tradeprice(tns)-((tradeprice(tns)*sl)/underlaying)*pointsize
elsif countofshortshares=2 then
sloss=tradeprice(tns)+((tradeprice(tns)*sl)/underlaying)*pointsize
endif
if longonmarket and low <= sloss then
sell at sloss stop
elsif shortonmarket and high >= sloss then
exitshort at sloss stop
endif
if displaysl then
graphonprice sloss coloured(255,0,0,255) as "stoploss"
endif
endif
// to display profittarget
if enablept then
if not onmarket then
ptarget=0
elsif countoflongshares=1 then
ptarget=tradeprice(1)+((tradeprice(1)*pt)/underlaying)*pointsize
elsif countofshortshares=1 then
ptarget=tradeprice(1)-((tradeprice(1)*pt)/underlaying)*pointsize
elsif countoflongshares=2 then
ptarget=tradeprice(tnp)+((tradeprice(tnp)*pt)/underlaying)*pointsize
elsif countofshortshares=2 then
ptarget=tradeprice(tnp)-((tradeprice(tnp)*pt)/underlaying)*pointsize
endif
if longonmarket and high>=ptarget then
sell at ptarget stop
elsif shortonmarket and low<=ptarget then
exitshort at ptarget stop
endif
if displaypt then
graphonprice ptarget coloured(121,141,35,255) as "profittarget"
endif
endif
just some comparison test
1% stoploss, trailing stop and no profit target
running 2 positions everything the same
VS
1 position and adding another one only when 1st reached -.20% loss; stoploss both positions based on first position and trailing stop based on second position
Bit less profit, but less drawdown, better gain/loss ratio and balanced average gain of winning and losing trades.
Sounds great Paul, thank you for your time and skills and for sharing!
Please forgive the daft question, but what is the 1st, 2nd and 3rd … anybody?
there’s nothing to it. Some things on top, below and on the bottom in the strategy.
With a trailing stop and stoploss working nicely, the only thing left is a good entry 🙂
// test trailingstop and breakeven on the dax
defparam cumulateorders = false
once enableSL = 1 // stop loss
once enablePT = 1 // profit target
once enableTS = 1 // trailing stop
once enableBE = 1 // breakeven stop
once displaySL = 1 // stop loss
once displayPT = 1 // profit target
once displayTS = 1 // trailing stop
once displayBE = 1 // breakeven stop
SL = 0.75 // % stop loss
PT = 1.50 // % profit target
TS = 0.25 // % trailing stop
BESG = 0.25 // % break even stop gain
BESL = 0.00 // % break even stop level
SM = 186 //170
LM = 36 //20
// underlaying security / index / forex
// profittargets and stoploss have to match the lines
// 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 Crude
// DAX=100
underlaying=100
// reset at start
if intradaybarindex=0 then
longtradecounter=0
shorttradecounter=0
endif
pclong = longtradecounter<1
pcshort = shorttradecounter<1
shortma = average[sm](close)
longma = average[lm](close)
// conditions to enter long positions
l1 = (shortma crosses over longma)
// conditions to enter short
s1 = (shortma crosses under longma)
if pclong and l1 then
buy 1 contract at market
longtradecounter=longtradecounter+1
endif
if pcshort and s1 then
sellshort 1 contract at market
shorttradecounter=shorttradecounter+1
endif
// trailing stop
if enableTS then
trailingstop = (tradeprice/100)*TS
if not onmarket then
maxprice=0
minprice=close
priceexit=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice=0
minprice=close
priceexit=0
endif
if longonmarket then
maxprice=max(maxprice,close)
if maxprice-tradeprice(1)>=(trailingstop) then
priceexit=maxprice-(trailingstop/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
minprice=min(minprice,close)
if tradeprice(1)-minprice>=(trailingstop) then
priceexit=minprice+(trailingstop/(underlaying/100))*pointsize
endif
endif
if longonmarket and priceexit>0 then
sell at priceexit stop
endif
if shortonmarket and priceexit>0 then
exitshort at priceexit stop
endif
//if displayTS then
//graphonprice priceexit coloured(0,0,255,255) as "trailingstop"
//endif
//endif
// break even stop
if enableBE then
if not onmarket then
newsl=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl=0
endif
if longonmarket then
if close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then
newsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
if tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then
newsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize
endif
endif
if longonmarket and newsl>0 then
sell at newsl stop
endif
if shortonmarket and newsl>0 then
exitshort at newsl stop
endif
//if displayBE then
//graphonprice newsl coloured(244,102,27,255) as "breakevenstop"
//endif
//endif
// to set & display profittarget
if enablePT then
set target %profit PT
if displaypt then
if not onmarket then
ptarget=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
ptarget=0
endif
if onmarket then
if longonmarket then
ptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying)*pointsize
endif
if shortonmarket then
ptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying)*pointsize
endif
//endif
//graphonprice ptarget coloured(121,141,35,255) as "profittarget"
//endif
//endif
// to set & display stoploss
if enableSL then
set stop %loss SL
if displaysl then
if not onmarket then
sloss=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
sloss=0
endif
if onmarket then
if longonmarket then
sloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying)*pointsize
endif
if shortonmarket then
sloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying)*pointsize
endif
//endif
//graphonprice sloss coloured(255,0,0,255) as "stoploss"
//endif
//endif
//graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"
// trailing stop
if enablets then
ts1=0.33
ts2=0.16
switch=0.45
// i.e. from 0.33% to 0.45% trade performance it uses a 0.33% trailing stop
// from 0.45% and higher it uses a 0.16% trailing stop
trailingstop1 = (tradeprice/100)*ts1
trailingstop2 = (tradeprice/100)*ts2
if not onmarket then
maxprice1=0
minprice1=close
priceexit1=0
maxprice2=0
minprice2=close
priceexit2=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice1=0
minprice1=close
priceexit1=0
maxprice2=0
minprice2=close
priceexit2=0
endif
a1 = (positionperf*100)<=switch
a2 = (positionperf*100)>switch
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
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
if longonmarket then
maxprice2=max(maxprice2,close)
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2) then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
if shortonmarket then
minprice2=min(minprice2,close)
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2) then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
endif
If longonmarket and priceexit1>0 then
sell at priceexit1 stop
endif
if shortonmarket and priceexit1>0 then
exitshort at priceexit1 stop
endif
If longonmarket and priceexit2>0 then
sell at priceexit2 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
Function ghaph
Hello, sorry for the ignorance on the subject, but I can’t understand what to remove in the ghaph function to make the strategy work automatically. I did tests but without success. Thanks for your help.
Hi Fabiano
You disabled too much.
for real trading use this;
//graphonprice sloss coloured(255,0,0,255) as "stoploss"
sloss=sloss
same for the profittarget
add // before graphonprice for trailingstop and breakeven
add // before graph
Now for backtesting you wan to see the lines, so
graphonprice sloss coloured(255,0,0,255) as "stoploss"
//sloss=sloss
same for the profittarget.
remove // before graphonprice for trailingstop and breakeven
remove // before graph
I attached 2 codes as example for real and the backtest to see the differences.
just what I’am using now.
Ditched the Breakeven strategy and use a 3 level trailing stop.
same code, one for backtest and to see the difference one for real.
atr trailing stop & atr breakeven stop. Both can be used.
// breakeven stop atr
once breakevenstoptype = 1 // breakeven stop - 0 off, 1 on
once breakevenstoplong = 1 // breakeven stop atr relative distance
once breakevenstopshort = 1 // breakeven stop atr relative distance
once pointstokeep = 5 // positive or negative
once atrperiodbreakeven = 14 // atr parameter value
once minstopbreakeven = 10 // minimum breakeven stop distance
//----------------------------------------------
atrbreakeven = averagetruerange[atrperiodbreakeven]((close/10)*pipsize)/1000
//atrbreakeven=averagetruerange[atrperiodbreakeven]((close/1)*pipsize) // (forex)
bestopl = round(atrbreakeven*breakevenstoplong)
bestops = round(atrbreakeven*breakevenstopshort)
if breakevenstoptype = 1 then
//
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxpricebe = 0
minpricebe = close
newslbe = 0
endif
//
if longonmarket then
maxpricebe = max(maxpricebe,close)
if maxpricebe-tradeprice(1)>=bestopl*pointsize then
if maxpricebe-tradeprice(1)>=minstopbreakeven then
newslbe=tradeprice(1)+pointstokeep*pipsize
else
newslbe=tradeprice(1)- minstopbreakeven*pointsize
endif
endif
endif
//
if shortonmarket then
minpricebe = min(minpricebe,close)
if tradeprice(1)-minpricebe>=bestops*pointsize then
if tradeprice(1)-minpricebe>=minstopbreakeven then
newslbe = tradeprice(1)-pointstokeep*pipsize
else
newslbe = tradeprice(1) + minstopbreakeven*pointsize
endif
endif
endif
//
if longonmarket then
if newslbe>0 then
sell at newslbe stop
endif
if newslbe>0 then
if low < newslbe then
sell at market
endif
endif
endif
//
if shortonmarket then
if newslbe>0 then
exitshort at newslbe stop
endif
if newslbe>0 then
if high > newslbe then
exitshort at market
endif
endif
endif
//graphonprice newslbe coloured(255,165,0) as "breakevenstop atr"
endif
// trailing stop atr
once trailingstoptype = 1 // trailing stop - 0 off, 1 on
once trailingstoplong = 3 // trailing stop atr relative distance
once trailingstopshort = 3 // trailing stop atr relative distance
once atrtrailingperiod = 14 // atr parameter value
once minstop = 10 // minimum trailing stop distance
//----------------------------------------------
atrtrail = averagetruerange[atrtrailingperiod]((close/10)*pipsize)/1000
//atrtrail=averagetruerange[atrtrailingperiod]((close/1)*pipsize) (forex)
tgl = round(atrtrail*trailingstoplong)
tgs = round(atrtrail*trailingstopshort)
if trailingstoptype = 1 then
//
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice = 0
minprice = close
newsl = 0
endif
//
if longonmarket then
maxprice = max(maxprice,close)
if maxprice-tradeprice(1)>=tgl*pointsize then
if maxprice-tradeprice(1)>=minstop then
newsl = maxprice-tgl*pointsize
else
newsl = maxprice - minstop*pointsize
endif
endif
endif
//
if shortonmarket then
minprice = min(minprice,close)
if tradeprice(1)-minprice>=tgs*pointsize then
if tradeprice(1)-minprice>=minstop then
newsl = minprice+tgs*pointsize
else
newsl = minprice + minstop*pointsize
endif
endif
endif
//
if longonmarket then
if newsl>0 then
sell at newsl stop
endif
if newsl>0 then
if low < newsl then
sell at market
endif
endif
endif
//
if shortonmarket then
if newsl>0 then
exitshort at newsl stop
endif
if newsl>0 then
if high > newsl then
exitshort at market
endif
endif
endif
//graphonprice newsl coloured(0,0,255,255) as "trailingstop atr"
endif
Breakeven- en trailingstop on different securities & indexes & forex
This topic contains 79 replies,
has 15 voices, and was last updated by Paul
5 years, 6 months ago.
| Forum: | ProOrder support |
| Language: | English |
| Started: | 02/05/2019 |
| Status: | Active |
| Attachments: | 34 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.