Skip to content
broncatwPublic
forked from pmorissette/bt

About

Python 回测框架 bt(pmorissette/bt 二次开发版):修复 WeighInvVol 在资产存在缺失数据时污染其他资产波动率样本的 bug(issue #560),297/297 测试通过

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

bt — Flexible Backtesting for Python

Fork notice: This repository is a fork of pmorissette/bt (MIT License). Fix added on top of upstream main:

  1. WeighInvVol missing-data isolation (issue #560) — the volatility sample used dropna() across all selected columns, so a gap in one asset shrank (or, with an all-missing asset, eliminated) the sample used by every other asset: adding a gappy third asset silently changed the A:B weight split, and a panel containing an all-missing asset produced an empty weight map (strategy silently stops rebalancing). Each asset's inverse volatility is now estimated from its own valid observations within the same lookback window. Two regression tests added; full suite 297/297 green.
  2. 中文说明:本仓库为 bt 的二次开发版本,修复了 WeighInvVol 在任一资产存在缺失数据时污染其他资产波动率样本(最坏情况导致权重为空、策略静默停止调仓)的问题。

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.

About

Python 回测框架 bt(pmorissette/bt 二次开发版):修复 WeighInvVol 在资产存在缺失数据时污染其他资产波动率样本的 bug(issue #560),297/297 测试通过

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages