Backtest shows wrong results
Forums › ProRealTime English forum › ProOrder support › Backtest shows wrong results
- This topic has 28 replies, 7 voices, and was last updated 1 year ago by ProRealP.
-
-
03/10/2023 at 5:53 PM #211325
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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101defparam cumulateorders = falseDEFPARAM CumulateOrders = falseDEFPARAM Preloadbars = 10000OK = time >= 153000 AND time <= 220000//if OK and onmarket thenset stop loss slendifindicator3 = AVERAGE[50]indicator4 = AVERAGE[200]c5 = indicator3 crosses over indicator4c6 = indicator3 crosses under indicator4//B/SIf not onmarket and c5 thenbuy positionsize contract at marketset stop loss slendifIf not onmarket and c6 thensellshort positionsize contract at marketset stop loss slendif//SLTIMEFRAME(1hour)sl = PositionPrice + AverageTrueRange[14](close)*pipsizetimeframe(default)//MaTrailingminDistance = 12If LongOnMarket thenTP = Close + average[450](close)If TP > 0 thenSELL AT min(close-minDistance*pointsize,average[450]) stopEndifEndifIf ShortOnMarket thenTP = Close - average[450](close)If TP > 0 thenEXITSHORT AT min(close-minDistance*pointsize,average[450]) stopEndifEndif//PartialClosetimeframe(2hour)// partial closeONCE partialclose = 1ONCE PerCent = 0.25 //25% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumONCE Increments = 1//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyCloseQuantity2 = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0Increments = 1endif//IF LongOnMarket and close >= (PositionPrice + AverageTrueRange[14](close)*pipsize) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0Increments = Increments + 1endifIF ShortOnMarket and close <= (PositionPrice - AverageTrueRange[14](close)*pipsize) AND Flag2 THENexitshort CloseQuantity2 Contracts AT MarketFlag2 = 0Increments = Increments + 1endifIF Not OnMarket THENFlag = 1Flag2 = 1ENDIFTIMEFRAME(default)//BEif flag = 0 or flag2 = 0 thenset stop breakevenendifI’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?
03/10/2023 at 6:04 PM #21132803/10/2023 at 8:10 PM #21133203/10/2023 at 9:00 PM #21133303/10/2023 at 9:07 PM #21133403/10/2023 at 9:13 PM #21133603/10/2023 at 9:19 PM #21133703/10/2023 at 9:46 PM #21133803/11/2023 at 7:50 AM #21134803/11/2023 at 10:50 AM #211351Hi @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…???
03/11/2023 at 10:57 AM #211352Hello @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.
03/11/2023 at 11:42 AM #211363Glancing 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. ‘-)
03/11/2023 at 12:43 PM #21136603/12/2023 at 12:11 PM #211409@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..
03/12/2023 at 12:53 PM #211412 -
AuthorPosts
Find exclusive trading pro-tools on