Skip to main content

Quick Start

Get a working backtest running in under 5 minutes.

Installation

Install via pip and set up your environment.

Core Concepts

Learn how Strategy, Backtest, and orders work together.

API Reference

Full reference for every class, method, and parameter.

What is Backtesting.py?

Backtesting.py is a Python framework for testing trading strategies against historical market data. You define a strategy by subclassing Strategy, load OHLCV price data into a Backtest, then call .run() to simulate trading and .plot() to visualize the results interactively.
quickstart.py
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG

class SmaCross(Strategy):
    n1 = 10
    n2 = 20

    def init(self):
        price = self.data.Close
        self.ma1 = self.I(SMA, price, self.n1)
        self.ma2 = self.I(SMA, price, self.n2)

    def next(self):
        if crossover(self.ma1, self.ma2):
            self.buy()
        elif crossover(self.ma2, self.ma1):
            self.sell()

bt = Backtest(GOOG, SmaCross, commission=.002, exclusive_orders=True)
stats = bt.run()
bt.plot()

Key features

Fast execution

NumPy-powered engine simulates thousands of bars per second.

Built-in optimizer

Grid search or Bayesian optimization across any strategy parameters.

Interactive plots

Bokeh-powered equity curves, drawdown charts, and trade markers.

Rich statistics

Sharpe, Sortino, Calmar, SQN, Kelly Criterion, CAGR, and more.

Full order types

Market, limit, stop-limit, stop-loss, and take-profit orders.

Indicator agnostic

Works with TA-Lib, pandas-ta, NumPy, or any Python function.

Multiple time frames

Combine signals from different resolutions in one strategy.

Composable strategies

Extend SignalStrategy, TrailingStrategy, or FractionalBacktest.

Performance results

A complete backtest of a simple SMA crossover strategy on Google stock (2004–2013) produces detailed statistics in seconds:
Start                     2004-08-19 00:00:00
End                       2013-03-01 00:00:00
Duration                   3116 days 00:00:00
Exposure Time [%]                       94.27
Equity Final [$]                     68935.12
Return [%]                             589.35
Buy & Hold Return [%]                  703.46
Sharpe Ratio                             0.66
Sortino Ratio                            1.30
Max. Drawdown [%]                      -33.08
# Trades                                   93
Win Rate [%]                            53.76

Supports any instrument

Backtesting.py works with any asset class that has candlestick data — stocks, ETFs, forex pairs, cryptocurrencies, commodities, and futures. As long as your data has Open, High, Low, Close columns in a pandas DataFrame, you can backtest against it.

Optimization guide

Tune parameters with grid search and heatmaps.

Multiple time frames

Combine daily signals with intraday execution.

Machine learning

Integrate ML models as trading signals.

Multi-asset

Run parallel backtests across many instruments.

Build docs developers (and LLMs) love