Backtest shows wrong results

Viewing 15 posts - 1 through 15 (of 29 total)
  • Author
    Posts
  • #211325 quote
    ProRealP
    Participant
    Senior

    Hello,

    I’m running a system with a partial close code that shows that it’s closing positions in backtesting, but it doesn’t happen when live. How could this happen? Spread is the same, or even lower live, but this keeps happening. The stoploss also resets itself at completely the wrong level for no apparent reason. It’s quite maddening!

    This is a revised version of my code.

    defparam cumulateorders = false
    DEFPARAM CumulateOrders = false
    DEFPARAM Preloadbars = 10000
    
    OK = time >= 153000 AND time <= 220000
    
    //
    
    if OK and onmarket then
    set stop loss sl
    endif
    
    indicator3 = AVERAGE[50]
    indicator4 = AVERAGE[200]
    
    c5 = indicator3 crosses over indicator4
    c6 = indicator3 crosses under indicator4
    
    //B/S
    
    If not onmarket and c5 then
    buy positionsize contract at market
    set stop loss sl
    endif
    
    If not onmarket and c6 then
    sellshort positionsize contract at market
    set stop loss sl
    endif
    
    
    
    //SL
    
    TIMEFRAME(1hour)
    sl = PositionPrice + AverageTrueRange[14](close)*pipsize
    timeframe(default)
    //MaTrailing
    
    minDistance = 12
     
    If LongOnMarket then
    TP = Close + average[450](close)
    If TP > 0 then
    SELL AT min(close-minDistance*pointsize,average[450]) stop
    Endif
    Endif
     
    If ShortOnMarket then
    TP = Close - average[450](close)
    If TP > 0 then
    EXITSHORT AT min(close-minDistance*pointsize,average[450]) stop
    Endif
    Endif
    
    //PartialClose
    
    timeframe(2hour)
    // partial close
    ONCE partialclose = 1
    ONCE PerCent      = 0.25             //25%  = positions to close
    ONCE PerCentGain  = 0.005            //0.5% increase
    ONCE MinLotSize   = 0.5              //0.5  lots minimum
    ONCE Increments   = 1
    //
    if partialclose and OnMarket then
    ExitQuantity   = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)
    LeftQty        = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity  = abs(CountOfPosition) - LeftQty
    CloseQuantity2  = abs(CountOfPosition) - LeftQty
    else
    CloseQuantity  = 0
    Increments     = 1
    endif
    //
    
    
    IF LongOnMarket and close >= (PositionPrice + AverageTrueRange[14](close)*pipsize)  AND Flag THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    Increments = Increments + 1
    endif
    
    IF ShortOnMarket and close <= (PositionPrice - AverageTrueRange[14](close)*pipsize)  AND Flag2 THEN
    exitshort CloseQuantity2 Contracts AT Market
    Flag2 = 0
    Increments = Increments + 1
    endif
    
    IF Not OnMarket THEN
    Flag = 1
    Flag2 = 1
    ENDIF
    
    TIMEFRAME(default)
    
    //BE
    
    if flag = 0 or flag2 = 0 then
    set stop breakeven
    endif

    I’ve attached a couple of pictures that show this issue when the system is live. As you can see in the first picture the system should’ve sold a portion of the position and then moved the stop to breakeven. Instead I logged in to see it had set the stop loss to a completely different, made up level faaar away. I closed it manually. In the second picture you can see that when live the system doesn’t close the positions at nearly the same level as in back test. How is this possible?

    ex1.png ex1.png ex2.png ex2.png
    #211328 quote
    ProRealP
    Participant
    Senior

    I found another example. Again the system should’ve partially closed the open position in the afternoon, but didn’t, and instead closed the entire position when it hit the ma 450. Again, costing me money.

    ex3.png ex3.png
    #211332 quote
    phoentzs
    Participant
    Master

    Isn’t the MA450 in the TF 1hour completely somewhere else at the moment or am I misunderstanding your code?

    #211333 quote
    ProRealP
    Participant
    Senior

    The system runs on 1m TF, on us tech 100(nasdaq). Not sure of the relevance though tbh. The question is, why does the backtest act completely differently to live, when spread etc. are the same.

    #211334 quote
    ProRealP
    Participant
    Senior

    Misread your reply. Yes, it’s probably somewhere else at the moment. Again, that’s not relevant?

    #211336 quote
    fifi743
    Participant
    Master

    Je voudrais le tester : indice ou forex et quelle unité ?

    #211337 quote
    ProRealP
    Participant
    Senior

    I don’t speak french.

    #211338 quote
    fifi743
    Participant
    Master

    I would like to test it: indice or forex and which unit?

    #211348 quote
    ProRealP
    Participant
    Senior

    Nasdaq 1m

    #211351 quote
    JS
    Participant
    Senior

    Hi @ProRealPierre,

    I’ve only looked at your code superficially but what strikes me is that your Stop Loss is reposted every time by:

    If OK and OnMarket then

    Set Stop Loss SL

    EndIf

    The actual SL is calculated with:

    TimeFrame(1 Hour)

    SL = PositionPrice + AverageTrueRange[14](Close) * PipSize

    So every hour your SL will change and be reinserted… (is this the intention?)

    On the partial closure of positions:

    I don’t know any better that partial closure only works in “backtesting” and not in “live” at IG… but maybe this has changed…???

    #211352 quote
    ProRealP
    Participant
    Senior
    Hello @JS, Yes, that’s the intention, though I don’t mind changing it if it fixes the problem with the SL. See the first picture I’ve attached to this post. It show’s the SL yesterday being put at 21082, though according to the backtest it should’ve been set to roughly 12010. That’s changed then, yes. See the second attached picture which shows another system of mine partially closing the position as it should.
    FelSL.png FelSL.png PartialClose.png PartialClose.png
    #211363 quote
    PeterSt
    Participant
    Master
    Glancing over your code … it is full with ambiguities.  This really can’t work. Like JS said, reposting the SL is a first thing I would stay far from. Then, you can’t have several SL commands in there in the first place because only the last one executed will be THE one and only which is active. Next you have pending Stops which also don’t combine with SL. Oh, it can, if you have very explicit mutual exclusive pieces of code for it. This is quite hard in itself (to make decent) and you seem not to know about it. And thus nothing works. Next, what you see from the Backtest, never comprises reality when MTF is in order *and* when pending orders are used (Limit, Stop). Those orders only fall on the main TF limit. However, in practice it will just work fine (in Live). So this is “interpretation” as such (the gain reported by the backtest will act upon the correct price levels of exit (and entry)) Lastly – and a positive one – you can partially close orders as much as you want. But, do it the correct way (I have no judgment on your code regarding this – I just did not look for it). … Now tell me this is on PRT-IB and someone might delete my post and I’d have to create a new one. ‘-)
    #211366 quote
    ProRealP
    Participant
    Senior
    Thanks for your reply, @PeterSt! I see your point. I’ll change the stop to sell at at stop instead of setting the stop loss, and see if that works better. Appreciate the help!
    #211409 quote
    ProRealP
    Participant
    Senior
    @robertogozzi, or @Nicolas, do you have any idea as to why my system won’t partially close positions? I could understand slippage being the reason in cases where it just touches the level, but that doesn’t look to always be the case(look at the picture “ex3”, for example). Sometimes it just doesn’t partially close. There is seemingly nothing wrong with the code, right? I mean, sometimes it works..
    #211412 quote
    phoentzs
    Participant
    Master
    Would you trust your money to a code that sometimes works? 😉
Viewing 15 posts - 1 through 15 (of 29 total)
  • You must be logged in to reply to this topic.

Backtest shows wrong results


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
ProRealP @prorealpierre Participant
Summary

This topic contains 28 replies,
has 7 voices, and was last updated by ProRealP
2 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/10/2023
Status: Active
Attachments: 7 files
Logo Logo
Loading...