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

Uploaded CPython 3.10+Windows x86-64

wbt-0.1.6-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.6-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.6-cp310-abi3-macosx_11_0_arm64.whl (9.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

wbt-0.1.6-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.6.tar.gz.

File metadata

  • Download URL: wbt-0.1.6.tar.gz
  • Upload date:
  • Size: 337.4 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.6.tar.gz
Algorithm Hash digest
SHA256 dc66e69ad608461e2570bf4ac75dac7562038ea5ffbd252836ae176b636fde91
MD5 f2ed0cd723ba94b7387631c125dff400
BLAKE2b-256 53db95c213f6388a575e45cc3c96f9c63cac68d6a218e535bf52d73a3df48eaf

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: wbt-0.1.6-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.6-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 dd11179830b38025f4d44994b3be316043554810aa26618cea1b09b7700b8d2e
MD5 9fcc1f3258b650e0e687d54febed11b7
BLAKE2b-256 a7fd2dbcebac046b9f1bada2f6404adafa46fcd07e2769b6dd5efa6eb50b7358

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for wbt-0.1.6-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b5cab6f3bc47a8c27bb06129c43bb65bf03121a9707e53566f497d8b67016e59
MD5 ee34488b8cf5fa7d55bdadc5d66401dc
BLAKE2b-256 c0e60b9d84f48b31b431382076b3bdf8e49c82594d77c96742bd552a26a6fe5e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for wbt-0.1.6-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 48e17c30a5d5e2c8bdab6044d6491cec7355c44c4aa084e0428078c7793d4e66
MD5 511980f12eaff7dbc07d63bf794da784
BLAKE2b-256 e60780ca9c25595470b87ee52a715978c78fe4eac83e2625b64abd61d589701d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: wbt-0.1.6-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.6-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 636328e6fbb63f6160a7a7c2cf60e59284b2234c1d76203229963acf3e9535d2
MD5 8a28dd9e43d7c129f76e3cf91efdfac2
BLAKE2b-256 a54cf4899f212c46301d6d3bbc0ac7da204d41a0664463afc0169ccf72f0fc41

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: wbt-0.1.6-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.6-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 271bd5450f29200a61cbd576159a4bdfb14246875fae257e524416ae460baa2a
MD5 17e710201776b2b22d54578ade2bc6de
BLAKE2b-256 1494291bbfd4f79f9abdb9ff50c7f90c1fef781dc89e9516966ae48ec52b78cc

See more details on using hashes here.

Provenance

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