How to prevent recently closed trade from reopening
Forums › ProRealTime English forum › ProOrder support › How to prevent recently closed trade from reopening
- This topic has 10 replies, 4 voices, and was last updated 5 years ago by
deletedaccount100622.
-
-
02/20/2020 at 4:31 PM #120088
I’m finding that with trailing stops a trade will often close at a profit only to reopen a few candles later if entry conditions persist, even if it’s near the top of the run and not actually a sensible place to buy. Is there an easy way to prevent this? such as, only opening at a price better than wherever last trade closed?
02/20/2020 at 4:53 PM #120089You can use this example https://www.prorealcode.com/reply/95908/
1 user thanked author for this post.
02/20/2020 at 5:35 PM #120091Ciao Roberto, thanks for that – could definitely work. Is there no way to do it base on price rather than time (bars) ?
02/20/2020 at 5:47 PM #120092Is there no way to do it base on price rather than time (bars) ?
The problem with doing it on price is that if you say I will only enter again at a lower price and then the market just goes up from there then your strategy will never open a trade ever again. I guess you could have this rule and then maybe reset things once or twice a day or week to get the strategy trading again.
02/20/2020 at 5:56 PM #120093Something like this perhaps:
1234567891011121314151617181920defparam cumulateorders = falseonce tradeon = 1if (your conditions) and tradeon thenbuy 1 contract at markettradeon = 0endifif strategyprofit <> strategyprofit[1] thenlasttime = opentimeendifif not tradeon and (your conditions) and close < tradeprice thenbuy 1 contract at marketendifif intradaybarindex = 0 or time = lasttime + 120000 then //resets 12 hours after last trade is closed or at start of the day.tradeon = 1endif1 user thanked author for this post.
02/20/2020 at 6:08 PM #120095Yeah, that looks good too. I’ll play around with both solutions and see which works best. Thanks to you both!
02/21/2020 at 2:22 PM #120158@Vonasi unfortunately this results in no trades at all, unless I added it wrong? I tried altering the reset time, but no difference.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121// Definition of code parametersDEFPARAM CumulateOrders = false // Cumulating positions deactivatedDEFPARAM preloadbars = 5000//Money ManagementMM = 0if MM = 0 then //MM = 0 for optimizationpositionsize=1ENDIFif MM = 1 thenONCE startpositionsize = 1ONCE factor = 10 // factor of 15 means margin increases/decreases @ 6.6% of strategy profit; 10 = 10% 20 = 5% etc — optimize for best result, profit vs drawdownONCE margin = 40 // enter margin value of 1 contractONCE maxpositionsize = 200 // NAS €1 IG first tier margin limitONCE minpositionsize = .5 // enter minimum position allowedIF Not OnMarket THENpositionsize = startpositionsize + Strategyprofit/(factor*margin)ENDIFIF Not OnMarket THENif startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THENpositionsize = minpositionsize// keeps positionsize from going below allowed minimumENDIFIF startpositionsize + Strategyprofit/(factor*margin) > maxpositionsize thenpositionsize = maxpositionsize// keeps positionsize from going above IG first tier margin limitENDIFENDIFENDIFTIMEFRAME(120 minutes)Period= pinner = 2*weightedaverage[round( Period/2)](typicalprice)-weightedaverage[Period](typicalprice)HULL = weightedaverage[round(sqrt(Period))](inner)c1 = HULL > HULL[1]//c2 = HULL < HULL[1]indicator1 = SuperTrend[m,n]c3 = (close > indicator1)//c4 = (close < indicator1)TIMEFRAME(30 minutes)indicator4 = Average[2](typicalPrice)indicator5 = Average[8](typicalPrice)c11 = (indicator4 > indicator5)//c12 = (indicator4 < indicator5)TIMEFRAME(15 minutes)indicator2 = Average[7](typicalPrice)indicator3 = Average[10](typicalPrice)c7 = (indicator2 > indicator3)//c8 = (indicator2 < indicator3)Perioda= p1innera = 2*weightedaverage[round( Perioda/2)](typicalprice)-weightedaverage[Perioda](typicalprice)HULLa = weightedaverage[round(sqrt(Perioda))](innera)c9 = HULLa > HULLa[1]//c10 = HULLa < HULLa[1]TIMEFRAME(5 minutes)once tradeon = 1Periodb= p2innerb = 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 tradeon THENBUY PositionSize CONTRACT AT MARKETtradeon = 0ENDIFif strategyprofit <> strategyprofit[1] thenlasttime = opentimeIF not tradeon and c1 AND C3 AND C5 and c7 and c9 and c11 and close < tradeprice THENBUY PositionSize CONTRACT AT MARKETENDIFif intradaybarindex = 0 or time = lasttime + 120000 then //resets 12 hours after last trade is closed or at start of the day.tradeon = 1endifENDIFSET STOP %LOSS stlSET TARGET %PROFIT .7//trailing stop functiontrailingstart = tst //trailing will start @trailinstart points profittrailingstep = st //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF02/21/2020 at 2:42 PM #120161Have you analysed the trades to ensure that the reentries are not profitable?
Your comment suggests that you see the second entry as less optimal, are you able to define the optimal state that you would want for entry in the code ie more than 30 points from high of day? That you could add as an opening condition to support the averages and supertrend that you are using that are more persistent conditions
02/21/2020 at 3:09 PM #12016402/21/2020 at 4:41 PM #120169Thanks Vonasi, that solves the problem I thought I had … but sadly doesn’t make the algo any more effective. Back to the proverbial…
@Robo Futures Trader tbh I haven’t gone through the backtest trade by trade, it’s more of an instinctive thing. The win rate is 87% so it’s getting it mostly right, but when the price retraces and closes a position it just ‘feels wrong’ to see it reopen 15 minutes later at a worse price. I’ve tried all kinds of things to redefine the entry conditions but nothing seems to help. Perhaps as you suggest, some sort of fixed price criteria rather than the combination of averages i tend to work with…
02/22/2020 at 9:28 AM #120200If you get the entries into Excel, convert the entry date to only be the date and then use a countif formula which will give you a column with the entry number on that day, you can then pivot to compare first, second entries etc
I was not saying to drop the use of moving averages but if you can define what you see as ideal and less ideal entries then it may be that you can add a further condition to allow the system to only take the first entry
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on