Maintained

bt guide

bt shines when the question is “how should I allocate?” rather than “when should I trade?”.

Last reviewed · Also known as bt backtesting, ffn

Quick answer

bt is a Python backtesting framework for portfolio strategies. A strategy is a stack of reusable algos, for example run monthly, select all, weigh equally, rebalance, and strategies can be nested into trees. It handles multi-asset portfolios naturally and uses the ffn library for statistics and charts.

bt at a glance

Category
Research & vectorized backtesting
Language
Python
License
MIT
Does
Backtesting, Performance analytics
Markets
Stocks, ETFs
Best for
Asset-allocation, rotation and risk-parity backtests across many instruments.
In AlgoGP
Use the exported strategy object from your own code

Pick it if

Asset-allocation, rotation and risk-parity backtests across many instruments.

Watch out for

Not suited to intraday or signal-heavy single-asset trading.

bt strengths and limitations

Strengths

  • Composable algo stacks and nested strategies.
  • Multi-asset by design.
  • Good statistics via ffn.

Limitations

  • Not suited to intraday or signal-heavy single-asset trading.
  • No live trading.

bt example

pip install bt
Install
import bt
data = bt.get("spy,agg,gld", start="2015-01-01")
strategy = bt.Strategy("EqualWeight", [bt.algos.RunMonthly(), bt.algos.SelectAll(),
                                      bt.algos.WeighEqually(), bt.algos.Rebalance()])
result = bt.run(bt.Backtest(strategy, data))
result.display()
Equal-weight monthly rebalance.

Use bt with AlgoGP

AlgoGP does not add bt to projects automatically yet, but nothing stops you using them together. Every AlgoGP project exports plain Python with a framework-neutral strategy object, so you can call it from bt yourself.

  1. 1

    Design and backtest in AlgoGP

    Describe the idea to the copilot and backtest it on AlgoGP’s normalized history.

  2. 2

    Add any bridge to get the adapter core

    Adding a bridged framework (for example Backtrader) writes frameworks/algogp_bridge.py, whose StrategyBridge.on_bar() returns the orders the strategy wants.

  3. 3

    Call it from bt

    Feed bt’s bars to StrategyBridge, place the returned orders with bt, and report fills back with sync().

bt connects to Yahoo Finance. Each guide shows how to create the API key, which you can store in the project’s Keys & secrets.

bt: frequently asked questions

bt vs backtesting.py?

bt is for multi-asset allocation; backtesting.py is for single-instrument trading rules.

bt alternatives

Independent guide. AlgoGP is not affiliated with bt. Features, licenses and maintenance change; check the official documentation. Nothing here is investment advice.

Build a strategy