Skip to main content

Weight-based backtesting engine for quantitative trading

Project description

wbt Python Package

Python API for the wbt Rust backtesting engine.

中文文档

Development Objectives

This Python subproject aims to provide a practical research-facing interface for weight-based backtesting while keeping the heavy computation in Rust.

Design priorities:

  1. Keep data input flexible for common research formats.
  2. Return analysis-friendly outputs as pandas objects.
  3. Preserve one consistent metric schema across stats outputs.
  4. Provide plotting utilities that work directly on backtest outputs.

Project Layout

This directory is an independent Python subproject.

python/
|-- pyproject.toml
|-- README.md
|-- scripts/
|-- tests/
`-- wbt/

The Rust crate remains one level up at ../Cargo.toml. maturin builds the extension module from there.

Installation And Local Setup

Requirements:

  • Rust toolchain
  • Python 3.10+
  • uv

Setup:

cd python
uv sync --extra dev
uv run maturin develop --release

Quick Start

import pandas as pd
from wbt import WeightBacktest

df = pd.DataFrame(
    {
        "dt": [
            "2024-01-02 09:01:00",
            "2024-01-02 09:02:00",
            "2024-01-02 09:03:00",
            "2024-01-02 09:04:00",
        ],
        "symbol": ["AAPL", "AAPL", "AAPL", "AAPL"],
        "weight": [0.5, 0.2, 0.0, -0.3],
        "price": [185.0, 186.0, 186.5, 184.5],
    }
)

wb = WeightBacktest(
    df,
    digits=2,
    fee_rate=0.0002,
    n_jobs=4,
    weight_type="ts",   # "ts" or "cs"
    yearly_days=252,
)

print("all:", wb.stats)
print("long:", wb.long_stats)
print("short:", wb.short_stats)

print(wb.daily_return.head())
print(wb.dailys.head())
print(wb.pairs.head())

print(wb.segment_stats("2024-01-01", "2024-12-31", kind="多空"))
print(wb.long_alpha_stats)

Accepted Inputs

The data argument accepts:

  • pandas.DataFrame
  • polars.DataFrame
  • polars.LazyFrame
  • file path as str or Path

Supported file formats from path input:

  • csv
  • parquet
  • feather
  • arrow

Required columns:

Column Type Meaning
dt datetime-like Bar end time
symbol str Instrument code
weight float Target position weight
price float Price used for return calculation

Notes:

  • Null values are not allowed.
  • weight is rounded by digits before backtest.

Main API Surface

Imports:

from wbt import WeightBacktest, backtest, daily_performance

Primary class and helper:

  • WeightBacktest(...): main entry.
  • backtest(...): convenience wrapper returning WeightBacktest.
  • daily_performance(...): standalone metric utility.

Core properties and methods:

  • stats, long_stats, short_stats
  • daily_return, long_daily_return, short_daily_return
  • dailys, pairs
  • alpha, alpha_stats, bench_stats
  • segment_stats(sdt, edt, kind)
  • long_alpha_stats
  • get_symbol_daily(symbol), get_symbol_pairs(symbol)

Plotting Utilities

All plotting helpers are under wbt.plotting.

from wbt.plotting import (
    plot_backtest_overview,
    plot_cumulative_returns,
    plot_daily_return_dist,
    plot_drawdown,
    plot_long_short_comparison,
    plot_monthly_heatmap,
    plot_pairs_analysis,
    plot_symbol_returns,
)

Typical usage:

fig1 = plot_cumulative_returns(wb.daily_return)
fig2 = plot_drawdown(wb.daily_return)
fig3 = plot_pairs_analysis(wb.pairs)

# Optional HTML export
html = plot_backtest_overview(wb.daily_return, to_html=True)

Quality And Testing

Run checks from python/:

uv run pytest -v
uv run ruff format --check .
uv run ruff check . --no-fix
uv run basedpyright

Architecture Snapshot

repo-root/
|-- Cargo.toml
|-- src/
`-- python/
    `-- wbt/
        |-- __init__.py
        |-- _df_convert.py
        |-- _wbt.pyi
        |-- backtest.py
        `-- plotting/
            |-- __init__.py
            |-- _common.py
            |-- returns.py
            |-- risk.py
            |-- trades.py
            `-- overview.py

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wbt-0.1.8.tar.gz (349.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

wbt-0.1.8-cp310-abi3-win_amd64.whl (21.2 MB view details)

Uploaded CPython 3.10+Windows x86-64

wbt-0.1.8-cp310-abi3-manylinux_2_28_x86_64.whl (19.6 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

wbt-0.1.8-cp310-abi3-manylinux_2_28_aarch64.whl (17.9 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

wbt-0.1.8-cp310-abi3-macosx_11_0_arm64.whl (17.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

wbt-0.1.8-cp310-abi3-macosx_10_12_x86_64.whl (18.9 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file wbt-0.1.8.tar.gz.

File metadata

  • Download URL: wbt-0.1.8.tar.gz
  • Upload date:
  • Size: 349.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wbt-0.1.8.tar.gz
Algorithm Hash digest
SHA256 7bb449222a9f11df0cb570f6ddb8db1e056b17a63823451d7f54ec6013424496
MD5 c78495237185674f5590bd4b07c74f03
BLAKE2b-256 80c05423d30e3caba49b71ad6e81b5f7a60474f44e7d1e81e8937b55e8e8ad08

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.8.tar.gz:

Publisher: release.yml on zengbin93/wbt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wbt-0.1.8-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: wbt-0.1.8-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 21.2 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wbt-0.1.8-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 493f1b0f91a4311257faf70b4f3f1acae18bcadda140805e0ee6e278dd0b17aa
MD5 693fde57c6764ea32468d5f9181ff0d6
BLAKE2b-256 58d380a1fe9294439e6d7a6ce6d80756b8b1db564378f5e82765e5dde9007bdf

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.8-cp310-abi3-win_amd64.whl:

Publisher: release.yml on zengbin93/wbt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wbt-0.1.8-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: wbt-0.1.8-cp310-abi3-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 19.6 MB
  • Tags: CPython 3.10+, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wbt-0.1.8-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7079b278cc414d7fd32f2a7052a6223f7096216f914455fd4524e24b37a29f22
MD5 dab7e4b61dd2e1c6d02e6d4fd8251030
BLAKE2b-256 a30553ce717b89ee44bd982fed02eefeb9f9904dffd844aa48f41a2d0143de1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.8-cp310-abi3-manylinux_2_28_x86_64.whl:

Publisher: release.yml on zengbin93/wbt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wbt-0.1.8-cp310-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for wbt-0.1.8-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ce0ae777eab5039fa3ae89d4d8e42ef477675c9213c5b96102575f9290b78224
MD5 b988a6c9a624cebe05ddd7fac765dd11
BLAKE2b-256 09b110d057f48e6ca18b852e0d4ca6753d0fb93df7ebcc3f84ae8ee9faf09519

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.8-cp310-abi3-manylinux_2_28_aarch64.whl:

Publisher: release.yml on zengbin93/wbt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wbt-0.1.8-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: wbt-0.1.8-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 17.3 MB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wbt-0.1.8-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0d8b4adef64f843a81604cf6b528056878c096fdf7e3283b1f4e9fb9f756b7c3
MD5 5685f42051ecce40884e29540eebd2c6
BLAKE2b-256 67000b20cfd81f66be0ec7735bbbda962353c129d715f51e9892dcc25a6feeb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.8-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on zengbin93/wbt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wbt-0.1.8-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: wbt-0.1.8-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 18.9 MB
  • Tags: CPython 3.10+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wbt-0.1.8-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 693913b1e6a66d234e43ed9d0a6e7195f201083a7935e311502cefa0d6200aaa
MD5 afb11dc46ae15c0ca81d8c200e10781b
BLAKE2b-256 d5555c1c58a1dcb6dd832f684c2f6ece617c44fbefccde67bdaa68862d597acc

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.8-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on zengbin93/wbt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page