Skip to main content

bt — Flexible Backtesting for Python

Build Status PyPI Version PyPI License

Build, test, and compare investment strategies from reusable Python components. bt combines strategy logic with historical price data, tracks portfolio positions and transactions, and provides performance statistics and charts through ffn.

  • Compose strategy logic: combine algorithms for scheduling, security selection, weighting, and rebalancing.
  • Build portfolios of strategies: nest strategies and securities in a common tree.
  • Model trading costs: configure commissions and transaction cost models.
  • Compare results: inspect returns, weights, transactions, drawdowns, and other statistics.

Install

pip install bt

See the installation guide for additional details.

A first backtest

This example uses synthetic prices, so it runs without downloading market data:

import numpy as np
import pandas as pd

import bt

prices = pd.DataFrame(
    {
        "asset_a": np.linspace(100, 120, 252),
        "asset_b": np.linspace(100, 110, 252),
    },
    index=pd.bdate_range("2020-01-01", periods=252),
)

strategy = bt.Strategy(
    "equal_weight",
    [
        bt.algos.RunMonthly(),
        bt.algos.SelectAll(),
        bt.algos.WeighEqually(),
        bt.algos.Rebalance(),
    ],
)
result = bt.run(bt.Backtest(strategy, prices))
result.display()

The strategy selects both assets, gives each equal weight, and rebalances monthly. Replace the synthetic prices with your own data to explore a strategy. Backtest results depend on data quality and modeling assumptions; they do not predict future performance.

Explore the documentation

The published documentation is at https://pmorissette.github.io/bt/.

Contribute

See the development guide for environment setup, tests, documentation builds, and Copier template updates. Report bugs and propose improvements through GitHub issues.

bt is released under the MIT license.

Metadata

Release files for bt 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bt 1.4.0
File Size Uploaded
bt-1.4.0.tar.gz 57.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bt 1.4.0
File
bt-1.4.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
bt-1.4.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 Details
bt-1.4.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
bt-1.4.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
bt-1.4.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
bt-1.4.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 Details
bt-1.4.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
bt-1.4.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
bt-1.4.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
bt-1.4.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 Details
bt-1.4.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
bt-1.4.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
bt-1.4.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
bt-1.4.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 Details
bt-1.4.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
bt-1.4.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details

Total release size: 16.3 MB

Release files / bt-1.4.0.tar.gz

Download URL bt-1.4.0.tar.gz
Size 57.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ad8d3d53e2be412e2a39bee9aadd9fddf121dedd3791293030595932bec0ee83
BLAKE2b-256 checksum
How to use checksums
9dfe25a8609520b2a1c88a7c108c380c3a0818637f3d1c41a92ce6c916dc3dd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp314-cp314-win_amd64.whl

Download URL bt-1.4.0-cp314-cp314-win_amd64.whl
Size 254.7 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
6b716a992bd09d9aacd0580853cf9f9ebc00213360a554cd6e23ad83b3c58d97
BLAKE2b-256 checksum
How to use checksums
5a485d826c011c519ed5ce52c663c316349f943c878c2bed10737ddb7a7a3448
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.6 MB
Tags CPython 3.14 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
a2ea67a044443a8f4b246bb79f97e480b6cef68d9bf673782c585736f9a2e1be
BLAKE2b-256 checksum
How to use checksums
cba2c4377b6d769c1cd525156a68b7156d1bdb5410f383c16699e67d3efb6fc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.6 MB
Tags CPython 3.14 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
949d220218c9f8f012baa7a32f0e30fbfcba773e47d6288db36a5a4380f4b8c9
BLAKE2b-256 checksum
How to use checksums
8f5e18742b0674949cf95ec0b697ea00b567c85bd2c59ba189fc38eb69733567
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL bt-1.4.0-cp314-cp314-macosx_11_0_arm64.whl
Size 623.1 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
67d4e202dfc3e893d237d3f9750071fe6474cbded406b634fdf403a311c68d01
BLAKE2b-256 checksum
How to use checksums
dd4d03040716117459cd94e410deecdf6cea1aad0eadbf7f238266312a3cd43e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp313-cp313-win_amd64.whl

