Backtesting.py

github.com ↗

Test entry and exit rules against historical price bars

SI Score
80/100
Pricing
Open Source AI reading, not confirmed
Free plan
Yes AI reading, not confirmed
Sign-up required
Not required AI reading, not confirmed

Ask SI about Backtesting.py

Answers use saved listing facts and recorded sources.
Homepage screenshot
Screenshot of the Backtesting.py homepage

What it does

Backtesting.py is a Python framework for simulating trading rules, comparing parameter settings and plotting results. It works with one tradable asset at a time using open, high, low and close price data. The package is freely available under AGPL version 3 and runs in a local Python environment.

Backtesting.py suits a focused question about entry and exit rules for one instrument. Supply a pandas table with Open, High, Low and Close columns, plus optional Volume. It is not designed for stock selection, arbitrage across assets or whole-portfolio rebalancing.

Define indicators in a Strategy’s init() method and the per-bar decisions in next(). Construct a Backtest with the data, strategy, starting cash and commission assumption. Calling run() returns summary metrics, the equity curve and detailed trades; plot() creates an interactive view. Installation uses pip install backtesting, without registering for a hosted service.

Strategy parameters can be compared through optimize(). Supply the allowed values and a constraint that rejects unsuitable combinations, such as a fast moving average period that is not shorter than the slow period. The objective can be a selected result metric, rather than only final portfolio value.

The simulation makes decisions once each price bar is available. It cannot infer the path inside a candle; finer timing requires finer input data. Market orders normally use the next bar’s open, with a separate option to use the current close. These choices affect the meaning of the resulting trades. The project’s AGPL licence also needs attention when modifying the framework for a network service.

Strengths

  • Returns detailed trades and equity history alongside summary statistics
  • Parameter searches accept constraints on allowed combinations
  • Interactive charts connect strategy events with price history

Watch out for

  • The core workflow handles one instrument at a time
  • Bar data cannot establish an exact price path within each candle

Best for

  • Python users prototyping single-instrument entry and exit rules
  • Analysts comparing parameter choices with explicit trading costs

Sources