Performance Optimization
I have recently done a lot of work to optimize the performance of my backtest for both regular optimization and genetic.
Here are my takeaways:
- DO use IsInstantiateOnEachOptimization=false. Make sure your strategy properly resets everything in Configure because the strategy object will get reused for multiple runs. This saves a lot of time.
- Use a small value for "Keep Best # Results." Mostly this impacts memory, but might slow things down a lot too. Experiment with setting it to 5 and see if that's significantly faster than setting to to 300.
- Avoid Session Iterators. If you don't require them, avoid them. Session iterators are quite slow.
- Hard code all data series that your strategy might need and never change that for your strategy. For a long time I didn't understand what they meant by "dynamic" data series. But the NT caching can freak out in very unpredictable ways. Even an if statement related to adding data series can screw things up. If you hard code the data series, even if you add several, the performance is quite good as all the caching works seamlessly.
- Avoid custom PerformanceMetrics - I don't know why, but they really slow things down. Note that custom OptimizationFitnesses do NOT. They are fine. Maybe I was doing something wrong, certainly possible.
- My experience says that executing in a Chart is slower than executing in Strategy Analyzer. I think there's some extra overhead in a Chart... which makes sense. So don't assume chart performance is reflective of SA performance.
- Backtest speed is proportional to number of bars. So be consistent in comparisons with the amount of data and number of data series you are using.
- IncludeTradeHistoryInBacktest - Documentation says setting this to false will improve performance. Perhaps this again just affects memory and I have plenty on my machine. Backtest speed seems just as good.
- Code a stopwatch in your strategy. Start it in Configure and end it on the last historical bar. Print the time.
- Comment out all of your code logic and validate that everything runs super fast.
- Add logic back in one piece at a time until you discover what's crashing or adding a lot of extra time.
Comment