Backtrader RealisticExecutionModel

The same SMA-20 strategy on SPY, run through a realistic fill model instead of the default close-price fill: most of the return was never real.

+8.46%
return, no costs
+1.22%
with realistic execution
−7.24%
cost drag

Open-source contribution to Backtrader, one of the most widely-used Python backtesting libraries (20,000+ GitHub stars).

Overview

Backtrader has a structural blind spot: by default, every order fills at the exact closing price, no spread, no slippage, perfect execution that doesn't exist in real markets. I identified this flaw and built a RealisticExecutionModel to fix it.

What I Built

The model adjusts the actual fill price before an order is placed by modelling bid-ask spread, slippage, and commission in the direction that actually hurts the strategy. This matters because when fill price changes, the entire P&L, drawdown, and Sharpe ratio shift, every position was effectively entered and exited at the wrong price from the start. Subtracting fees at the end doesn't fix that.

Key Result

Running the same SMA-20 strategy on SPY (2020–2024):

  • No costs: +8.46% return
  • With realistic execution: +1.22% return
  • Cost drag: −7.24%

7.24% gone, not from a bad strategy, but from costs that most backtests never model.

Tools & Skills

  • Python, Backtrader framework
  • Market microstructure, bid-ask spread, slippage modelling
  • Open-source contribution workflow (Pull Request #514)

What This Demonstrates

  • Ability to identify structural flaws in widely-used tools
  • Translating market theory into working code
  • Contributing to open-source at a library level

Links

→ View Pull Request #514 on GitHub