bt — Flexible Portfolio Backtesting Framework for Python

bt is a Python backtesting framework designed for portfolio-level strategy testing. Built on top of ffn (Financial Functions for Python), it uses a tree-based structure to compose modular, reusable strategy components. Where most frameworks focus on individual security signals, bt focuses on asset allocation, rebalancing, and portfolio optimization.

Language: Python | License: MIT | Price: Free | GitHub Stars: ~2,800


Key Features

  • Tree-based strategy composition — each node has its own price index
  • Modular Algo stacks that can be mixed and matched like building blocks
  • Built-in charting and reporting with matplotlib
  • Multi-backtest comparison with comprehensive statistics
  • Rebalancing and portfolio optimization support
  • Built on ffn for financial calculations (Sharpe, drawdown, etc.)
  • Long/short strategies and multiple asset classes
  • Pandas-native — works directly with DataFrames

Strategy Example

import bt

# fetch data
data = bt.get('spy,agg', start='2010-01-01')

# create strategy
s = bt.Strategy('s1', [
    bt.algos.RunMonthly(),
    bt.algos.SelectAll(),
    bt.algos.WeighEqually(),
    bt.algos.Rebalance()
])

# create backtest and run
test = bt.Backtest(s, data)
res = bt.run(test)

# plot results
res.plot()
res.display()

The composable algo pattern is bt’s signature. You chain together small, reusable algorithm blocks — selection, weighting, rebalancing — to build complex portfolio strategies without writing custom logic.

Data Sources

  • Yahoo Finance via bt.get() helper (uses yfinance)
  • Any Pandas DataFrame — CSV, database, API, whatever you can load
  • Compatible with any OHLCV data source

Pros

  • Excellent for portfolio-level backtesting and asset allocation
  • Very Pythonic API — clean, modular, composable
  • Tree structure makes complex multi-asset strategies intuitive
  • Good built-in visualization and statistics
  • Active maintenance (latest v1.1.5, March 2026)
  • MIT license, fully open-source
  • Strong Pandas ecosystem integration
  • Multi-backtest comparison out of the box

Cons

  • Primarily designed for portfolio rebalancing — less suited for tick-level or high-frequency strategies
  • No built-in live/paper trading
  • Smaller community compared to Backtrader or Zipline
  • Documentation could use more examples
  • Still labeled as Alpha despite years of development
  • No broker/exchange integration
  • Limited order types (no stop-loss, limit orders natively)
  • Not designed for individual security signal strategies

Pricing

Free and open-source under the MIT license. No paid tiers.

Community & Support

  • ~2,800 GitHub stars
  • Active development with 625+ commits, 10 releases
  • 1,700+ dependents on PyPI
  • Small but dedicated user base focused on portfolio allocation

Who Should Use bt?

bt is the right choice for portfolio allocation strategies — equal weight, risk parity, momentum rotation, mean-variance optimization. If you think in terms of “which assets to hold and how much” rather than “when to enter and exit,” bt’s composable algo pattern will feel natural.

If you need individual trade signals with stop-losses and limit orders, use Backtrader or Backtesting.py. If you want cross-sectional factor research, try Zipline. For speed on large datasets, check VectorBT.

Resources