{
"cells": [
{
"cell_type": "markdown",
"id": "aeb4f0a4",
"metadata": {},
"source": [
"# Weight Backtest 快速入门\n",
"---\n",
"\n",
"核心功能:\n",
"\n",
"1. 使用 `backtest` 函数执行回测,并查看结果\n",
"2. 使用 `plotting` 模块查看回测结果相关图表\n",
"\n",
"`weight_type` 默认 `ts`,仅接受严格小写的 `ts`(品种收益取均值)或 `cs`(求和);其他字符串会抛出 `ValueError`,不会自动去除空白或回退。\n",
"\n",
"`alpha['策略']` 与 `daily_return.total` 共用组合日收益,审核年度/近期绝对收益按该序列单利求和。基准始终是当日有效品种的等权均值,原始超额=策略−基准。\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11904623",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:11.904232Z",
"iopub.status.busy": "2026-08-26T06:24:11.903915Z",
"iopub.status.idle": "2026-08-26T06:24:13.037074Z",
"shell.execute_reply": "2026-08-26T06:24:13.036593Z"
}
},
"outputs": [],
"source": [
"import wbt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eeda544a",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.038413Z",
"iopub.status.busy": "2026-08-26T06:24:13.038294Z",
"iopub.status.idle": "2026-08-26T06:24:13.118232Z",
"shell.execute_reply": "2026-08-26T06:24:13.117759Z"
}
},
"outputs": [],
"source": [
"# 生成示例权重数据(自洽可跑);也可替换为自己的 DataFrame\n",
"df = wbt.mock_weights(freq=\"30分钟\")\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f922872",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.119307Z",
"iopub.status.busy": "2026-08-26T06:24:13.119223Z",
"iopub.status.idle": "2026-08-26T06:24:13.222992Z",
"shell.execute_reply": "2026-08-26T06:24:13.222469Z"
}
},
"outputs": [],
"source": [
"wb = wbt.backtest(df)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cec892b0",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.224174Z",
"iopub.status.busy": "2026-08-26T06:24:13.224097Z",
"iopub.status.idle": "2026-08-26T06:24:13.226719Z",
"shell.execute_reply": "2026-08-26T06:24:13.226322Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'绝对收益': -7.6277,\n",
" '年化收益': -0.3508,\n",
" '夏普比率': -3.4511,\n",
" '卡玛比率': -0.046,\n",
" '新高占比': 0.0002,\n",
" '单笔盈亏比': 0.9593,\n",
" '单笔收益': 0.06,\n",
" '日胜率': 0.4119,\n",
" '周胜率': 0.2704,\n",
" '月胜率': 0.1105,\n",
" '季胜率': 0.0328,\n",
" '年胜率': 0.0,\n",
" '最大回撤': 7.6268,\n",
" '年化波动率': 0.1016,\n",
" '下行波动率': 0.0682,\n",
" '新高间隔': 5479.0,\n",
" '交易次数': 96782.57,\n",
" '年化交易次数': 4450.59,\n",
" '持仓K线数': 2.41,\n",
" '交易胜率': 0.5108,\n",
" '多头占比': 0.4194,\n",
" '空头占比': 0.4194,\n",
" '品种数量': 5,\n",
" '开始日期': '2010-01-01',\n",
" '结束日期': '2025-01-01'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wb.stats"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "030cb912",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.227723Z",
"iopub.status.busy": "2026-08-26T06:24:13.227649Z",
"iopub.status.idle": "2026-08-26T06:24:13.235152Z",
"shell.execute_reply": "2026-08-26T06:24:13.234720Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'绝对收益': -0.5046,\n",
" '年化收益': -0.3475,\n",
" '夏普比率': -3.6247,\n",
" '卡玛比率': -0.6863,\n",
" '新高占比': 0.0027,\n",
" '单笔盈亏比': 1.0064,\n",
" '单笔收益': 0.08,\n",
" '日胜率': 0.418,\n",
" '周胜率': 0.2642,\n",
" '月胜率': 0.1667,\n",
" '季胜率': 0.0,\n",
" '年胜率': 0.0,\n",
" '最大回撤': 0.5064,\n",
" '年化波动率': 0.0959,\n",
" '下行波动率': 0.0635,\n",
" '新高间隔': 365.0,\n",
" '交易次数': 6428.59,\n",
" '年化交易次数': 4426.24,\n",
" '持仓K线数': 2.4,\n",
" '交易胜率': 0.499,\n",
" '多头占比': 0.4172,\n",
" '空头占比': 0.4181,\n",
" '品种数量': 5,\n",
" '开始日期': '2020-01-01',\n",
" '结束日期': '2020-12-31'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wb.segment_stats(sdt=\"2020-01-01\", edt=\"2020-12-31\", kind=\"多空\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "130d5f03",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.236142Z",
"iopub.status.busy": "2026-08-26T06:24:13.236072Z",
"iopub.status.idle": "2026-08-26T06:24:13.238976Z",
"shell.execute_reply": "2026-08-26T06:24:13.238589Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'绝对收益': -11.4258,\n",
" '年化收益': -0.5254,\n",
" '夏普比率': -2.921,\n",
" '卡玛比率': -0.0458,\n",
" '新高占比': 0.0015,\n",
" '日胜率': 0.4239,\n",
" '周胜率': 0.3265,\n",
" '月胜率': 0.1713,\n",
" '季胜率': 0.0656,\n",
" '年胜率': 0.0,\n",
" '最大回撤': 11.4774,\n",
" '年化波动率': 0.1799,\n",
" '下行波动率': 0.1183,\n",
" '新高间隔': 5388.0}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wb.long_alpha_stats"
]
},