Breakeven- en trailingstop on different securities & indexes & forex
Forums › ProRealTime English forum › ProOrder support › Breakeven- en trailingstop on different securities & indexes & forex
- This topic has 79 replies, 15 voices, and was last updated 4 years ago by Paul.
-
-
02/05/2019 at 4:25 AM #90468
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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167// test trailingstop and breakeven on the daxdefparam cumulateorders = falseonce enableSL = 1 // stop lossonce enablePT = 1 // profit targetonce enableTS = 0 // trailing stoponce enableBE = 1 // breakeven stoponce displaySL = 1 // stop lossonce displayPT = 1 // profit targetonce displayTS = 0 // trailing stoponce displayBE = 1 // breakeven stopSL = 0.75 // % stop lossPT = 0.50 // % profit targetTS = 0.35 // % trailing stopBESG = 0.25 // % break even stop gainBESL = 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 = 100underlaying1=1 // SL & PTunderlaying2=underlaying1/100 // BE [ don't change]//underlaying3=1// reset at startif intradaybarindex=0 thenlongtradecounter=0shorttradecounter=0endifpclong = longtradecounter<1pcshort = shorttradecounter<1shortma = average[sm](close)longma = average[lm](close)// conditions to enter long positionsl1 = (shortma crosses over longma)// conditions to enter shorts1 = (shortma crosses under longma)if pclong and l1 thenbuy 1 contract at marketlongtradecounter=longtradecounter+1endifif pcshort and s1 thensellshort 1 contract at marketshorttradecounter=shorttradecounter+1endif// trailing stopif enableTS thentrailingstop = (tradeprice/100)*TS*pointsizeif not onmarket thenmaxprice=0minprice=closepriceexit=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice=0minprice=closepriceexit=0endifif longonmarket thenmaxprice=max(maxprice,close)if maxprice-tradeprice(1)>=trailingstop*pointsize thenpriceexit=maxprice-trailingstop*pointsizeendifendifif shortonmarket thenminprice=min(minprice,close)if tradeprice(1)-minprice>=trailingstop*pointsize thenpriceexit=minprice+trailingstop*pointsizeendifendifif longonmarket and priceexit>0 thensell at priceexit stopendifif shortonmarket and priceexit>0 thenexitshort at priceexit stopendifif displayTS thengraphonprice priceexit coloured(0,0,255,255) as "trailingstop"endifendif// break even stopif enableBE thenif not onmarket thennewsl=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif longonmarket thenif close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/underlaying2)*pointsize thennewsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/underlaying2)*pointsizeendifendifif shortonmarket thenif tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/underlaying2)*pointsize thennewsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/underlaying2)*pointsizeendifendifif longonmarket and newsl>0 thensell at newsl stopendifif shortonmarket and newsl>0 thenexitshort at newsl stopendifif displayBE thengraphonprice newsl coloured(244,102,27,255) as "breakevenstop"endifendif// to set & display profittargetif enablePT thenset target %profit PTif displaypt thenif not onmarket thenptarget=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenptarget=0endifif onmarket thenif longonmarket thenptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying1)*pointsizeendifif shortonmarket thenptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying1)*pointsizeendifendifgraphonprice ptarget coloured(121,141,35,255) as "profittarget"endifendif// to set & display stoplossif enableSL thenset stop %loss SLif displaysl thenif not onmarket thensloss=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thensloss=0endifif onmarket thenif longonmarket thensloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying1)*pointsizeendifif shortonmarket thensloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying1)*pointsizeendifendifgraphonprice sloss coloured(255,0,0,255) as "stoploss"endifendifgraph (positionperf*100)coloured(0,0,0,255) as "positionperformance"1 user thanked author for this post.
02/05/2019 at 2:31 PM #90511Finished code! Case closed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167// test trailingstop and breakeven on the daxdefparam cumulateorders = falseonce enableSL = 1 // stop lossonce enablePT = 1 // profit targetonce enableTS = 1 // trailing stoponce enableBE = 1 // breakeven stoponce displaySL = 1 // stop lossonce displayPT = 1 // profit targetonce displayTS = 1 // trailing stoponce displayBE = 1 // breakeven stopSL = 0.75 // % stop lossPT = 1.50 // % profit targetTS = 0.25 // % trailing stopBESG = 0.25 // % break even stop gainBESL = 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=100underlaying=100// reset at startif intradaybarindex=0 thenlongtradecounter=0shorttradecounter=0endifpclong = longtradecounter<1pcshort = shorttradecounter<1shortma = average[sm](close)longma = average[lm](close)// conditions to enter long positionsl1 = (shortma crosses over longma)// conditions to enter shorts1 = (shortma crosses under longma)if pclong and l1 thenbuy 1 contract at marketlongtradecounter=longtradecounter+1endifif pcshort and s1 thensellshort 1 contract at marketshorttradecounter=shorttradecounter+1endif// trailing stopif enableTS thentrailingstop = (tradeprice/100)*TSif not onmarket thenmaxprice=0minprice=closepriceexit=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice=0minprice=closepriceexit=0endifif longonmarket thenmaxprice=max(maxprice,close)if maxprice-tradeprice(1)>=(trailingstop) thenpriceexit=maxprice-(trailingstop/(underlaying/100))*pointsizeendifendifif shortonmarket thenminprice=min(minprice,close)if tradeprice(1)-minprice>=(trailingstop) thenpriceexit=minprice+(trailingstop/(underlaying/100))*pointsizeendifendifif longonmarket and priceexit>0 thensell at priceexit stopendifif shortonmarket and priceexit>0 thenexitshort at priceexit stopendifif displayTS thengraphonprice priceexit coloured(0,0,255,255) as "trailingstop"endifendif// break even stopif enableBE thenif not onmarket thennewsl=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif longonmarket thenif close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize thennewsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsizeendifendifif shortonmarket thenif tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize thennewsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsizeendifendifif longonmarket and newsl>0 thensell at newsl stopendifif shortonmarket and newsl>0 thenexitshort at newsl stopendifif displayBE thengraphonprice newsl coloured(244,102,27,255) as "breakevenstop"endifendif// to set & display profittargetif enablePT thenset target %profit PTif displaypt thenif not onmarket thenptarget=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenptarget=0endifif onmarket thenif longonmarket thenptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying)*pointsizeendifif shortonmarket thenptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying)*pointsizeendifendifgraphonprice ptarget coloured(121,141,35,255) as "profittarget"endifendif// to set & display stoplossif enableSL thenset stop %loss SLif displaysl thenif not onmarket thensloss=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thensloss=0endifif onmarket thenif longonmarket thensloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying)*pointsizeendifif shortonmarket thensloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying)*pointsizeendifendifgraphonprice sloss coloured(255,0,0,255) as "stoploss"endifendifgraph (positionperf*100)coloured(0,0,0,255) as "positionperformance"03/19/2019 at 5:05 AM #93987Just an addition.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// trailing stopif enablets thents1=0.33ts2=0.16switch=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 stoptrailingstop1 = (tradeprice/100)*ts1trailingstop2 = (tradeprice/100)*ts2if not onmarket thenmaxprice1=0minprice1=closepriceexit1=0maxprice2=0minprice2=closepriceexit2=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice1=0minprice1=closepriceexit1=0maxprice2=0minprice2=closepriceexit2=0endifa1 = (positionperf*100)<=switcha2 = (positionperf*100)>switchif longonmarket thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendifIf shortonmarket thenminprice1=min(minprice1,close)if a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendifif longonmarket thenmaxprice2=max(maxprice2,close)if a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendifif shortonmarket thenminprice2=min(minprice2,close)if a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendifIf longonmarket and priceexit1>0 thensell at priceexit1 stopendifif shortonmarket and priceexit1>0 thenexitshort at priceexit1 stopendifIf longonmarket and priceexit2>0 thensell at priceexit2 stopendifif shortonmarket and priceexit2>0 thenexitshort at priceexit2 stopendifif displayts thengraphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"endifendif1 user thanked author for this post.
03/19/2019 at 11:29 AM #9401603/31/2019 at 8:36 PM #95139Too much work went into this so better store it here.
3 Level trailing stop and manage 2 positions in the same direction.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198// trailing stopif enablets thents1=0.35ts2=0.25ts3=0.20switch =ts3+ts2 //0.45-0.60 switch to 25% trailing stopswitch2=ts2+ts1 //0.60 and higher switch to 20% trailing stopif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentrailingstop1 = (tradeprice(1)/100)*ts1trailingstop2 = (tradeprice(1)/100)*ts2trailingstop3 = (tradeprice(1)/100)*ts3elsif (COUNTOFLONGSHARES=2 or COUNTOFSHORTSHARES=2) thentrailingstop1 = (tradeprice(2)/100)*ts1trailingstop2 = (tradeprice(2)/100)*ts2trailingstop3 = (tradeprice(2)/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=0pp1=0pp2=0endifif COUNTOFLONGSHARES=1 thenpp1=((close/tradeprice(1))-1)*100if pp1<switch thena1=1endifif pp1>=switch thena2=1endifif pp1>=switch2 thena3=1endifelsif COUNTOFLONGSHARES=2 thenpp1=((close/tradeprice(2))-1)*100pp2=((close/tradeprice(1))-1)*100if pp1<switch thena1=1endifif pp1>=switch thena2=1endifif pp1>=switch2 thena3=1endifendifif COUNTOFSHORTSHARES=1 thenpp1=((close/tradeprice(1))-1)*-100if pp1<switch thena1=1endifif pp1>=switch thena2=1endifif pp1>=switch2 thena3=1endifelsif COUNTOFSHORTSHARES=2 thenpp1=((close/tradeprice(2))-1)*-100pp2=((close/tradeprice(1))-1)*-100if pp1<switch thena1=1endifif pp1>=switch thena2=1endifif pp1>=switch2 thena3=1endifendif//first legif longonmarket thenmaxprice1=max(maxprice1,close)if COUNTOFLONGSHARES=1 and a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifIf countoflongshares=2 and a1 thenif maxprice1-tradeprice(2)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendifIf shortonmarket thenminprice1=min(minprice1,close)if countofshortshares=1 and a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifif countofshortshares=2 and a1 thenif tradeprice(2)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendif//2nd legif longonmarket thenmaxprice2=max(maxprice2,high)if COUNTOFLONGSHARES=1 and a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifif countoflongshares=2 and a2 thenif maxprice2-tradeprice(2)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendifIf shortonmarket thenminprice2=min(minprice2,low)if countofshortshares=1 and a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifIf countofshortshares=2 and a2 thenif tradeprice(2)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendif//3rd legif longonmarket thenmaxprice3=max(maxprice3,high)if COUNTOFLONGSHARES=1 and a3 thenif maxprice3-tradeprice(1)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifIf countoflongshares=2 and a3 thenif maxprice3-tradeprice(2)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendifIf shortonmarket thenminprice3=min(minprice3,low)if countofshortshares=1 and a3 thenif tradeprice(1)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifif countofshortshares=2 and a3 thenif tradeprice(2)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifendifIf longonmarket and priceexit1>0 thensell at priceexit1 stopendifif shortonmarket and priceexit1>0 thenexitshort at priceexit1 stopendifIf longonmarket and priceexit2>0 thensell at priceexit2 stopendifif shortonmarket and priceexit2>0 thenexitshort at priceexit2 stopendifIf longonmarket and priceexit3>0 thensell at priceexit3 stopendifif shortonmarket and priceexit3>0 thenexitshort at priceexit3 stopendifendif1 user thanked author for this post.
03/31/2019 at 8:53 PM #95140Just 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.
1 user thanked author for this post.
04/29/2019 at 12:04 AM #97312update trailing-stop for 2 positions
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226// trailing stopif enablets thents1=0.35ts2=0.30ts3=0.20switch =ts3+ts2 //0.50-0.65 switch to 30% trailing stopswitch2=ts2+ts1 //0.65 and higher switch to 20% trailing stopunderlaying=100tn=1 //use trailing stop on x tradenumberif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentrailingstop1 = (tradeprice(1)/100)*ts1trailingstop2 = (tradeprice(1)/100)*ts2trailingstop3 = (tradeprice(1)/100)*ts3elsif (COUNTOFLONGSHARES=2 or COUNTOFSHORTSHARES=2) thentrailingstop1 = (tradeprice(tn)/100)*ts1trailingstop2 = (tradeprice(tn)/100)*ts2trailingstop3 = (tradeprice(tn)/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 COUNTOFLONGSHARES=1 thenpp=((close/tradeprice(1))-1)*100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif COUNTOFSHORTSHARES=1 thenpp=((close/tradeprice(1))-1)*-100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif COUNTOFLONGSHARES=2 thenpp=((close/tradeprice(tn))-1)*100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif COUNTOFSHORTSHARES=1 thenpp=((close/tradeprice(1))-1)*-100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif COUNTOFSHORTSHARES=2 thenpp=((close/tradeprice(tn))-1)*-100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifendif//first legif COUNTOFLONGSHARES=1 thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendifIf countoflongshares=2 thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(tn)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendifIf countofshortshares=1 thenminprice1=min(minprice1,close)if a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendifif countofshortshares=2 thenminprice1=min(minprice1,close)if a1 thenif tradeprice(tn)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendif//2nd legif COUNTOFLONGSHARES=1 thenmaxprice2=max(maxprice2,high)if a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendifif countoflongshares=2 thenmaxprice2=max(maxprice2,high)if a2 thenif maxprice2-tradeprice(tn)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendifif countofshortshares=1 thenminprice2=min(minprice2,low)if a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendifIf countofshortshares=2 thenminprice2=min(minprice2,low)if a2 thenif tradeprice(tn)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendif//3rd legif countoflongshares=1 thenmaxprice3=max(maxprice3,high)if a3 thenif maxprice3-tradeprice(1)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendifIf countoflongshares=2 thenmaxprice3=max(maxprice3,high)if a3 thenif maxprice3-tradeprice(tn)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendifIf countofshortshares=1 thenminprice3=min(minprice3,low)if a3 thenif tradeprice(1)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifendifif countofshortshares=2 thenminprice3=min(minprice3,low)if a3 thenif tradeprice(tn)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifendifIf longonmarket and priceexit1>0 thensell at priceexit1 stopendifif shortonmarket and priceexit1>0 thenexitshort at priceexit1 stopendifIf longonmarket and priceexit2>0 thensell at priceexit2 stopendifif shortonmarket and priceexit2>0 thenexitshort at priceexit2 stopendifIf longonmarket and priceexit3>0 thensell at priceexit3 stopendifif shortonmarket and priceexit3>0 thenexitshort at priceexit3 stopendifendifupdate stoploss for 2 positions
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647// set stoplossif enablesl thensl=1.00 //max loss in %underlaying=100if not onmarket thensloss=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thensloss=0elsif countoflongshares>1 or countofshortshares>1 thensloss=0endifif countoflongshares=1 thensloss=tradeprice(1)-((tradeprice(1)*sl)/underlaying)*pointsizeelsif countofshortshares=1 thensloss=tradeprice(1)+((tradeprice(1)*sl)/underlaying)*pointsizeelsif countoflongshares=2 thensloss=tradeprice(2)-((tradeprice(2)*sl)/underlaying)*pointsizeelsif countofshortshares=2 thensloss=tradeprice(2)+((tradeprice(2)*sl)/underlaying)*pointsizeendifif countoflongshares=1 thenif low <= sloss thensell at sloss stopendifelsif countofshortshares=1 thenif high >= sloss thenexitshort at sloss stopendifelsif countoflongshares=2 thenif low <= sloss thensell at sloss stopendifelsif countofshortshares=2 thenif high >= sloss thenexitshort at sloss stopendifendifif displaysl thengraphonprice sloss coloured(255,0,0,255) as "stoploss"endifendif04/29/2019 at 2:33 AM #973131st
1234567once enablesl = 1 // stop lossonce enablept = 0 // profit targetonce enablets = 1 // trailing stoponce displaysl = 1 // stop lossonce displaypt = 1 // profit targetonce displayts = 1 // trailing stop2nd
12345678910111213141516171819202122232425sl = 1 // % stoplosspt = 1 // % profit targetts1=0.35ts2=0.30ts3=0.20// use trailingstop / profittarget / stoploss based on tradenumber [1=lastest / 2=first trade]tnt=1tnp=1tns=2 //first trade !switch =ts3+ts2 //0.50-0.65 switch to 30% trailing stopswitch2=ts2+ts1 //0.65 and higher switch to 20% trailing stopunderlaying=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 crude3rd
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266// trailing stopif 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)*ts3elsif (countoflongshares=2 or countofshortshares=2) thentrailingstop1 = (tradeprice(tnt)/100)*ts1trailingstop2 = (tradeprice(tnt)/100)*ts2trailingstop3 = (tradeprice(tnt)/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 countoflongshares=1 thenpp=((close/tradeprice(1))-1)*100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif countofshortshares=1 thenpp=((close/tradeprice(1))-1)*-100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif countoflongshares=2 thenpp=((close/tradeprice(tnt))-1)*100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifelsif countofshortshares=2 thenpp=((close/tradeprice(tnt))-1)*-100if pp>=ts1 and pp<switch thena1=1endifif pp>=switch thena2=1endifif pp>=switch2 thena3=1endifendif//first leg longif countoflongshares=1 thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendifif countoflongshares=2 thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(tnt)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendif//first leg shortif countofshortshares=1 thenminprice1=min(minprice1,close)if a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendifif countofshortshares=2 thenminprice1=min(minprice1,close)if a1 thenif tradeprice(tnt)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendif//2nd leg longif countoflongshares=1 thenmaxprice2=max(maxprice2,high)if a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendifif countoflongshares=2 thenmaxprice2=max(maxprice2,high)if a2 thenif maxprice2-tradeprice(tnt)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendif//2nd leg shortif countofshortshares=1 thenminprice2=min(minprice2,low)if a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendifif countofshortshares=2 thenminprice2=min(minprice2,low)if a2 thenif tradeprice(tnt)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendif//3rd leg longif countoflongshares=1 thenmaxprice3=max(maxprice3,high)if a3 thenif maxprice3-tradeprice(1)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendifif countoflongshares=2 thenmaxprice3=max(maxprice3,high)if a3 thenif maxprice3-tradeprice(tnt)>=(trailingstop3) thenpriceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsizeendifendifendif//3rd leg shortif countofshortshares=1 thenminprice3=min(minprice3,low)if a3 thenif tradeprice(1)-minprice3>=(trailingstop3) thenpriceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsizeendifendifendifif countofshortshares=2 thenminprice3=min(minprice3,low)if a3 thenif tradeprice(tnt)-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"endifendif// set stoplossif enablesl thenif not onmarket thensloss=0elsif countoflongshares=1 thensloss=tradeprice(1)-((tradeprice(1)*sl)/underlaying)*pointsizeelsif countofshortshares=1 thensloss=tradeprice(1)+((tradeprice(1)*sl)/underlaying)*pointsizeelsif countoflongshares=2 thensloss=tradeprice(tns)-((tradeprice(tns)*sl)/underlaying)*pointsizeelsif countofshortshares=2 thensloss=tradeprice(tns)+((tradeprice(tns)*sl)/underlaying)*pointsizeendifif longonmarket and low <= sloss thensell at sloss stopelsif shortonmarket and high >= sloss thenexitshort at sloss stopendifif displaysl thengraphonprice sloss coloured(255,0,0,255) as "stoploss"endifendif// to display profittargetif enablept thenif not onmarket thenptarget=0elsif countoflongshares=1 thenptarget=tradeprice(1)+((tradeprice(1)*pt)/underlaying)*pointsizeelsif countofshortshares=1 thenptarget=tradeprice(1)-((tradeprice(1)*pt)/underlaying)*pointsizeelsif countoflongshares=2 thenptarget=tradeprice(tnp)+((tradeprice(tnp)*pt)/underlaying)*pointsizeelsif countofshortshares=2 thenptarget=tradeprice(tnp)-((tradeprice(tnp)*pt)/underlaying)*pointsizeendifif longonmarket and high>=ptarget thensell at ptarget stopelsif shortonmarket and low<=ptarget thenexitshort at ptarget stopendifif displaypt thengraphonprice ptarget coloured(121,141,35,255) as "profittarget"endifendif1 user thanked author for this post.
05/03/2019 at 6:57 PM #97630just 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.
05/03/2019 at 9:20 PM #9764005/04/2019 at 2:33 PM #9768005/05/2019 at 3:07 PM #97729// test trailingstop and breakeven on the dax123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255// test trailingstop and breakeven on the daxdefparam cumulateorders = falseonce enableSL = 1 // stop lossonce enablePT = 1 // profit targetonce enableTS = 1 // trailing stoponce enableBE = 1 // breakeven stoponce displaySL = 1 // stop lossonce displayPT = 1 // profit targetonce displayTS = 1 // trailing stoponce displayBE = 1 // breakeven stopSL = 0.75 // % stop lossPT = 1.50 // % profit targetTS = 0.25 // % trailing stopBESG = 0.25 // % break even stop gainBESL = 0.00 // % break even stop levelSM = 186 //170LM = 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=100underlaying=100// reset at startif intradaybarindex=0 thenlongtradecounter=0shorttradecounter=0endifpclong = longtradecounter<1pcshort = shorttradecounter<1shortma = average[sm](close)longma = average[lm](close)// conditions to enter long positionsl1 = (shortma crosses over longma)// conditions to enter shorts1 = (shortma crosses under longma)if pclong and l1 thenbuy 1 contract at marketlongtradecounter=longtradecounter+1endifif pcshort and s1 thensellshort 1 contract at marketshorttradecounter=shorttradecounter+1endif// trailing stopif enableTS thentrailingstop = (tradeprice/100)*TSif not onmarket thenmaxprice=0minprice=closepriceexit=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice=0minprice=closepriceexit=0endifif longonmarket thenmaxprice=max(maxprice,close)if maxprice-tradeprice(1)>=(trailingstop) thenpriceexit=maxprice-(trailingstop/(underlaying/100))*pointsizeendifendifif shortonmarket thenminprice=min(minprice,close)if tradeprice(1)-minprice>=(trailingstop) thenpriceexit=minprice+(trailingstop/(underlaying/100))*pointsizeendifendifif longonmarket and priceexit>0 thensell at priceexit stopendifif shortonmarket and priceexit>0 thenexitshort at priceexit stopendif//if displayTS then//graphonprice priceexit coloured(0,0,255,255) as "trailingstop"//endif//endif// break even stopif enableBE thenif not onmarket thennewsl=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif longonmarket thenif close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize thennewsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsizeendifendifif shortonmarket thenif tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize thennewsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsizeendifendifif longonmarket and newsl>0 thensell at newsl stopendifif shortonmarket and newsl>0 thenexitshort at newsl stopendif//if displayBE then//graphonprice newsl coloured(244,102,27,255) as "breakevenstop"//endif//endif// to set & display profittargetif enablePT thenset target %profit PTif displaypt thenif not onmarket thenptarget=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenptarget=0endifif onmarket thenif longonmarket thenptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying)*pointsizeendifif shortonmarket thenptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying)*pointsizeendif//endif//graphonprice ptarget coloured(121,141,35,255) as "profittarget"//endif//endif// to set & display stoplossif enableSL thenset stop %loss SLif displaysl thenif not onmarket thensloss=0elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thensloss=0endifif onmarket thenif longonmarket thensloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying)*pointsizeendifif shortonmarket thensloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying)*pointsizeendif//endif//graphonprice sloss coloured(255,0,0,255) as "stoploss"//endif//endif//graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"// trailing stopif enablets thents1=0.33ts2=0.16switch=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 stoptrailingstop1 = (tradeprice/100)*ts1trailingstop2 = (tradeprice/100)*ts2if not onmarket thenmaxprice1=0minprice1=closepriceexit1=0maxprice2=0minprice2=closepriceexit2=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thenmaxprice1=0minprice1=closepriceexit1=0maxprice2=0minprice2=closepriceexit2=0endifa1 = (positionperf*100)<=switcha2 = (positionperf*100)>switchif longonmarket thenmaxprice1=max(maxprice1,close)if a1 thenif maxprice1-tradeprice(1)>=(trailingstop1) thenpriceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsizeendifendifendifIf shortonmarket thenminprice1=min(minprice1,close)if a1 thenif tradeprice(1)-minprice1>=(trailingstop1) thenpriceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsizeendifendifendifif longonmarket thenmaxprice2=max(maxprice2,close)if a2 thenif maxprice2-tradeprice(1)>=(trailingstop2) thenpriceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsizeendifendifendifif shortonmarket thenminprice2=min(minprice2,close)if a2 thenif tradeprice(1)-minprice2>=(trailingstop2) thenpriceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsizeendifendifendifIf longonmarket and priceexit1>0 thensell at priceexit1 stopendifif shortonmarket and priceexit1>0 thenexitshort at priceexit1 stopendifIf longonmarket and priceexit2>0 thensell at priceexit2 stopendifif shortonmarket and priceexit2>0 thenexitshort at priceexit2 stopendifFunction 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.
1 user thanked author for this post.
05/09/2019 at 3:38 AM #98006Hi Fabiano
You disabled too much.
for real trading use this;
12//graphonprice sloss coloured(255,0,0,255) as "stoploss"sloss=slosssame for the profittarget
add // before graphonprice for trailingstop and breakeven
add // before graph
Now for backtesting you wan to see the lines, so
12graphonprice sloss coloured(255,0,0,255) as "stoploss"//sloss=slosssame 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.
1 user thanked author for this post.
05/09/2019 at 4:06 AM #9800904/14/2020 at 6:37 PM #126139atr trailing stop & atr breakeven stop. Both can be used.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768// breakeven stop atronce breakevenstoptype = 1 // breakeven stop - 0 off, 1 ononce breakevenstoplong = 1 // breakeven stop atr relative distanceonce breakevenstopshort = 1 // breakeven stop atr relative distanceonce pointstokeep = 5 // positive or negativeonce atrperiodbreakeven = 14 // atr parameter valueonce 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)) thenmaxpricebe = 0minpricebe = closenewslbe = 0endif//if longonmarket thenmaxpricebe = max(maxpricebe,close)if maxpricebe-tradeprice(1)>=bestopl*pointsize thenif maxpricebe-tradeprice(1)>=minstopbreakeven thennewslbe=tradeprice(1)+pointstokeep*pipsizeelsenewslbe=tradeprice(1)- minstopbreakeven*pointsizeendifendifendif//if shortonmarket thenminpricebe = min(minpricebe,close)if tradeprice(1)-minpricebe>=bestops*pointsize thenif tradeprice(1)-minpricebe>=minstopbreakeven thennewslbe = tradeprice(1)-pointstokeep*pipsizeelsenewslbe = tradeprice(1) + minstopbreakeven*pointsizeendifendifendif//if longonmarket thenif newslbe>0 thensell at newslbe stopendifif newslbe>0 thenif low < newslbe thensell at marketendifendifendif//if shortonmarket thenif newslbe>0 thenexitshort at newslbe stopendifif newslbe>0 thenif high > newslbe thenexitshort at marketendifendifendif//graphonprice newslbe coloured(255,165,0) as "breakevenstop atr"endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566// trailing stop atronce trailingstoptype = 1 // trailing stop - 0 off, 1 ononce trailingstoplong = 3 // trailing stop atr relative distanceonce trailingstopshort = 3 // trailing stop atr relative distanceonce atrtrailingperiod = 14 // atr parameter valueonce 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)) thenmaxprice = 0minprice = closenewsl = 0endif//if longonmarket thenmaxprice = max(maxprice,close)if maxprice-tradeprice(1)>=tgl*pointsize thenif maxprice-tradeprice(1)>=minstop thennewsl = maxprice-tgl*pointsizeelsenewsl = maxprice - minstop*pointsizeendifendifendif//if shortonmarket thenminprice = min(minprice,close)if tradeprice(1)-minprice>=tgs*pointsize thenif tradeprice(1)-minprice>=minstop thennewsl = minprice+tgs*pointsizeelsenewsl = minprice + minstop*pointsizeendifendifendif//if longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif low < newsl thensell at marketendifendifendif//if shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif high > newsl thenexitshort at marketendifendifendif//graphonprice newsl coloured(0,0,255,255) as "trailingstop atr"endif -
AuthorPosts