One strategy in three Python backtesting engines
Run a small known fixture before comparing the performance of libraries or strategies. We tested twelve synthetic daily candles with an initial balance of 1,000. Each engine started flat, held at most one unit and charged 0.10% of executed value on each side. There was no leverage or slippage in this fixture.
Match the clock before running the strategy
Once three closes are available, hold one unit when the latest close is above their simple average. Otherwise hold cash. Decide at the close and execute a state change at the next open. Equality means flat. The candles were chosen to end with no open position.
| Run | Timing mechanism | Ending equity |
|---|---|---|
| Backtrader | Market order queued during next() |
990.587 |
| Backtesting.py | trade_on_close=False |
990.587 |
| vectorbt | Signal shifted one row and order price set to the open | 990.587 |
All three produced the same four fills. The first round trip bought at 105 and sold at 100. The second bought at 106 and sold at 102. The gross loss was 9 and total commission was 0.413. The script checks fill sides and prices as well as equity.
Run the comparison
Save backtest-three-engines.py and run the commands in a Python 3.12 virtual environment.
python -m pip install backtrader==1.9.78.123 backtesting==0.6.6 vectorbt==1.2.2 pandas==3.1.2
python backtest-three-engines.py
If a result drifts, compare the first divergent fill. Inspect the signal timestamp, warm-up, quantity interpretation and exit commission before inspecting performance metrics. With positions open at the end, choose and document a liquidation or mark-to-market convention before rerunning.
Backtrader order execution · Backtesting.py reference · vectorbt portfolio reference
Continue with a side-by-side close and next-open example.