
{
"cell_type": "markdown",
"id": "02488db9",
"metadata": {},
"source": [
"## 绘图示例\n",
"\n",
"所有绘图函数以 `BacktestResult` 为统一输入:`result = wb.to_result()`,下游零数据转换。"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "vyq9b1vd8ei",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.240000Z",
"iopub.status.busy": "2026-08-26T06:24:13.239936Z",
"iopub.status.idle": "2026-08-26T06:24:13.352222Z",
"shell.execute_reply": "2026-08-26T06:24:13.351817Z"
}
},
"outputs": [],
"source": [
"from wbt.plotting import (\n",
" plot_colored_table,\n",
" plot_cumulative_returns,\n",
" plot_daily_return_dist,\n",
" plot_drawdown,\n",
" plot_drawdowns_table,\n",
" plot_key_trades,\n",
" plot_monthly_heatmap,\n",
" plot_pairs_hold_dist,\n",
" plot_pairs_pnl_dist,\n",
" plot_rolling_metrics,\n",
" plot_segment_comparison,\n",
" plot_stats_comparison,\n",
" plot_symbol_returns,\n",
" plot_verdict,\n",
" plot_yearly_returns,\n",
")\n",
"\n",
"# 标准输入:所有绘图共用同一个 result\n",
"result = wb.to_result()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "uj0wv49dyhq",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:13.353361Z",
"iopub.status.busy": "2026-08-26T06:24:13.353282Z",
"iopub.status.idle": "2026-08-26T06:24:14.224311Z",
"shell.execute_reply": "2026-08-26T06:24:14.223812Z"
}
},
"outputs": [],
"source": [
"# 累计收益曲线(原始)\n",
"plot_cumulative_returns(result, keys=[\"多空\", \"多头\", \"空头\", \"基准\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26ec3476",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.229768Z",
"iopub.status.busy": "2026-08-26T06:24:14.229656Z",
"iopub.status.idle": "2026-08-26T06:24:14.349796Z",
"shell.execute_reply": "2026-08-26T06:24:14.349425Z"
}
},
"outputs": [],
"source": [
"# 累计收益曲线(波动率归一)\n",
"plot_cumulative_returns(result, keys=[\"多空\", \"多头\", \"空头\", \"基准\", \"多头超额\", \"空头超额\"], voladj=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "qg1zln8tjjd",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.357476Z",
"iopub.status.busy": "2026-08-26T06:24:14.357367Z",
"iopub.status.idle": "2026-08-26T06:24:14.422386Z",
"shell.execute_reply": "2026-08-26T06:24:14.422015Z"
}
},
"outputs": [],
"source": [
"# 回撤分析\n",
"plot_drawdown(result, key=\"多空\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9013n207wmq",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.425119Z",
"iopub.status.busy": "2026-08-26T06:24:14.425033Z",
"iopub.status.idle": "2026-08-26T06:24:14.443508Z",
"shell.execute_reply": "2026-08-26T06:24:14.443166Z"
}
},
"outputs": [],
"source": [
"# 日收益分布\n",
"plot_daily_return_dist(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcwauyvkbeh",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.444618Z",
"iopub.status.busy": "2026-08-26T06:24:14.444521Z",
"iopub.status.idle": "2026-08-26T06:24:14.461943Z",
"shell.execute_reply": "2026-08-26T06:24:14.461499Z"
}
},
"outputs": [],
"source": [
"# 月度收益热力图\n",
"plot_monthly_heatmap(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "awz5ejkohwk",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.463011Z",
"iopub.status.busy": "2026-08-26T06:24:14.462924Z",
"iopub.status.idle": "2026-08-26T06:24:14.474354Z",
"shell.execute_reply": "2026-08-26T06:24:14.473919Z"
}
},
"outputs": [],
"source": [
"# 品种收益分布\n",
"plot_symbol_returns(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68ea8e43",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.475461Z",
"iopub.status.busy": "2026-08-26T06:24:14.475373Z",
"iopub.status.idle": "2026-08-26T06:24:14.488793Z",
"shell.execute_reply": "2026-08-26T06:24:14.488413Z"
}
},
"outputs": [],
"source": [
"# 稳健性:年度收益(绝对 vs 超额)\n",
"plot_yearly_returns(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a18f1c98",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.489944Z",
"iopub.status.busy": "2026-08-26T06:24:14.489830Z",
"iopub.status.idle": "2026-08-26T06:24:14.588933Z",
"shell.execute_reply": "2026-08-26T06:24:14.588570Z"
}
},
"outputs": [],
"source": [
"# 稳健性:滚动指标(252日窗口,年化收益/波动率/夏普)\n",
"plot_rolling_metrics(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6162329",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.592684Z",
"iopub.status.busy": "2026-08-26T06:24:14.592583Z",
"iopub.status.idle": "2026-08-26T06:24:14.609378Z",
"shell.execute_reply": "2026-08-26T06:24:14.608878Z"
}
},
"outputs": [],
"source": [
"# 稳健性:分段对比(近1年 vs 全样本)\n",
"plot_segment_comparison(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7s4joww374i",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.610380Z",
"iopub.status.busy": "2026-08-26T06:24:14.610308Z",
"iopub.status.idle": "2026-08-26T06:24:14.650631Z",
"shell.execute_reply": "2026-08-26T06:24:14.650185Z"
}
},
"outputs": [],
"source": [
"# 盈亏比例分布(按方向)\n",
"plot_pairs_pnl_dist(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "s4a9j694tsq",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.656505Z",
"iopub.status.busy": "2026-08-26T06:24:14.656394Z",
"iopub.status.idle": "2026-08-26T06:24:14.671513Z",
"shell.execute_reply": "2026-08-26T06:24:14.671068Z"
}
},
"outputs": [],
"source": [
"# 持仓K线数分布(按方向)\n",
"plot_pairs_hold_dist(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ey61iditsul",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.672663Z",
"iopub.status.busy": "2026-08-26T06:24:14.672585Z",
"iopub.status.idle": "2026-08-26T06:24:14.684819Z",
"shell.execute_reply": "2026-08-26T06:24:14.684413Z"
}
},
"outputs": [],
"source": [
"# 绩效指标表格(红涨绿跌)\n",
"plot_colored_table(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cmq7k0wduoh",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.685820Z",
"iopub.status.busy": "2026-08-26T06:24:14.685741Z",
"iopub.status.idle": "2026-08-26T06:24:14.696810Z",
"shell.execute_reply": "2026-08-26T06:24:14.696398Z"
}
},
"outputs": [],
"source": [
"# 多空/多头/空头/基准/超额 关键指标对比表\n",
"plot_stats_comparison(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1b2c3d4",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.697780Z",
"iopub.status.busy": "2026-08-26T06:24:14.697712Z",
"iopub.status.idle": "2026-08-26T06:24:14.796198Z",
"shell.execute_reply": "2026-08-26T06:24:14.795849Z"
}
},
"outputs": [],
"source": [
"# 关键交易:每年最赚/最亏各 3 笔\n",
"plot_key_trades(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5f6a7b8",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.797372Z",
"iopub.status.busy": "2026-08-26T06:24:14.797282Z",
"iopub.status.idle": "2026-08-26T06:24:14.811155Z",
"shell.execute_reply": "2026-08-26T06:24:14.810776Z"
}
},
"outputs": [],
"source": [
"# 回撤明细表(审核页面)\n",
"plot_drawdowns_table(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell00extra",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.812347Z",
"iopub.status.busy": "2026-08-26T06:24:14.812256Z",
"iopub.status.idle": "2026-08-26T06:24:14.825051Z",
"shell.execute_reply": "2026-08-26T06:24:14.824675Z"
}
},
"outputs": [],
"source": [
"# 策略判定:history(逐年)+ recent(默认尾部 252 个交易日;卡内显示实际窗口、收益与回撤)\n",
"plot_verdict(result)"
]
},