
The Warmup Period Problem: Why Your Python Backtest Doesn't Match Live Trading
I ran the same SMA crossover strategy through a pandas backtest and a live-trading simulator. Same rules. Same data. Same time period. Different results. Metric Python/Pandas Live Simulation Total Return 13.23% 16.77% Total Trades 3 2 Max Drawdown 14.50% 13.78% This isn't a bug in either system. It's a fundamental difference in how they handle indicator warmup periods - and understanding it will change how you write backtests. The Strategy Classic moving average crossover on AAPL: Entry: Buy when 20-day SMA crosses above 50-day SMA Exit: Sell when 20-day SMA crosses below 50-day SMA Capital: $10,000 Period: 365 days Simple enough that implementation differences should be minimal. Yet one found 3 trades, the other found 2. The Typical Pandas Approach Here's how most of us write this: def calculate_moving_averages ( df , short_window , long_window ): df = df . copy () df [ " sma_short " ] = df [ " close " ]. rolling ( window = short_window ). mean () df [ " sma_long " ] = df [ " close "
Continue reading on Dev.to Python
Opens in a new tab