Download URL bt-1.4.0-cp313-cp313-win_amd64.whl
Size 249.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
4bc6d15419ed0a5acdad2518f906818e02132e5a9eb84cb5c348af87e9a9fd31
BLAKE2b-256 checksum
How to use checksums
a57e8602bc7e7209c4962efc290f7f1a84a0bd924f91b6a14d4f39c02d1cfca4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.6 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
f69a131751f374f3dc43e79b6005b8165af70e44bc9f79fd13ccc86787e327ee
BLAKE2b-256 checksum
How to use checksums
e834efbce9e67920fea277cfa582d796e7c89094bab78637130d0ecd71c827f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.6 MB
Tags CPython 3.13 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
d0fff6d1aed1abb0ba08888875a3903bc3aea25f24c7aa17ffab057e25de7412
BLAKE2b-256 checksum
How to use checksums
cbeae8f5ab7163d0b142a908f60592dec968bb1456af681d2bf794711b4a7ba9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL bt-1.4.0-cp313-cp313-macosx_11_0_arm64.whl
Size 622.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4f2e431308619b1e7b88d7fc396b6c0755380b0aa8c6f4c8166d450a84da06dc
BLAKE2b-256 checksum
How to use checksums
bd35e873fa0c0da9bee2953305fc52fa4b36b9c911f99738bc97953658a592bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp312-cp312-win_amd64.whl

Download URL bt-1.4.0-cp312-cp312-win_amd64.whl
Size 250.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
405272064e7ffd9ec398f7ae38b68193cf12c74d0b3f1c9f7fedb593a91c8614
BLAKE2b-256 checksum
How to use checksums
4611fa13978b26ea2999eaa0e60c0e80af3e651a9353842347b815d202cbb0e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
08ce34bdf0d0a625eda6a3ed2a3dcb5e7600f64a5c96856e3a11e31789b9244b
BLAKE2b-256 checksum
How to use checksums
7e0c948f6d6ff05fa05bfa087a8b0c36a6d17661a3fc72560e526001702756bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.7 MB
Tags CPython 3.12 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
80a1a6f9b82cad47fd6b899266b672d267caeaa6add6e0d3d7ceeeb7e3071c4b
BLAKE2b-256 checksum
How to use checksums
fe8204e81cff8a8eb3e2400d8d8138d7de367a988cf9813d87755dbffe1a8928
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL bt-1.4.0-cp312-cp312-macosx_11_0_arm64.whl
Size 623.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1232985fa3e449ab665765ffb0e150d52084e1ee6104a4e3c3925c62dc1c2aa5
BLAKE2b-256 checksum
How to use checksums
5d1479182b654a27974b2069d8dcc3c41bed38cf3a75c0f0a16b5ba21bd20df1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp311-cp311-win_amd64.whl

Download URL bt-1.4.0-cp311-cp311-win_amd64.whl
Size 254.8 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
669174ebd84facb3fbcdb85cd530a5061aca7f372db5e868e4179eafdaf0d514
BLAKE2b-256 checksum
How to use checksums
548e7516f4c8e488f46d75d1a2b28a2c8266ebd2b2fd399828da91a2ce129cf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.4 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ae2753f6cdde654964da25cb71118ee1dd8d8811009458522c96fb1b0701a724
BLAKE2b-256 checksum
How to use checksums
be5c81296197f47e300d257b65836791cdc8095322989c466fc91a3a5a61cae1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.4 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
e9d339cc06422efe5b5db8b946a77ab1993b721e829f63edccf93b35e18e716c
BLAKE2b-256 checksum
How to use checksums
129dc6b307135873c125dad83d01f60689e407d0fe16c80d91ab1f0c7636febc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / bt-1.4.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL bt-1.4.0-cp311-cp311-macosx_11_0_arm64.whl
Size 621.2 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2ead5124529dd774d5382f75e8c1217732e532f54c2a7b20614f004d911be053
BLAKE2b-256 checksum
How to use checksums
04d089a73902fa417c9689ad05f701a62c3f610170ee22f07b086cfa5775de4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

1.4.1

17 release files

This release

1.4.0 This release

17 release files

1.2.3

17 release files

1.2.2

26 release files

1.2.1

25 release files

1.2.0

25 release files

1.1.5

6 release files

1.1.3

21 release files

1.1.2

26 release files

1.1.1

16 release files

1.0.1

23 release files

1.0.0

9 release files

0.2.9

9 release files

0.2.8

8 release files

0.2.7

1 release file

0.2.6

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.15

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page