Skip to main content

backtest_lint —— 回测体检工具

tests License: MIT

中文 · English

A CLI that audits a backtest before you believe it. 9 classes of measurement error (E1–E9) + 3 classes of search-size discount (E10–E12), turned into runnable checks. Input: daily returns + a small meta.json (+ optional trials.csv). Output: a PASS / WARN / FAIL report card.

pip install -r requirements.txt
python -m backtest_lint --bundle examples/bad          # 故意踩坑的示例:10 项阻断
python -m backtest_lint --bundle examples/good         # 干净示例:全部通过
python -m backtest_lint --bundle examples/multi_trial  # 24 组网格里挑最好:E1–E9 全过,E12 拦住
python -m pytest tests -q                              # 59 passed

把九类测量误差(含 2026-11-12 新增的 E9)和三类搜索规模折价(2026-11-16 新增的 E10–E12) 做成一个能跑的 CLI:输入一份回测的日收益与元数据,输出 PASS / WARN / FAIL 报告卡。

它不判断策略未来能不能赚钱,只检查这份回测有没有可能骗人。


为什么做这个

一个 A 股短期反转策略,最初报告 +10.42%/年、IR 0.31、六年全正。 逐项修正后,同一份数据、同一套成本,变成 −1.76%/年、IR −0.04; 换成零成本买入持有基准,是 −5.61%/年、六年五年跑输。

九类误差中有八类全部朝同一个方向——让策略看起来比实际更好:

# 误差 实测幅度(该项目)
E1 基准记账:exp(mean(log r)) 不是等权组合收益 基准被压低 10.87pp/年
E2 基准每日再平衡(100% 日换手)却按零成本计 与策略成本不对称
E3 静态池:用末端流动性挑历史成分 同窗口基准 +22.71% vs −7.07%,差 29.8pp
E4 单批建仓的调仓相位 年度超额可从 −20.6% 到 +17.4% 翻符号
E5 面板/回看窗口漂移(EMA 无限记忆) 同一日期算出不同分数
E6 算术年化与几何年化混用、报告区间与日收益文件不一致 σ=3% 时约 12pp/年
E7 参数网格不校正多重检验 18 组网格 Bonferroni 门槛 |t|>2.99
E8 退市股缺失(幸存者偏差) 方向通常不利于策略,无法量化
E9 成交时点可执行性:信号用的是收盘价,成交价也按收盘价算 实测:同一信号换成交口径,毛年化 −42.8% ↔ +50.5%(差 93pp)
E10 搜索规模折价(DSR):试了 N 次,把 N 折进显著性门槛 T=154 期、年化 SR 0.34:N=1 时 DSR 0.887,n_trials=8 就掉到 0.41
E11 回测过拟合概率(PBO):样本内最优在样本外排中的频率 同一批候选配置里,最优那组的优势可能整个是挑出来的
E12 样本外衰减(CPCV):净化切分后,样本内最优还赚不赚钱 示例包:样本内最优 46.7% 的切分在样本外不赚钱

E9 是 2026-11-12 新增的第九类,E10–E12 是 2026-11-16 新增的第十至十二类。 E1–E8 检查一次回测内部的测量误差;E10–E12 检查多次尝试之间的选择偏差—— 报表上看不出差别:只跑一次的「好结果」和挑了 42 组之后的「好结果」长得一模一样。

这个工具把每一条变成可执行的检查。


快速开始

# 方式一:直接从仓库装(不需要 PyPI)
pip install "git+https://github.com/tomori29/backtest-lint.git"

# 方式二:克隆后跑
git clone https://github.com/tomori29/backtest-lint && cd backtest-lint
pip install -r requirements.txt

# 1) 跑一个故意踩坑的示例(10 项阻断)
python -m backtest_lint --bundle examples/bad

# 2) 跑一个干净示例(全部通过)
python -m backtest_lint --bundle examples/good

