The 8GB RAM Spike That Shouldn't Exist
Backtesting a simple moving average crossover strategy on SPY from 2015-2023 shouldn't consume 8GB of RAM. Yet that's exactly what happened when I extended a working 1-year backtest to 8 years of daily data.
The strategy itself was trivial — 50-day vs 200-day SMA crossover, maybe 20 trades per year. The memory spike made no sense. Running the same period with 1-minute bars would be understandable, but daily bars? Something was leaking.
This isn't a Backtrader-specific issue. Any event-driven backtesting framework that accumulates state over time can hit this. But Backtrader's default settings make it particularly easy to blow up memory on multi-year tests without realizing why.
Why Backtrader Holds Every Single Bar
Backtrader's cerebro engine stores a full time series for every indicator and data feed by default. That self.sma50 indicator you defined? It's keeping every single value from day 1 in memory.
Continue reading the full article on TildAlice

Top comments (0)