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.
-
-
04/14/2020 at 7:05 PM #126143
Above added as Log 206 to here …
Also a Note added re the LOADS of TS in this Topic
3 users thanked author for this post.
04/14/2020 at 7:53 PM #126152Hi,
first a humble thanks for sharing all these cool codes!
I’ve been looking for a suitable protect-your-money-system and have been playing around with some Money Management-, Stop Loss-, Trailing Stop-, Breakeven-, Profit Target-systems and so on. There’s a lot of topics of the same subject with similar methods and I feel a bit overwhelmed of all the information. So I figured, why not ask someone who actually knows 🙂So Paul and other Jedi-coders. If you would rekommend a setup of protect-your-money-system for a padawan, what would it be and why? I would love to hear your thoughts about MM, SL, TS, BE, PT and such.
Keep up the awesome work! I hope to contribute in the future…
04/14/2020 at 8:01 PM #126153Hi Paul, thanks very much for posting this. Are these to be used in conjunction with the code you posted earlier, April 2019? or they’re designed to work independently of that?
1 user thanked author for this post.
04/15/2020 at 1:51 PM #126278@keewee Thanks! My advise, don’t use too many things. Check if something works as you think it should work (display the lines). Only work on mm if you already have a succesfull proven code. Don’t use a big stoplos (depended on timeframe) as in live trading they will occur more frequent then in an optimised backtest. Trailing stop there are a few, like posted before which used %. A.t.m. I prefer an atr based trailing stop. Also the atr breakeven is a nice edition. a pt is sometimes usefull. Too many things though lead quickly to over optimisation!
@nonetheless It’s different. The ones from april use %, this one uses atr. Also the % based uses the underlaying value to make it work on different assets. Probably it can work together if there are no code overlapping, but it’s not the goal. I thought the atr breakeven would be a nice addition, but not worth of a topic so that’s why I posted it here.
1 user thanked author for this post.
04/15/2020 at 2:35 PM #126280Thanks for the clarification, I thought that was prob the case. Going back to the underlaying code, would you mind having a look at this strategy to see if I’ve applied it correctly?
(BTW, how does it feel to be a Jedi-Coder? sounds pretty cool to me. Personally, I’m struggling to get beyond the Jar Jar Binks level)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292// Definition of code parametersDEFPARAM CumulateOrders = false // Cumulating positions deactivatedDEFPARAM preloadbars = 5000//Money Management DOWMM = 0 // = 0 for optimizationif MM = 0 thenpositionsize=1ENDIFif MM = 1 thenONCE startpositionsize = .4ONCE factor = 10 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etcONCE factor2 = 20 // 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 limitENDIFENDIFENDIFENDIFonce 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 = 1.9 // % stop lossPT = 2.3 // % profit targetTS = 0.26 // % 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<1TIMEFRAME(2 hours,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(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 pclong and 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 MARKETlongtradecounter=longtradecounter+1ENDIF// Conditions to enter short positionsIF pcshort and 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 MARKETshorttradecounter=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 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"//sloss=slossendifendif// 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"//ptarget=ptargetendifendif// 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"endifendifgraph (positionperf*100)coloured(0,0,0,255) as "positionperformance"04/15/2020 at 3:21 PM #126281It was a very cool way of saying! Still got a lot of work to do to feel like that!
I’ve loaded up your code on the dji 5min and looking at the lines, it looks correct!
You have trailingstop & the breakeven almost go active at the same level, which makes the breakeven not effective.
how about increasing the trailingstop slightly and using the breakeven to reduce the stoploss?
(optimised without spread)
try activate ts at 0.5%, besg at 0.3 and besl at -0.3 and still >90% winchance.
or ts at 0.5, besg at 0.3 and besl at 0.05%
that’s also great with 97% winchance.
04/15/2020 at 3:34 PM #126284Excellent, thanks ever so much. I haven’t even begun to play with the numbers as I wanted to be sure I had it set up right (just copied in what I had) .
And your ATR TS should also work as a variation, replacing the %TS?
04/15/2020 at 5:38 PM #126314i cannot run the code because the instruction “Graph”…may somebody help me? thank you
04/15/2020 at 5:58 PM #126319You just have to rem out any line that says graph, or graphonprice etc. For example:
12345//graphonprice newsl coloured(244,102,27,255) as "breakevenstop"endifendif//graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"04/15/2020 at 6:49 PM #126326And your ATR TS should also work as a variation, replacing the %TS?
yes but i’am not too sure about the atr trailingstop combined with % Breakeven, as both have something in common, like newsl.
1 user thanked author for this post.
04/15/2020 at 9:10 PM #126334Hi!
I am backtesting “Vectorial us100 v5” and want to test it on different instruments. On some instrument it runs the backtest as it should. On others it stops taking positions after a few trades or makes no trades at all.
This is a part of the original code:
// used for sl/pt/ (not used for ts); not to be optimized!
// forex use 0.01; securities use 1, index use 100
// profittargets and stoploss have to match the lines with displaysl/tp/ts set to 1
underlaying=100If I for example try Forex with the setting 0.01 instead of 100 it still doesn´t work.
“profittargets and stoploss have to match the lines with displaysl/tp/ts set to 1”
Is this the problem and what does it mean to match the lines?04/16/2020 at 10:14 AM #126373@OboeOpt match the lines means what it says.
if a stoploss is x% and you have the entryprice, the red line should exactly be there where you have your stoploss. If displayed at another (crazy) level, then you need to adjust the underlaying value.
Sometimes a strategy isn’t suitable for every market on any timeframe, because of range or volume etc.
You can post your itf file , with info on which market & timeframe , so I can quickly look if you put in right
1 user thanked author for this post.
04/16/2020 at 5:15 PM #126435Thanks for your answer Paul!
I´m just experimenting with
https://www.prorealcode.com/wp-content/uploads/2019/07/vectorial-us100-v5.0p-5m.itfFor example gbp/usd and other Forex mini with underlaying=0.01 I don´t get any trades (no other Changes to the code).
Same problem for stocks with underlaying=1. Works for most index with value 100.I use IG as broker and timeframe 5 minutes.
04/16/2020 at 5:34 PM #126437oke had a look on gbp/usd.
to test if the core code is right, I removed ts/sl/pt etc and and just used a default small stoploss&pt.
The core code of the vectorial dax doesn’t create a trade so it has nothing to do with the exit criteria which use the underlaying value.
1 user thanked author for this post.
04/16/2020 at 5:50 PM #126438Ok, so there is no Quick fix to change just to scan through instruments looking for candidates where vectorial can work?
Is there anything in perticular in the core code that does that no signal is triggered for some instruments? Just tested several Swedish stocks and didn´t get any trades.
-
AuthorPosts