
{
"cell_type": "markdown",
"id": "cell01extra",
"metadata": {},
"source": [
"## 结果序列化(JSON / MessagePack)\n",
"\n",
"`to_dict()` 是 JSON 安全结构(`NaN` / `Infinity` 一律转 `null`)。`full=True` 时 payload 同时包含 `verdict`(history,逐年)和 `verdict_recent`(recent,默认尾部 252 个交易日);recent 的实际窗口、收益和回撤字段为 `recent_start_date`、`recent_end_date`、`recent_actual_days`、`recent_abs_return`、`recent_alpha_return`、`recent_alpha_max_drawdown`。JSON 与 MessagePack 都使用相同的带版本 envelope(`format` / `format_version` / `payload`),读回后均返回 payload dict。JSON 无额外依赖;MessagePack 需 `pip install wbt[msgpack]`。\n",
"\n",
"定位:完整嵌套结果对象的交换格式,**不替代** Arrow IPC / Parquet 处理收益曲线等列式表格热数据。"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell02extra",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-26T06:24:14.826237Z",
"iopub.status.busy": "2026-08-26T06:24:14.826143Z",
"iopub.status.idle": "2026-08-26T06:24:16.647217Z",
"shell.execute_reply": "2026-08-26T06:24:16.646795Z"
}
},
"outputs": [],
"source": [
"# 两种格式写出后均读回 dict payload;严格比较值和类型\n",
"result.dump_json(\"backtest_results.json\", full=True)\n",
"json_payload = wbt.load_json(\"backtest_results.json\")\n",
"\n",
"result.dump_msgpack(\"backtest_results.msgpack\", full=True)\n",
"msgpack_payload = wbt.load_msgpack(\"backtest_results.msgpack\")\n",
"wbt.assert_payload_equal(json_payload, msgpack_payload)\n",
"print(json_payload[\"symbol_count\"], len(json_payload[\"dates\"]), list(json_payload[\"curves\"].keys()))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}