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.
-
-
05/16/2020 at 6:23 PM #131881
and an trailingstop with percentage, but with 3 steps. Didn’t optimise. i.e.
default trailingstop 2% (it needs to reach a positionperformance of 2%, to close on 2% retracement)
if positionperformance > 3% then change trailingstop to 1%
if positionperformance > 4% then change trailingstop to 0.5%
05/21/2020 at 5:19 PM #13264605/21/2020 at 5:36 PM #132653I’ve just set it going on Forward Test.
Seems to be doing well … results attached.
05/21/2020 at 5:42 PM #132657Not this one, post #126280 is on TF 5 minutes. I just try to change stop loss condition (if..) to classic SL code (I have an IG account with SL guaranteed) but same results…
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295/ Definition of code parametersDEFPARAM CumulateOrders = FALSE // Cumulating positions deactivatedDEFPARAM preloadbars = 500//Money Management DOWMM = 0 // = 0 for optimizationif MM = 0 thenpositionsize=1ENDIFif MM = 1 thenONCE startpositionsize = 1ONCE factor = 50 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etcONCE factor2 = 60 // tier 2 factorONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverageONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverageONCE tier1 = 55 // DOW €1 IG first tier margin limitONCE maxpositionsize = 550 // DOW €1 IG tier 2 margin limitONCE minpositionsize = .2 // enter minimum position allowedIF Not OnMarket THENpositionsize = startpositionsize + Strategyprofit/(factor*margin)ENDIFIF Not OnMarket THENIF startpositionsize + Strategyprofit/(factor*margin) > tier1 thenpositionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 //incorporating tier 2 marginENDIFIF Not OnMarket THENif startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THENpositionsize = minpositionsize //keeps positionsize from going below allowed minimumENDIFIF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 > maxpositionsize thenpositionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limitENDIFENDIFENDIFENDIF//once enableSL = 1 // stop lossonce enablePT = 1 // profit targetonce enableTS = 1 // trailing stoponce enableBE = 1 // breakeven stop//once displaySL = 1 // stop loss//once displayPT = 1 // profit target//once displayTS = 1 // trailing stop//once displayBE = 1 // breakeven stopSL = 1.7//1.9 // % stop lossPT = 2.4//2.3 // % profit targetTS = 0.4//0.35//0.26 // % trailing stopBESG = 0.5//0.35//0.25 // % break even stop gainBESL = 0.2//0.40//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=50//100// reset at start//if intradaybarindex=0 then//longtradecounter=0//shorttradecounter=0//endif//pclong = longtradecounter<1//pcshort = shorttradecounter<1TIMEFRAME(120 MINUTES,updateonclose)Period= 495inner = 2*weightedaverage[round( Period/2)](typicalprice)-weightedaverage[Period](typicalprice)HULLa = weightedaverage[round(sqrt(Period))](inner)c1 = HULLa > HULLa[1]c2 = HULLa < HULLa[1]indicator1 = SuperTrend[8,6]c3 = (close > indicator1)c4 = (close < indicator1)ma = average[60,3](close)c11 = ma > ma[1]c12 = ma < ma[1]//Stochastic RSI | indicatorlengthRSI = 15 //RSI periodlengthStoch = 9 //Stochastic periodsmoothK = 10 //Smooth signal of stochastic RSIsmoothD = 5 //Smooth signal of smoothed stochastic RSImyRSI = RSI[lengthRSI](close)MinRSI = lowest[lengthStoch](myrsi)MaxRSI = highest[lengthStoch](myrsi)StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)K = average[smoothK](stochrsi)*100D = average[smoothD](K)c13 = K>Dc14 = K<DTIMEFRAME(30 minutes,updateonclose)indicator5 = Average[2](typicalPrice)indicator6 = Average[7](typicalPrice)c15 = (indicator5 > indicator6)c16 = (indicator5 < indicator6)TIMEFRAME(15 minutes,updateonclose)indicator2 = Average[4](typicalPrice)indicator3 = Average[8](typicalPrice)c7 = (indicator2 > indicator3)c8 = (indicator2 < indicator3)ma2 = average[25,1](close)c17 = ma2 > ma2[1]c18 = ma2 < ma2[1]Periodc= 23innerc = 2*weightedaverage[round( Periodc/2)](typicalprice)-weightedaverage[Periodc](typicalprice)HULLc = weightedaverage[round(sqrt(Periodc))](innerc)c9 = HULLc > HULLc[1]c10 = HULLc < HULLc[1]TIMEFRAME(10 minutes)indicator1a = SuperTrend[2,7]c19 = (close > indicator1a)c20 = (close < indicator1a)TIMEFRAME (5 minutes)//(default)//Stochastic RSI | indicatorlengthRSIa = 3 //RSI periodlengthStocha = 6 //Stochastic periodsmoothKa = 9 //Smooth signal of stochastic RSIsmoothDa = 3 //Smooth signal of smoothed stochastic RSImyRSIa = RSI[lengthRSIa](close)MinRSIa = lowest[lengthStocha](myrsia)MaxRSIa = highest[lengthStocha](myrsia)StochRSIa = (myRSIa-MinRSIa) / (MaxRSIa-MinRSIa)Ka = average[smoothKa](stochrsia)*100Da = average[smoothDa](Ka)c23 = Ka>Dac24 = Ka<Dama3 = average[15,3](close)c21 = ma3 > ma3[1]c22 = ma3 < ma3[1]Periodb= 15innerb = 2*weightedaverage[round( Periodb/2)](typicalprice)-weightedaverage[Periodb](typicalprice)HULLb = weightedaverage[round(sqrt(Periodb))](innerb)c5 = HULLb > HULLb[1]and HULLb[1]<HULLb[2]c6 = HULLb < HULLb[1]and HULLb[1]>HULLb[2]// Conditions to enter long positionsIF c1 AND C3 AND C5 and c7 and c9 and c11 and c13 and c15 and c17 and c19 and c21 and c23 THENBUY positionsize CONTRACT AT MARKETset stop %loss SL//longtradecounter=longtradecounter+1ENDIF// Conditions to enter short positionsIF c2 AND C4 AND C6 and c8 and c10 and c12 and c14 and c16 and c18 and c20 and c22 and c24 THENSELLSHORT positionsize CONTRACT AT MARKETset stop %loss SL//shorttradecounter=shorttradecounter+1ENDIF//================== exit in profitif longonmarket and C6 and c8 and close>positionprice thensell at marketendifIf shortonmarket and C5 and c7 and close<positionprice thenexitshort at marketendif//==============exit at lossif longonmarket AND c2 and c6 and close<positionprice thensell at marketendifIf shortonmarket and c1 and c5 and close>positionprice thenexitshort at marketendif// 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”//sloss=sloss//endif//endif// to set & display profittargetif enablePT thenset 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”//ptarget=ptarget//endifendif// 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//priceexit=priceexit//graphonprice 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 stopendif//if displayBE then//newsl=newsl//graphonprice newsl coloured(244,102,27,255) as “breakevenstop”//endifendif//graph (positionperf*100)coloured(0,0,0,255) as “positionperformance”05/21/2020 at 5:44 PM #13265805/21/2020 at 5:46 PM #13265905/21/2020 at 5:57 PM #13266605/21/2020 at 6:25 PM #13267505/21/2020 at 7:15 PM #13268605/22/2020 at 11:44 AM #132799Hi @Paul
Thanks for your nice work !
Have you try to compare a strategy with and without breakeven, in a backtest on in Sample and Walk Forward In/OOS ?
Because in my experience it is often less interesting to be stop/lose frequentely on Breakeven than to have a bigger stop to let the trade breathe. May be to be at your Breakeven at x ATR could help instead a “classic” Breakeven at x Pips ?
Have a nice day
05/22/2020 at 1:30 PM #132814Hi zilliq
Have you try to compare a strategy with and without breakeven, in a backtest on in Sample and Walk Forward In/OOS ?
no, because i’am in general not a fan of such approach. Again additional choices, how many reputations, which %, linked or not and if you found something and take the last parameters next question is when to run again? It’s very time consuming.
May be to be at your Breakeven at x ATR could help instead a “classic” Breakeven at x Pips
yeah could very well help. Something to test!
Take care
1 user thanked author for this post.
05/26/2020 at 10:33 PM #133376Hi @auvergnat, the code I posted in #1260280 was an unfinished test. The latest working version of that is here: #125210
05/27/2020 at 8:30 AM #133394Seems to be doing well
30 sec TF still looking good … 14 days Forward Test attached.
05/27/2020 at 12:04 PM #13342505/27/2020 at 12:12 PM #133429Hi @GraHal, every time I say ‘no more’ to sub-1m TFs I seem to get drawn back in. It’s all your fault.
Anyway, I did a quick and dirty optimisation and you might want to try the following values:
1234567SL = 0.75 // % stop lossPT = 1.50 // % profit targetTS = .3 // % trailing stopBESG = .15 // % break even stop gainBESL = 0.00 // % break even stop levelsm = 20lm = 40Or for a higher win rate, v2
1234567SL = 0.75 // % stop lossPT = 1.50 // % profit targetTS = .2 // % trailing stopBESG = .15 // % break even stop gainBESL = 0.00 // % break even stop levelsm = 20lm = 30For it not to have blown up in 14 days of forward testing is good going – maybe it’s a cash cow?
-
AuthorPosts