What it is
At its core, backtesting.py simulates a trading strategy running bar-by-bar through a historical OHLCV dataset. You write aStrategy subclass, declare your indicators in init(), and place orders in next(). The Backtest engine handles order execution, position tracking, commissions, and statistics.
The library is indicator-library-agnostic: you can use NumPy, pandas, TA-Lib, Tulipy, scikit-learn, or any other tool to compute indicator values. If it returns a NumPy array the same length as your data, it works.
Core concepts
| Concept | Description |
|---|---|
Strategy | Abstract base class. Subclass it and implement init() and next(). |
Backtest | Engine that runs the simulation. Accepts a data DataFrame and a Strategy class. |
data | A pandas DataFrame with Open, High, Low, Close, and (optionally) Volume columns, indexed by datetime. |
Strategy.I() | Registers an indicator array with the engine so it is revealed bar-by-bar during next(). |
bt.run() | Executes the backtest and returns a pandas Series of performance statistics. |
bt.plot() | Opens an interactive Bokeh chart of the equity curve, trades, and indicators. |
Key features
Simple API
Two methods to implement (
init and next), two objects to use (Backtest and Strategy). The entire public surface fits on a single page.Blazing fast execution
The simulation loop is written to minimise Python overhead. Vectorised indicator computation happens before the loop; only order logic runs bar-by-bar.
Built-in optimizer
bt.optimize() performs grid search or sambo-based Bayesian optimisation over any strategy parameter, with parallel execution and heatmap visualisation.Interactive visualizations
bt.plot() produces a fully interactive Bokeh chart: zoom, pan, toggle indicators, inspect individual trades, and view the equity curve.Detailed statistics
Every run returns 30+ metrics including Sharpe ratio, Sortino ratio, Calmar ratio, max drawdown, win rate, profit factor, SQN, Kelly criterion, alpha, and beta.
Indicator-library-agnostic
Works with TA-Lib, Tulipy, pandas-ta, NumPy, scikit-learn, or any function that returns a NumPy array aligned to your data.
Any instrument
Supports equities, ETFs, futures, forex, crypto, bonds — any asset that produces OHLCV candlestick data.
Composable base strategies
backtesting.lib ships reusable building blocks: SignalStrategy, TrailingStrategy, FractionalBacktest, MultiBacktest, and helper utilities.Who it is for
Backtesting.py is aimed at:- Algorithmic traders who want to verify that a strategy idea actually worked on historical data before risking capital.
- Quantitative researchers who need a reproducible, scriptable simulation environment they can integrate into a larger research pipeline.
- Students and educators who want a minimal, readable codebase to learn the mechanics of strategy backtesting without wading through enterprise-grade complexity.
Get started
Quickstart
Run your first backtest in under five minutes using the built-in Google stock dataset.
Installation
Install backtesting.py and verify your environment is set up correctly.
License
Backtesting.py is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You may use it freely for any purpose, including commercial trading. If you distribute a modified version of the library itself (not your strategy code), those modifications must also be released under AGPL-3.0. Your proprietary strategy code remains entirely your own.