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.3.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.3.0
File Size Uploaded
bt-1.3.0.tar.gz 55.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bt 1.3.0
File
bt-1.3.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
bt-1.3.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.28+ x86-64, Linux glibc 2.17+ x86-64 Details
bt-1.3.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ ARM64, Linux glibc 2.17+ ARM64 Details
bt-1.3.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
bt-1.3.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
bt-1.3.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.28+ x86-64, Linux glibc 2.17+ x86-64 Details
bt-1.3.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.3.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
bt-1.3.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
bt-1.3.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.3.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ ARM64, Linux glibc 2.17+ ARM64 Details
bt-1.3.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
bt-1.3.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
bt-1.3.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.3.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.3.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details

Total release size: 16.2 MB

Release files / bt-1.3.0.tar.gz

Download URL bt-1.3.0.tar.gz
Size 55.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c0ff4fb24456aff7e35f29353f273ecc22489d1e59f9bda580de47c306ac3ab4
BLAKE2b-256 checksum
How to use checksums
332068b883ad5fd4b9b7c6f9a7609dafba95e58acbb698722e405922a990974e
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.3.0-cp314-cp314-win_amd64.whl

Download URL bt-1.3.0-cp314-cp314-win_amd64.whl
Size 252.6 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
b437f48602a1d5c0c2ea8cee7b4df92845ab2fcc6f9801b2bf1e496b7171b53c
BLAKE2b-256 checksum
How to use checksums
76f2dac0d740f6f498ade34dcc5820c9f1ef91052faea512ea896d403170b38a
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.3.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.3.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
aa65f29dde5395bef5d2a63c29fb764171510399b97d653220626380faa4215d
BLAKE2b-256 checksum
How to use checksums
b53adc6358ebdd6a7a16186de6880150e7519355878e34a795d26dd572c8263a
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.3.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.3.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
060564f58f86bbed8ac2c4200a431601f41fab97e612b8cf163b39b8d6eda555
BLAKE2b-256 checksum
How to use checksums
bab603d2f95bd3aadc322e39293b10fe236729c408ceae42cf57b4de3b06b029
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.3.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL bt-1.3.0-cp314-cp314-macosx_11_0_arm64.whl
Size 620.2 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
037e6f46c248b26dccbdcc927389415e4b000628448838ba8a06a830e2462a3b
BLAKE2b-256 checksum
How to use checksums
80896606376ef69ba927fa05d89addf743992e989a3ab3a69f67321990bc262c
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.3.0-cp313-cp313-win_amd64.whl

Download URL bt-1.3.0-cp313-cp313-win_amd64.whl
Size 247.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
66371f97843f9308b358798124c0c10f2abd321c1b1584fc5dc8d5425b34c4bb
BLAKE2b-256 checksum
How to use checksums
d9e3752a63bbe4dfcd2cd67fe9a5fa8a593c0b33d934f2b78ff93e654253a5a5
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.3.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.3.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
067ebecd28172abbb0c3edfeab59250daab24b57a61d16bbdaa7aae813b9a76e
BLAKE2b-256 checksum
How to use checksums
2cbf2e87c9309ada88197f575f76ae9bd1b662bd0b94d8c99caa82686bb81d98
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.3.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.3.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
f2164fdeafb59b695894d2d2da446b90dad1a68c45e8b509d1f7a3f7c32a927e
BLAKE2b-256 checksum
How to use checksums
691f2bc482d4ceb14a60a771315277da82c6b111d370689ba9487b447216a6ea
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.3.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL bt-1.3.0-cp313-cp313-macosx_11_0_arm64.whl
Size 619.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5f8925baacbe449402f065cb2bc212f50e3276ba53e81834b524c7dc78ccd823
BLAKE2b-256 checksum
How to use checksums
e008ef0428f45b19c8aaa8be9b1f5fadcf62fb4abb80717951b79d8d42d7d118
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.3.0-cp312-cp312-win_amd64.whl

Download URL bt-1.3.0-cp312-cp312-win_amd64.whl
Size 248.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
8477b7d5bea69871a0c7e2644b4b47c43105eca9c2832e467fc0e100f963df36
BLAKE2b-256 checksum
How to use checksums
34d094ebc1d87e22441c575fc745ffaa5fa226fec0680b77853f8e2e36444551
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.3.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.3.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
4679732dad84e7ffc1946c4245dbba1058addcbfed2db4871d888d3f9a61ac08
BLAKE2b-256 checksum
How to use checksums
c68aaf2f4f6d652ed2d94b84c210d4e35fc7a9d01bef84243c005e8571033040
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.3.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.3.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.6 MB
Tags CPython 3.12 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
d4a711f6e92bf7048a23a8f1a93287aafa59a5c33b9281465ecafab8a8e5304e
BLAKE2b-256 checksum
How to use checksums
5ed017a9777ae093c26165dc01d5a650d4f15772cbb24807435f8559f01097fe
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.3.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL bt-1.3.0-cp312-cp312-macosx_11_0_arm64.whl
Size 620.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bc7126ec7a8b804d3e39513eda1df095ac7f6ce5bf65d8326d2c36eab35c57df
BLAKE2b-256 checksum
How to use checksums
e41135353019f18ddf48f77b0cc5f15b93386f86826f16bf42c55f986e63a1c4
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.3.0-cp311-cp311-win_amd64.whl

Download URL bt-1.3.0-cp311-cp311-win_amd64.whl
Size 252.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
19ad66f0eebbe232e657261d2c6e1665090a4ba4cd2824617bbfbd4195e6ebbb
BLAKE2b-256 checksum
How to use checksums
dc8576a80fb4da2383384dd8add159ddb9f274f5293277f0f924731f66317a93
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.3.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.3.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
d92465c3f9907382e45fd0fa22d0edfd4b8cc64a9510f4cdd077f16a22330b3d
BLAKE2b-256 checksum
How to use checksums
3dd1dff8a53fb16900ae71e85e3a866e0f48be5a28f8e88662ce2658d8748282
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.3.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.3.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
b6a327ae084a2dbc95cd842732c7665cbb624e8bc6d99bb69918717405388486
BLAKE2b-256 checksum
How to use checksums
124918fe4a048b63584b0202615eafdacbdbf9caa545632c1703b57634fd9b82
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.3.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL bt-1.3.0-cp311-cp311-macosx_11_0_arm64.whl
Size 618.1 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e7e12a43c5718b646484320e8aefcbd09f2d38c4160adb7ebfdaaee87885fed2
BLAKE2b-256 checksum
How to use checksums
1a774dc40c68221a84d2c39fd9826c9b59ce86718ad420ec0bb6c7e114918256
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.3.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