Skip to main content
Backtesting.py is a fast, lightweight Python framework for testing trading strategies against historical market data. It gives you a clean, minimal API to define a strategy, run a simulation, inspect detailed statistics, and explore interactive charts — all without locking you into a particular data provider or indicator library.

What it is

At its core, backtesting.py simulates a trading strategy running bar-by-bar through a historical OHLCV dataset. You write a Strategy 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

ConceptDescription
StrategyAbstract base class. Subclass it and implement init() and next().
BacktestEngine that runs the simulation. Accepts a data DataFrame and a Strategy class.
dataA 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.
The library is deliberately small. It does not include a live-trading engine, a broker integration, or a data feed. It does one thing — simulate a strategy on historical data — and does it well.

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.

Build docs developers (and LLMs) love