wbt 0.9.0

Weight-based backtesting engine for quantitative trading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
{
 "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
}