# 3) 跑一个「网格里挑最好」的示例:E1–E9 全过,E12 拦住
python -m backtest_lint --bundle examples/multi_trial

# 4) 生成一份空白模板
python -m backtest_lint --init my_bundle

尚未发布到 PyPI;pip install . 之后也可以直接用 backtest-lint 命令。 在 Windows 的 cmd / PowerShell 里跑也没问题,报告里的 ✅/⚠️/❌ 会按控制台编码自动降级。

输出 Markdown 报告卡;--md / --json 可另存。有 FAIL 时退出码为 1,可直接接 CI。

python -m backtest_lint --bundle my_bundle --md report.md --json report.json

先看它怎么把一个假超额拆掉(demo/)

一个合成市场,真实 alpha 设为 0,然后逐步加上/去掉口径,看同一份数据能被推到多远:

python demo/run_demo.py
步骤                                                  年化超额
① 原样:幸存者池 / 18 组挑最好 / 策略不扣成本 / 基准零成本日再平衡      4.83%
② 修多重检验:18 组取中位数,不挑最好的一组                       3.56%
③ 修幸存者偏差:策略池含已退市股票                             1.01%
④ 修成本:策略每期换仓扣双边成本                             -3.40%
⑤ 修基准:日再平衡 → 买入持有(基准不再零成本全额换手)              -3.98%
⑥ 修年化口径:两边统一几何年化                                -3.95%

8.78pp 的落差全是口径造成的,因为真实 alpha 是 0。固定随机种子,数字可复现—— 不依赖任何真实数据,也能自己验一遍。详见 demo/README.md。


输入格式

一个目录,两个文件:

daily.csv

date,strategy,benchmark
2024-01-02,0.0012,0.0008
2024-01-03,-0.0005,0.0001
  • date 可省略;strategy 必填;benchmark 缺失会直接判 FAIL。
  • 列名支持中文别名:日期 / 策略 / 基准。
  • 收益是日简单收益(0.0012 = 0.12%),不是净值。

meta.json

{
  "name": "我的策略",
  "universe_mode": "pit",
  "delisted_included": false,
  "benchmark_type": "buy_hold",
  "cost": {"commission": 0.00025, "stamp": 0.0005, "slip": 0.001,
           "applied_to_benchmark": false},
  "phases_run": 21,
  "panel_fixed": true,
  "panel_trail_history": [2700],
  "n_trials": 1,
  "sample_days": 1425,
  "reported_t": 3.2,
  "reported_ann": 0.12,
  "reported_bench_ann": 0.05
}
字段 取值 说明
universe_mode pit / static / unknown 股票池是否逐日时点
delisted_included true / false 池子是否含退市股
benchmark_type buy_hold / index / ew_daily / strategy_like 基准类型
cost.applied_to_benchmark true / false 基准是否同样扣成本
phases_run 整数 跑了几个调仓相位
panel_fixed true / false 面板起点/回看窗口是否固定
panel_trail_history 数组 同一次比较里用过的窗口;多个值直接 FAIL
n_trials 整数 参数网格/变体尝试次数
sample_days 整数 报告里的样本天数;与日收益文件不一致会 FAIL
reported_t 浮点 主检验 t 值
reported_ann 浮点 报告的策略年化
reported_bench_ann 浮点 报告的基准年化
signal_uses close_t / high_t / vwap_t … 信号用了哪个时点的价格(E9)
trade_price open_t / close_t / open_t+1 … 成交价用的哪个时点(E9)
trials_csv 文件名 候选矩阵路径,默认 trials.csv(E11/E12)

trials.csv(可选,启用 E11/E12)

每列一个跑过的候选配置,每行一期,与 daily.csv 同频。没有这个文件, E11/E12 会显示 INFO 跳过——单条回测本来就无法计算过拟合概率,这不算失败。

cfg00,cfg01,cfg02
0.0012,-0.0043,0.0008
-0.0031,0.0022,-0.0011

