wbt 0.3.0

Weight-based backtesting engine for quantitative trading
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aeb4f0a4",
   "metadata": {},
   "source": [
    "# Weight Backtest 快速入门\n",
    "---\n",
    "\n",
    "核心功能:\n",
    "\n",
    "1. 使用 `backtest` 函数执行回测,并查看结果\n",
    "2. 使用 `plotting` 模块查看回测结果相关图表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11904623",
   "metadata": {},
   "outputs": [],
   "source": [
    "import wbt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeda544a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成示例权重数据(自洽可跑);也可替换为自己的 DataFrame\n",
    "df = wbt.mock_weights(freq=\"30分钟\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9f922872",
   "metadata": {},
   "outputs": [],
   "source": [
    "wb = wbt.backtest(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cec892b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'绝对收益': 1.034,\n",
       " '年化收益': 0.1385,\n",
       " '夏普比率': 1.3964,\n",
       " '卡玛比率': 0.8701,\n",
       " '新高占比': 0.084,\n",
       " '单笔盈亏比': 2.1722,\n",
       " '单笔收益': 25.59,\n",
       " '日胜率': 0.5228,\n",
       " '周胜率': 0.5727,\n",
       " '月胜率': 0.6456,\n",
       " '季胜率': 0.7407,\n",
       " '年胜率': 1.0,\n",
       " '最大回撤': 0.1592,\n",
       " '年化波动率': 0.0992,\n",
       " '下行波动率': 0.0629,\n",
       " '新高间隔': 209.0,\n",
       " '交易次数': 276334,\n",
       " '年化交易次数': 37001.15,\n",
       " '持仓K线数': 972.95,\n",
       " '交易胜率': 0.3716,\n",
       " '多头占比': 0.5028,\n",
       " '空头占比': 0.4611,\n",
       " '品种数量': 9,\n",
       " '开始日期': '2017-01-03',\n",
       " '结束日期': '2023-07-31'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wb.stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "030cb912",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'绝对收益': -0.4861,\n",
       " '年化收益': -0.3347,\n",
       " '夏普比率': -3.2002,\n",
       " '卡玛比率': -0.6924,\n",
       " '新高占比': 0.0027,\n",
       " '单笔盈亏比': 1.0135,\n",
       " '单笔收益': 0.21,\n",
       " '日胜率': 0.4071,\n",
       " '周胜率': 0.3208,\n",
       " '月胜率': 0.0,\n",
       " '季胜率': 0.0,\n",
       " '年胜率': 0.0,\n",
       " '最大回撤': 0.4834,\n",
       " '年化波动率': 0.1046,\n",
       " '下行波动率': 0.0671,\n",
       " '新高间隔': 0.0,\n",
       " '交易次数': 642859,\n",
       " '年化交易次数': 442624.23,\n",
       " '持仓K线数': 2.4,\n",
       " '交易胜率': 0.4983}"
      ]
     },
     "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": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'绝对收益': -12.3992,\n",
       " '年化收益': -0.5702,\n",
       " '夏普比率': -3.1444,\n",
       " '卡玛比率': -0.0459,\n",
       " '新高占比': 0.0004,\n",
       " '日胜率': 0.4188,\n",
       " '周胜率': 0.2997,\n",
       " '月胜率': 0.1602,\n",
       " '季胜率': 0.0328,\n",
       " '年胜率': 0.0,\n",
       " '最大回撤': 12.4099,\n",
       " '年化波动率': 0.1813,\n",
       " '下行波动率': 0.1169,\n",
       " '新高间隔': 1.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": {},
   "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_stats_comparison,\n    plot_symbol_returns,\n    plot_verdict,\n)\n\n# 标准输入:所有绘图共用同一个 result\nresult = wb.to_result()"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "uj0wv49dyhq",
   "metadata": {},
   "outputs": [],
   "source": "# 累计收益曲线(原始)\nplot_cumulative_returns(result, keys=[\"多空\", \"多头\", \"空头\", \"基准\"])"
  },
  {
   "cell_type": "code",
   "id": "26ec3476",
   "source": "# 累计收益曲线(波动率归一)\nplot_cumulative_returns(result, keys=[\"多空\", \"多头\", \"空头\", \"基准\", \"超额\"], voladj=True)",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "qg1zln8tjjd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回撤分析\n",
    "plot_drawdown(result, key=\"多空\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9013n207wmq",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 日收益分布\n",
    "plot_daily_return_dist(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcwauyvkbeh",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 月度收益热力图\n",
    "plot_monthly_heatmap(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "awz5ejkohwk",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 品种收益分布\n",
    "plot_symbol_returns(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7s4joww374i",
   "metadata": {},
   "outputs": [],
   "source": "# 盈亏比例分布(按方向)\nplot_pairs_pnl_dist(result)"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "s4a9j694tsq",
   "metadata": {},
   "outputs": [],
   "source": "# 持仓K线数分布(按方向)\nplot_pairs_hold_dist(result)"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ey61iditsul",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绩效指标表格(红涨绿跌)\n",
    "plot_colored_table(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cmq7k0wduoh",
   "metadata": {},
   "outputs": [],
   "source": "# 多空/多头/空头/基准/超额 关键指标对比表\nplot_stats_comparison(result)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 关键交易:每年最赚/最亏各 3 笔\n",
    "plot_key_trades(result)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 回撤明细表(审核页面)\n",
    "plot_drawdowns_table(result)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 策略判定 + 年度指标(审核页面)\n",
    "plot_verdict(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfbaf336",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.13.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}