How to prevent recently closed trade from reopening

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #120088 quote
    nonetheless
    Participant
    Master

    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?

    #120089 quote
    robertogozzi
    Moderator
    Master

    You can use this example https://www.prorealcode.com/reply/95908/

    nonetheless thanked this post
    #120091 quote
    nonetheless
    Participant
    Master

    Ciao Roberto, thanks for that – could definitely work. Is there no way to do it base on price rather than time (bars) ?

    #120092 quote
    Vonasi
    Moderator
    Master

    Is 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.

    #120093 quote
    Vonasi
    Moderator
    Master

    Something like this perhaps:

    defparam cumulateorders = false
    
    once tradeon = 1
    
    if (your conditions) and tradeon then 
    buy 1 contract at market
    tradeon = 0
    endif
    
    if strategyprofit <> strategyprofit[1] then
    lasttime = opentime
    endif
    
    if not tradeon and (your conditions) and close < tradeprice then 
    buy 1 contract at market
    endif
    
    if intradaybarindex = 0 or time = lasttime + 120000 then //resets 12 hours after last trade is closed or at start of the day.
    tradeon = 1
    endif
    nonetheless thanked this post
    #120095 quote
    nonetheless
    Participant
    Master

    Yeah, that looks good too. I’ll play around with both solutions and see which works best. Thanks to you both!

    #120158 quote
    nonetheless
    Participant
    Master

    @Vonasi unfortunately this results in no trades at all, unless I added it wrong? I tried altering the reset time, but no difference.

    // Definition of code parameters
    DEFPARAM CumulateOrders = false // Cumulating positions deactivated
    DEFPARAM preloadbars = 5000
    //Money Management
    MM = 0
    if MM = 0 then //MM = 0 for optimization
    positionsize=1
    ENDIF
    if MM = 1 then
    ONCE startpositionsize = 1
    ONCE 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 drawdown
    ONCE margin = 40 // enter margin value of 1 contract
    ONCE maxpositionsize = 200 // NAS €1 IG first tier margin limit
    ONCE minpositionsize = .5 // enter minimum position allowed
    IF Not OnMarket THEN
    positionsize = startpositionsize + Strategyprofit/(factor*margin)
    ENDIF
    IF Not OnMarket THEN
    if startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
    positionsize = minpositionsize// keeps positionsize from going below allowed minimum
    ENDIF
    IF startpositionsize + Strategyprofit/(factor*margin) > maxpositionsize then
    positionsize = maxpositionsize// keeps positionsize from going above IG first tier margin limit
    ENDIF
    ENDIF
    ENDIF
    
    TIMEFRAME(120 minutes)
    Period= p
    inner = 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= p1
    innera = 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 = 1
    Periodb= p2
    innerb = 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 positions
    IF c1 AND C3 AND C5 and c7 and c9 and c11 and tradeon THEN
    BUY PositionSize CONTRACT AT MARKET
    tradeon = 0
    ENDIF
    
    if strategyprofit <> strategyprofit[1] then
    lasttime = opentime
    IF not tradeon and c1 AND C3 AND C5 and c7 and c9 and c11 and close < tradeprice THEN
    BUY PositionSize CONTRACT AT MARKET
    ENDIF
    
    if intradaybarindex = 0 or time = lasttime + 120000 then //resets 12 hours after last trade is closed or at start of the day.
    tradeon = 1
    endif
    ENDIF
    
    SET STOP %LOSS stl
    SET TARGET %PROFIT .7
    //trailing stop function
    trailingstart = tst //trailing will start @trailinstart points profit
    trailingstep = st //trailing step to move the "stoploss"
     
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    
    #120161 quote
    deletedaccount100622
    Participant
    New

    Have 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

    #120164 quote
    Vonasi
    Moderator
    Master

    Insert an ENDIF after line 72. It seems there was one missing in my code (I didn’t test it as it was ust a general idea to work with).

    I’ll edit the code in my earlier post to correct it.

    #120169 quote
    nonetheless
    Participant
    Master

    Thanks 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…

    #120200 quote
    deletedaccount100622
    Participant
    New

    If 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

    nonetheless thanked this post
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

How to prevent recently closed trade from reopening


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 10 replies,
has 4 voices, and was last updated by deletedaccount100622
6 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/20/2020
Status: Active
Attachments: No files
Logo Logo
Loading...