只填「报告里那一条」没有意义:E11/E12 要看的正是你没报告的那些。 把参数网格、指标窗口、行业中性开关……所有跑过的变体都留成列。


检查项与判据

# 检查 PASS WARN FAIL
B0 基准存在 有 benchmark 列 — 缺基准
E1 基准记账 报告值与几何一致或差 <1pp 几何/算术差 >1pp 报告值更接近算术且差 >0.5pp
E2 成本对称 基准也扣成本,或基准是买入持有/指数 基准类型未声明 ew_daily/strategy_like 且基准零成本
E3 股票池时点性 pit — static 或未声明
E4 调仓相位 ≥5 个相位 2–4 个 1 个或未声明
E5 面板固定 panel_fixed=true 且窗口唯一 未声明 false 或窗口有多个值
E6 年化口径 报告年化与几何一致 未给报告值 差 >0.5pp,或样本天数不一致
E7 多重检验 |t| > Bonferroni 门槛 未给 t |t| < 门槛
E8 幸存者偏差 含退市股 未声明 只有当前在市标的
E9 成交时点可执行性 成交价在信号确认之后(如 close_t → open_t+1) 未声明 signal_uses/trade_price 信号用收盘后价格、成交也用同一交易日价格
E10 搜索规模折价(DSR) DSR ≥ 0.95 0.90–0.95 DSR < 0.90
E11 过拟合概率(PBO) PBO ≤ 0.20 0.20–0.50 > 0.50
E12 样本外衰减(CPCV) 样本外不赚钱的切分 ≤ 10% 10–30% > 30%
S1 收益健全性 无异常 缺失 >5% 或单日 >50% 有效样本 <20 或方差为 0
S2 对齐 ≥95% 日期同时有效 — <95%
S3 基准健全性 无异常 单日 >15% 单日 >30% 或有效样本 <20

Bonferroni 门槛:z = Φ⁻¹(1 − 0.025/n_trials);n=18 时约 2.99,n=1 时 1.96。

E11/E12 需要 trials.csv;没有就返回 INFO(跳过),既不算通过也不算失败。 E10 只需要 daily.csv 和 n_trials——所以它默认就在跑,n_trials 填对了才有意义。


真实案例:V2.0 反转策略的体检报告

examples/v2_reversal/ 是项目里真实跑过的结果(2020-12 ~ 2026-10,时点池,错峰 4)。 体检结论:5 项阻断、4 项警告:

检查 结果 说明
E2 ❌ 基准是每日再平衡等权(100% 日换手)却零成本
E6 ❌ 报告样本 1545 天,日收益文件 1425 天,不是同一个样本
E7 ❌ 18 组网格,门槛 |t|>2.99,实测 t=0.80
E8 ❌ 只有当前在市标的,退市股缺失
E10 ❌ 18 组尝试下 SR0=0.0491/期,实测 SR=−0.0101/期,DSR=0.013
E1 ⚠️ 基准几何 −5.95% vs 算术 −2.55%,差 3.40pp,报告未声明口径
E4 ⚠️ 只跑了 4 个相位
E9 ⚠️ meta 未声明 signal_uses / trade_price,成交时点无法判断
S3 ⚠️ 基准有 1 天单日 +18.70%,先确认是真实波动还是对齐问题

完整报告见 examples/v2_reversal_report.md。 E6 那条尤其值得看:报告正文的 −6.24% 和日收益文件里的 −7.87%,不是同一个样本—— 这种"数字对不上"在没有工具时很难被发现。


目录

