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.7.tar.gz (341.2 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.7-cp310-abi3-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.10+Windows x86-64

wbt-0.1.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.8 MB view details)

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

wbt-0.1.7-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.8 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

wbt-0.1.7-cp310-abi3-macosx_11_0_arm64.whl (9.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

wbt-0.1.7-cp310-abi3-macosx_10_12_x86_64.whl (10.4 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: wbt-0.1.7.tar.gz
  • Upload date:
  • Size: 341.2 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.7.tar.gz
Algorithm Hash digest
SHA256 b4c400fcde06b1dc14879c49fff4bc67e80d0fd70eef6af5867a5b3ed32ba73f
MD5 29171b82342cd365a4d43190baba4495
BLAKE2b-256 ad87337d04375b78ec37e98f3dec1f4dfc12db76b6076c8ebf19ebc3cfe0d090

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.7.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.7-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: wbt-0.1.7-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 11.5 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.7-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 cea88b3ff9ea951e52bd6eb5b20d77f90bc981079ce493940a4cedaed59954e7
MD5 cabe9e6eae33a6050384e3ca4c052ddf
BLAKE2b-256 657e4dd81587036fb9e8b156f3f4dd62a8bbc69d8feaa8949257f28e08cc82bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.7-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.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wbt-0.1.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fd9ff5f88133ba861b65a2c4e6c52278c232058699e75a1816770e8be4d84b3b
MD5 71025f879cced66441146ee3550630a7
BLAKE2b-256 383703d10c4b680688e7871614bdf64630a95296102832af3c5eab42dcb8feaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_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.7-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for wbt-0.1.7-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 11914f4e3ee16f0c788234914df492420d50cecf0d9ed179686cf530c963894c
MD5 b0ede535ce7b6241b1c423dfc890ea6b
BLAKE2b-256 98258316d48311966bee39b564c4a20a052f551a4c5abd6d3493098a69ee1d27

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.7-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_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.7-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: wbt-0.1.7-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 9.5 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.7-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4a2eb66091722f568367b71dd5f689665a764019e050a9dc42422b3cac518379
MD5 d4d9775e6d8a7f183bc72438df87bd55
BLAKE2b-256 3ce5a8f5afc7a178ff93735fad284b0fae81bd8ee1c47f05268d5d629c9283ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.7-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.7-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: wbt-0.1.7-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 10.4 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.7-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1bbb77d5f27528d139295339c21bd64f73e2bb0bcd9b362a53cb06e4c4e75a96
MD5 1c8ee62f162f6eb2e5d41cc04b815381
BLAKE2b-256 723a594d95d260e1ce027b0a5342427639706473e681cc985a548c18298263b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for wbt-0.1.7-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