Hello all,
Has anyone experienced a backtest on the same code returning different results? I noticed this a couple times just today (1/17/22). Most of the times, I just run a new backtest which ‘fixes’ the issue, but I'm surprised because I thought tests were deterministic.
Unfortunately, I cannot share the backtests and the bug is hard reproduce. However I do have two backtests that demonstrate this behavior.
AK M
I've seen this occur again in another backtest. Different results for the same code. The trades are different as well.
I found that in one backtest, the pricing seems incorrect, while in the other, the pricing is correct.
Specifically, in the ‘correct’ backtest, my algorithm executes
2018-09-17 15:25:00UVXYSell MarketFill: $395.46045 USD-215259Filledbut in the ‘incorrect’ backtest, the algorithm executes
2018-09-17 15:25:00UVXYSell MarketFill: $7.909209 USD-6008938FilledI'm saying correct/incorrect based on the fact that for the second backtest, the value $7.90 is far from the price in the following orders for UVXY.
It seems like a data issue, but even so I would like to know why it occurs randomly.
Fred Painchaud
Hi AK M,
Normally (without bugs), the same code on the same assets for the same period of time should be deterministic, UNLESS the strategy/algo itself is not deterministic.
In other words, if an algo is deterministic, by definition, it means that the same inputs are going to produce the same outputs.
From what you are writing, it looks like an error in the data. But I don't know why one execution triggers it and not the other. Looks like the algo you are using is not deterministic in some way… Or you are hitting some bug in the platform on one of the two executions.
Fred
AK M
Yes, I can confirm that my algorithm has no random elements. It doesn't use ML or anything that requires a random initial state either.
These changes did happen after I started using the SpreadExecutionModel(0.001) but I have no idea if that is indeed the root cause.
A closer look at the source doesn't reveal any random elements:
AK M
I also requested a backtest report, just in case it was a UI rendering issue. But the backtest report is also erroneous.
Fred Painchaud
Well, without the code, it is difficult, or some would say impossible 😊, to say.
However, note that nondeterminism is not only a question of explicit randomness used in the algo. That “randomness”, which yes is a nice intuitive way to grasp nondeterminism, can be introduced by many many other ways. Refer to the most succinct and incomplete overview of nondeterminism I've seen (for the first time now 😊), https://en.wikipedia.org/wiki/Nondeterministic_algorithm, for a few other means. The most common way, I'd say without real backing data, not in the list, is a non-crashing bug in software. That bug can easily be triggered only on certain execution paths and can lead to different states in the program without those states being invalid enough to make it crash but still leading to different outputs. That bug could be in the algo itself, but also anywhere in the stack: LEAN, CLR/Python runtime, OS, drivers, firmware, hardware…
And there are other reasons too. Another classic, and that one is listed, is threads/true parallelism. LEAN is multi-threaded and most certainly runs on multi-core computers (true parallelism) - in backtest, live, at your place, in the cloud.
Fred
AK M
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!