Backtesting.py — Lightweight Python Backtesting Framework

Backtesting.py is a lightweight, beginner-friendly Python backtesting library built on Pandas. With ~8,100 GitHub stars, it’s known for its clean API and excellent interactive HTML charts powered by Bokeh. If you want to go from idea to backtest results with minimal code, this is a strong choice.

Language: Python | License: AGPL-3.0 | Price: Free | GitHub Stars: ~8.1K


Key Features

  • Simple, Pythonic API — minimal boilerplate to get started
  • Interactive Bokeh charts — zoomable, hoverable HTML visualizations far better than matplotlib
  • Built-in optimizer with parameter heatmaps
  • Comprehensive metrics — Sharpe ratio, Sortino, max drawdown, win rate, and more
  • Works with any OHLCV data — just pass a Pandas DataFrame
  • Partial position sizing and multiple concurrent trades
  • Jupyter notebook friendly — charts render inline
  • Use any indicator library — TA-Lib, pandas-ta, tulipy, or plain NumPy

Quick Start Example

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):
        close = self.data.Close
        self.sma1 = self.I(SMA, close, self.n1)
        self.sma2 = self.I(SMA, close, self.n2)

    def next(self):
        if crossover(self.sma1, self.sma2):
            self.buy()
        elif crossover(self.sma2, self.sma1):
            self.sell()

bt = Backtest(GOOG, SmaCross, cash=10_000, commission=.002)
stats = bt.run()
bt.plot()

The bt.plot() call generates an interactive HTML chart with equity curve, drawdown, trade markers, and indicator overlays — all zoomable and hoverable.

Optimization Example

stats, heatmap = bt.optimize(
    n1=range(5, 30, 5),
    n2=range(10, 70, 5),
    maximize='Sharpe Ratio',
    return_heatmap=True
)

This tests all SMA period combinations and returns the best by Sharpe ratio, plus a heatmap showing performance across the parameter space.

Data Sources

Backtesting.py doesn’t include built-in data feeds — you bring your own data as a Pandas DataFrame with Open, High, Low, Close (and optionally Volume) columns. Common sources:

  • yfinance — free Yahoo Finance data
  • Alpha Vantage — stocks, forex, crypto
  • CSV files loaded with pd.read_csv()
  • Any API that outputs a DataFrame

Pros

  • Very easy to learn — the lowest barrier to entry among Python backtesting frameworks
  • Beautiful interactive charts — the best visualization of any framework in this category
  • Clean, Pythonic API that feels natural
  • Good documentation with example Jupyter notebooks
  • Fast execution for single-instrument strategies
  • Built-in optimization with visual heatmaps
  • Lightweight — few dependencies

Cons

  • Single instrument only — no portfolio-level backtesting across multiple assets
  • No live trading — backtesting only, you’ll need a separate system for execution
  • AGPL-3.0 license — even more restrictive than GPL for commercial/SaaS use
  • No built-in data feeds — must source data separately
  • Limited order types compared to Backtrader
  • No multi-timeframe support out of the box

Pricing

Completely free and open-source under AGPL-3.0. Note the AGPL license requires that if you offer the software as a network service, you must make your source code available — this can be a dealbreaker for commercial SaaS products.

Community & Support

  • ~8,100 GitHub stars, ~1,400 forks
  • Active GitHub Issues and Discussions
  • Growing Stack Overflow presence
  • Popular in beginner tutorials and YouTube content
  • 42 contributors

Who Should Use Backtesting.py?

Backtesting.py is ideal if you’re learning Python backtesting, want quick results with great visualizations, and are testing single-instrument strategies. It’s the fastest path from “I have an idea” to “here are the results.”

If you need portfolio-level backtesting, look at Backtrader. If you need speed for thousands of parameter combinations, VectorBT is dramatically faster. If you need live trading, consider Freqtrade (crypto) or QuantConnect (multi-asset).

Resources