backtest-lint/
├─ .github/workflows/  CI:Python 3.10–3.13 跑 59 个单测
├─ backtest_lint/
│  ├─ checks.py      九类误差(E1–E9)+ 搜索规模(E10–E12)+ 四项健全性检查
│  ├─ overfit.py     DSR / MTRL / PBO / CPCV(只依赖 numpy,scipy 可选)
│  ├─ metrics.py     几何/算术年化、NW、Bonferroni
│  ├─ loader.py      daily.csv + meta.json (+ trials.csv) 读取与模板
│  ├─ report.py      Markdown / JSON 报告
│  └─ cli.py         命令行入口
├─ demo/             可复现的「假超额拆解」演示(合成数据,真实 alpha=0)
├─ examples/
│  ├─ bad/           故意踩坑(10 项阻断)
│  ├─ good/          干净示例(全部通过)
│  ├─ multi_trial/   24 组网格挑最好(E12 拦住)
│  └─ v2_reversal/   项目真实结果(5 阻断 4 警告)
├─ tests/            59 个单测
├─ requirements.txt
└─ README.md

测试

python -m pytest tests -q
# 59 passed

E10–E12 的测试对着手算答案:T=1200/N=100 时 SR0=0.0731;N=1 时 DSR 退化为单尾检验 (SR=0.10、T=121 → 0.8643);T=154、SR 0.34 在 N=8 下掉到 0.41。CPCV 的切分数、 净化条数、embargo 区间也都是可数的性质,不是「能跑就算过」。


局限(写论文/交付时必须说)

  1. 只检查可观测的口径:特征前视、执行前视、代码 bug(如复权因子对齐错误) 需要读代码或对拍数据,工具查不出来。
  2. 不判断未来收益:通过体检 ≠ 能赚钱;它只排除"因为记账错误而看起来能赚"。
  3. meta 靠自报:universe_mode、phases_run 等字段填错,检查就失效。 工具能做的是把"必须声明"变成硬门槛。
  4. E1/E6 依赖报告值:没有 reported_ann / reported_bench_ann 时只能给 WARN。
  5. 幸存者偏差只能披露,不能修复:本地没有退市股就是没有。
  6. E10–E12 只能用你填的候选集:trials.csv 里少列了几组,PBO 就会偏低—— 它检查的是「你承认试过几次」,不是「你实际试过几次」。
  7. DSR 的 SR0 是正态近似:非正态、自相关、策略间高相关时,真实门槛更高, 这里的 DSR 偏乐观。E11/E12 同样假设候选配置之间是可比的。

它适合谁

  • 写完回测、准备对外报数字的人:先跑一遍,把阻断项消掉再说话。
  • 看别人回测报告的人:把对方的日收益和参数填进来,看哪些项过不了。
  • 教学/复试:十二类误差每一条都能落到一个可运行的检查上,比背概念有用。

这个工具的全部价值,来自一个项目把自己的 +10.42% 亲手推翻到 −1.76% 的过程。 它不是"策略生成器",是"策略证伪器"。

Metadata

Release files for backtest-lint 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for backtest-lint 0.3.0
File Size Uploaded Provenance Actions
backtest_lint-0.3.0.tar.gz 35.7 kB With provenance Details for backtest_lint-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for backtest-lint 0.3.0
File Interpreter ABI Platform Actions
backtest_lint-0.3.0-py3-none-any.whl Python 3 none any Details for backtest_lint-0.3.0-py3-none-any.whl

Total release size: 63.5 kB

Release files / backtest_lint-0.3.0.tar.gz

Download URL backtest_lint-0.3.0.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1383646de57bb325ac69bb4cc851b5a90ca5e9ee222a47afc50888d24e11254d
BLAKE2b-256 checksum
How to use checksums
b3701057962532ef9697b5d7f480b321e8935962638e9d66c1ba026684b30ea9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / backtest_lint-0.3.0-py3-none-any.whl

Download URL backtest_lint-0.3.0-py3-none-any.whl
Size 27.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
daaa98a970538b1cd7355bcf623542fa030ceb5dc9b5b141405b93be10a26048
BLAKE2b-256 checksum
How to use checksums
98915d743f1ffef4c23b3800a8e206dd5213bea12fcee2f724879e7a5236434d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for backtest-lint 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for backtest_lint-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

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