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.

Release files for bt 1.2.3

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.2.3
File Size Uploaded
bt-1.2.3.tar.gz 50.4 kB Details

Built distributions (wheels)

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

Total release size: 14.9 MB

Release files / bt-1.2.3.tar.gz

Download URL bt-1.2.3.tar.gz
Size 50.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cb8075f3090e762ae75314aa5f15bcabe6252d9c2cd4b9eecb58be0b2e68a1f5
BLAKE2b-256 checksum
How to use checksums
ddd9451378df38b6e5a36ae0564785999e6f15cef1ad8e1e4f8e34c888c29448
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.2.3-cp314-cp314-win_amd64.whl

Download URL bt-1.2.3-cp314-cp314-win_amd64.whl
Size 230.9 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
347425025932b917a43932a5bcb7b2cebe9f8c82f3ba45b9f9c515d039f6d74c
BLAKE2b-256 checksum
How to use checksums
c40cdcb4ca23baf25d22e56229825a570b77f4fdb61690d39942e375cb18492b
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.2.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.2.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.5 MB
Tags CPython 3.14 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ff617e44954ad851fc9b21ee24db2f82d3b38634755e98a0696c9e5fbe708785
BLAKE2b-256 checksum
How to use checksums
8cde90d6cb646d2c848bed13419743639fa5b96a843ecef3a8e70239c00afc65
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.2.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.2.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.5 MB
Tags CPython 3.14 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
cd20675f84061ad07dada7257bccee1a2c65632e3d5fb17799089ce6e2c34c08
BLAKE2b-256 checksum
How to use checksums
7f7fad74ec0c0d97280d91c5bebea6e96824d945dea2b0fe81cb78734547e8de
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.2.3-cp314-cp314-macosx_11_0_arm64.whl

Download URL bt-1.2.3-cp314-cp314-macosx_11_0_arm64.whl
Size 574.8 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ba0e25961c2158a9af529a5285eec567334812bc22261dd7b7312c3374910d4b
BLAKE2b-256 checksum
How to use checksums
53ad4ace6897493c98e91a8f5aff53af297d36c8c0d88b2e721441ce33d6260c
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.2.3-cp313-cp313-win_amd64.whl

Download URL bt-1.2.3-cp313-cp313-win_amd64.whl
Size 227.1 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
24d619ef6933a6b4d91f7030e60062e36390c4fe13a84557ad28b540dded450e
BLAKE2b-256 checksum
How to use checksums
a735bee1e994f9136a65d1a6135fc9d7baf834021490d185316a30a98e2549db
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.2.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.2.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.5 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
6b9ac28794583bb3d8877cacd158c7543484baa1da76b174f3513c0f6ebd2bbe
BLAKE2b-256 checksum
How to use checksums
38e28b59cd294e5decd0bb6bdd87df262ad53d1ea84b0b004530a8561c9c8b46
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.2.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.2.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.5 MB
Tags CPython 3.13 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
bd2a72ad8e12291d3f887f5447abdf77e9efed5b4036592708c9880fb3230efe
BLAKE2b-256 checksum
How to use checksums
2034749b0ee3ad603743a91b68d45e9e4bc70dcc0ebbe46e2e40de46b3ead820
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.2.3-cp313-cp313-macosx_11_0_arm64.whl

Download URL bt-1.2.3-cp313-cp313-macosx_11_0_arm64.whl
Size 573.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1c999313f2700c74957fdffaa3db0c2460460779e210bf1ef4f6269955859548
BLAKE2b-256 checksum
How to use checksums
8e38d1b47bf897cd962b642ef414df495d457126c6c546f38e34db0532a92e10
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.2.3-cp312-cp312-win_amd64.whl

Download URL bt-1.2.3-cp312-cp312-win_amd64.whl
Size 227.7 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
4a900d0fca7f70686d11403a54893645cfc7198db0cd2833993cf95df36fdc9f
BLAKE2b-256 checksum
How to use checksums
47baa4ae279e26e83b9a752dc3e48ba73c41e3d4d1fc30ea04001880db4a554c
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.2.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.2.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.5 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
898cb48fe6ee773cbcc3d595d7de176d8f869be68b972d1a6c31f21e2cfc7497
BLAKE2b-256 checksum
How to use checksums
e4db973edce2aa0dac9d1c1a0513efda13802c55bbbf3be769a2029e97d6bbe9
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.2.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.2.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.5 MB
Tags CPython 3.12 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
f5e47e1d0fe65b88c4aedd4a4e23aa40941dc8f62fd7223881e2c735cf0fc5b9
BLAKE2b-256 checksum
How to use checksums
f90f3885b8ac095ecdcabc355dfa1cd10d075f3706bba3c0d3835a4aa9fc77ad
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.2.3-cp312-cp312-macosx_11_0_arm64.whl

Download URL bt-1.2.3-cp312-cp312-macosx_11_0_arm64.whl
Size 574.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e08d06a28d97e9318de18d15d5380432c641885fa76679f63eea3712dc4dcee1
BLAKE2b-256 checksum
How to use checksums
8959072399b45b0a8705b3aecc2a8df84513c2ad4d565111e8e26b2bf843ac78
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.2.3-cp311-cp311-win_amd64.whl

Download URL bt-1.2.3-cp311-cp311-win_amd64.whl
Size 231.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
19e957bfefe302bc2ee05ada2b12b5f0f8d63a420a2f5d065db954fc28c6210e
BLAKE2b-256 checksum
How to use checksums
7b69d935a915ad3eeea1265811dd2ab11f19f2589e9386d0d6d86ad5570349eb
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.2.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.2.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 1.3 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
4ebefa106c954cdd691e394e3c77c07edbdbf48199e274b000e451b74205ddd6
BLAKE2b-256 checksum
How to use checksums
8605d7fa792ba5e28b9fa617ae05dfcf806fd117b861cb086b620449cb6f973d
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.2.3-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.2.3-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 1.3 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
89b52c1b70d2ac5c5586259f9c1770260886e6aadaa2400484fda1b0e8079128
BLAKE2b-256 checksum
How to use checksums
2c5ca88fe367877f6f9caee96d99bbc0842d6ed54c2a3d62941725f5fd5faa29
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.2.3-cp311-cp311-macosx_11_0_arm64.whl

Download URL bt-1.2.3-cp311-cp311-macosx_11_0_arm64.whl
Size 571.6 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0170f32f448feee6117f3bf9b11636a771f63ad258a226f243b896536adb7c6b
BLAKE2b-256 checksum
How to use checksums
cd74c6963cc376c848505a73b1d08c5bbd23a64c8cb53e71cb9b2ef054653455
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

This release

1.2.3 This release

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