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.1

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.1
File Size Uploaded
bt-1.4.1.tar.gz 57.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bt 1.4.1
File
bt-1.4.1-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
bt-1.4.1-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.1-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.4.1-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
bt-1.4.1-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
bt-1.4.1-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.1-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.1-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
bt-1.4.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
bt-1.4.1-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.1-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.1-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
bt-1.4.1-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
bt-1.4.1-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.1-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.1-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.1.tar.gz

Download URL bt-1.4.1.tar.gz
Size 57.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8ff30b9547c8f1637dba6918fdc2e8c3a5a8c7477d6ece4fe1ca5e496ecd3173
BLAKE2b-256 checksum
How to use checksums
f410651ab9f92fe766bec3a7b11b803c8b57052b642d0ad5aa270c200a873364
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.1-cp314-cp314-win_amd64.whl

Download URL bt-1.4.1-cp314-cp314-win_amd64.whl
Size 255.0 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
d8cd3e698ec842c4d3a02b5f28297f93aa2d8415bb75abd704b328ec27b47461
BLAKE2b-256 checksum
How to use checksums
5d3f918efd9c34176acbb0b83612469b49742d056194f35b629519e37c7a3353
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.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.1-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
ca368cfca2f038b8f52d5b151e70c33763b08c1a2721c0401f06625f5f5636f1
BLAKE2b-256 checksum
How to use checksums
9f8012ff5f66b5dfcb74061e399dcc2cd0520d357671dc800dc6d5bab0a0615d
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.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.1-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
9b7b2c387d46c4bf23d8bba0ac42d4b4f8d239883ec77be63db566c3c9cfdf65
BLAKE2b-256 checksum
How to use checksums
758d3449e4e651aade1cdc10cf4f70bb3fec048d94cb8d10a2072ea32543879b
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.1-cp314-cp314-macosx_11_0_arm64.whl

Download URL bt-1.4.1-cp314-cp314-macosx_11_0_arm64.whl
Size 623.3 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e4f50371f942af04db40da8eb7a30e0a4eb91b125f7bd72741c9f4638f4ef9f6
BLAKE2b-256 checksum
How to use checksums
b4ba794058c5cb4c7e5c4bc2120a80e91ac3331e0946af3fedf932a76cc3a8af
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.1-cp313-cp313-win_amd64.whl

Download URL bt-1.4.1-cp313-cp313-win_amd64.whl
Size 249.9 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
3671329dd0acca66be1769c5c34ff25d7c574bfe66962192251591b2742b38da
BLAKE2b-256 checksum
How to use checksums
1f7768ab769c337a546e497951f71694575766fa7d64b103ef51c326e10e7d5e
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.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.1-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
ea9d5da4726c130b4d3d34a4695be5d06800536cb53670d371a0d80f01111a98
BLAKE2b-256 checksum
How to use checksums
d34863514b7e724d665864bf7bac19e5e450b7d0fd355c0c82524b0f31f5415c
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.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.1-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
d28e830de702625444988dbd7b120e75fdfa9cba1cdf8246b38524f502b873ed
BLAKE2b-256 checksum
How to use checksums
7625efc7fb9f678d43fd10572261f90d3b9833c450165c62eb07caa7c5a91f53
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.1-cp313-cp313-macosx_11_0_arm64.whl

Download URL bt-1.4.1-cp313-cp313-macosx_11_0_arm64.whl
Size 622.4 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a0191f2d494d800ed6eedc46c3203ae2def2f391ea3f53c7186aa5170b8281de
BLAKE2b-256 checksum
How to use checksums
19fe80cfff85886f7acf2ea86f811fc7c9c28976c35432df9e710a66367d0c3a
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.1-cp312-cp312-win_amd64.whl

Download URL bt-1.4.1-cp312-cp312-win_amd64.whl
Size 250.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
c10a1d6be730a3ae16b423fca28e9a52f9cf9a46b2bc214cf3a9158b5085fef7
BLAKE2b-256 checksum
How to use checksums
b6e15574c8733751a56f6328dc855eb6bad34b8968406eca4632e7c53abeae07
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.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.1-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
baac5c156d63d34e040d516c0274543d65608ebc532dfe2e06ef819ab09c5883
BLAKE2b-256 checksum
How to use checksums
c354f6299ff611347a2180d9f5d3b5392888980f4efdd2884cc8247b52a6a329
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.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.1-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
c6f769bf2aa6f554554c0fba3a3905fee46e0e20d0f405ccce8a9e514017f782
BLAKE2b-256 checksum
How to use checksums
c814ea210b8434e750e31757d1351d2fce0ed0f7cecb60dde482e43380db4a64
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.1-cp312-cp312-macosx_11_0_arm64.whl

Download URL bt-1.4.1-cp312-cp312-macosx_11_0_arm64.whl
Size 624.1 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
71140dee3ba032b00397dc516438af84be50a76d99010bd47c40cbb18b87d253
BLAKE2b-256 checksum
How to use checksums
ee165c1a4a54a3d3989174b9f3cf36cd12c843e2404b555bd5060a0dbd1c2120
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.1-cp311-cp311-win_amd64.whl

Download URL bt-1.4.1-cp311-cp311-win_amd64.whl
Size 255.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
3519e260c51b3bde738b24e70996a3808ff09ce8f02db76a8673bed64800873a
BLAKE2b-256 checksum
How to use checksums
d385cc994433a8eedc7c759c319bdc4f3de5f862a155cc8a5f6e59f7082621de
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.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL bt-1.4.1-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
76dd7619283eb5e2e8c34eb49312de3c3d5f38f44d1a5049da97844f9ab82595
BLAKE2b-256 checksum
How to use checksums
6c21cbce519da5c044254b0df0f5aed875ffbf6b2895eeb5050f1d58a2ecfd24
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.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL bt-1.4.1-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
bc09ac2ecfe761fa98d682754adc7371aff93b77fa05cfc2966331602db11f6a
BLAKE2b-256 checksum
How to use checksums
8657c0037d35f94e46b30964e0a8adfec85afdb90a431663e76b3ff759427e61
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.1-cp311-cp311-macosx_11_0_arm64.whl

Download URL bt-1.4.1-cp311-cp311-macosx_11_0_arm64.whl
Size 621.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5b3703b1bebf8dcc62cbb7d0ae676496bec9abaeb8fac4b35ff2c4c3e560a935
BLAKE2b-256 checksum
How to use checksums
eb4fe22d838026a80d8c7e23ae668070d98634b688a7b5a329711e56160dbf94
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.4.1 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