Skip to content
Back to search

akquant

AKQuant is a high-performance quantitative research and trading framework built on Rust and Python! 开源量化回测框架

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today

Check it deeper on your machine

It has changed since its scan on 11 Nov 2026.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/akfamily/akquant with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/akfamily/akquant'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/akfamily--akquant/badge.svg)](/listings/akfamily--akquant)

HTML

<a href="/listings/akfamily--akquant"><img src="/api/listings/akfamily--akquant/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 302 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

This file has changed since it was scanned, so this is not exactly the text that was scanned, and no line is marked. The findings are listed with the safety scan above.

  1. <p align="center">
  2. <img src="https://raw.githubusercontent.com/akfamily/akquant/main/assets/logo.svg" alt="AKQuant" width="400">
  3. </p>
  4. <p align="center">
  5. <a href="https://pypi.org/project/akquant/">
  6. <img src="https://img.shields.io/pypi/v/akquant?style=flat-square&color=007ec6" alt="PyPI Version">
  7. </a>
  8. <a href="https://pypi.org/project/akquant/">
  9. <img src="https://img.shields.io/pypi/pyversions/akquant?style=flat-square" alt="Python Versions">
  10. </a>
  11. <a href="LICENSE">
  12. <img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="License">
  13. </a>
  14. <a href="https://github.com/akfamily/akshare">
  15. <img src="https://img.shields.io/badge/Data%20Science-AKShare-green?style=flat-square" alt="AKShare">
  16. </a>
  17. <a href="https://pepy.tech/projects/akquant">
  18. <img src="https://static.pepy.tech/personalized-badge/akquant?period=total&units=INTERNATIONAL_SYSTEM&left_color=BLACK&right_color=GREEN&left_text=downloads" alt="Downloads">
  19. </a>
  20. </p>
  21. <!-- mcp-name: io.github.akfamily/akquant -->
  22. **AKQuant** 是一个回测与实盘一体的量化交易框架:核心引擎用 **Rust** 编写,策略用 **Python** 编写,覆盖股票、期货、期权等品种。
  23. ## 核心特性
  24. * **Rust 引擎,Python 策略**:事件驱动的撮合、结算与风控在 Rust 中完成,NumPy 数组可零拷贝导入引擎;策略侧只需继承 `Strategy` 并实现 `on_bar` 等回调。实际速度取决于策略逻辑、数据规模与回调频率。
  25. * **回测与实盘同一套策略**:`run_backtest` 回测,`run_live` 做仿真或实盘;内置 CTP 期货接口,其他柜台可以注册自定义券商接口接入。
  26. * **贴近 A 股规则**:T+1、涨跌停、佣金/印花税/过户费、期货保证金、期权希腊值,以及中国时区内置的交易所日历。
  27. * **指标与因子**:`akquant.talib` 提供 104 个 TA-Lib 兼容指标(`python`/`rust` 双后端);`self.I()` 声明式指标支持多周期;Polars 驱动的因子表达式引擎支持 `Rank(Ts_Mean(Close, 5))` 这类 Alpha101 风格公式。
  28. * **订单与风控**:市价、限价、止损单,OCO 与 Bracket 组合订单,下单前的风控检查。
  29. * **参数优化与机器学习**:多进程网格搜索;内置 Walk-forward(滚动训练)框架,可接入 PyTorch、scikit-learn 模型。
  30. * **分析与可视化**:一行代码生成交互式 HTML 报告,另有 K 线复盘、收益归因、暴露与容量分析。
  31. * **AI 助手(MCP)**:自带 MCP 服务,可在 AI 助手中用自然语言写策略、跑回测,已上架 [MCP 官方注册表](https://registry.modelcontextprotocol.io/v0.1/servers?search=io.github.akfamily/akquant)(`io.github.akfamily/akquant`)。
  32. **[阅读完整文档](https://akquant.akfamily.xyz/)** | **[English Documentation](https://akquant.akfamily.xyz/en/)**
  33. ## 安装
  34. **AKQuant** 已发布至 PyPI,支持 Python 3.10 及以上版本,无需安装 Rust 环境。
  35. ```bash
  36. pip install akquant
  37. ```
  38. ## 快速开始
  39. 下面的示例用 AKShare 获取浦发银行日线,阳线买入、阴线卖出:
  40. ```python
  41. import akquant as aq
  42. import akshare as ak
  43. from akquant import Strategy
  44. # 1. 准备数据
  45. # 使用 akshare 获取 A 股历史数据 (需安装: pip install akshare)
  46. df = ak.stock_zh_a_hist_sina(symbol="sh600000", start_date="20250212", end_date="20260212")
  47. class MyStrategy(Strategy):
  48. def on_bar(self, bar):
  49. # 简单策略示例:
  50. # 当收盘价 > 开盘价 (阳线) -> 买入
  51. # 当收盘价 < 开盘价 (阴线) -> 卖出
  52. # 获取当前持仓
  53. current_pos = self.get_position(bar.symbol)
  54. if current_pos == 0 and bar.close > bar.open:
  55. self.buy(symbol=bar.symbol, quantity=100)
  56. print(f"[{bar.timestamp_iso}] Buy 100 at {bar.close:.2f}") # UTC ISO 8601
  57. elif current_pos > 0 and bar.close < bar.open:
  58. self.close_position(symbol=bar.symbol)
  59. print(f"[{bar.timestamp_iso}] Sell 100 at {bar.close:.2f}") # UTC ISO 8601
  60. # 运行回测
  61. result = aq.run_backtest(
  62. data=df,
  63. strategy=MyStrategy,
  64. initial_cash=100000.0,
  65. symbols="sh600000",
  66. broker_profile="cn_stock_t1_low_fee", # A 股佣金/印花税/过户费、T+1、一手 100 股
  67. )
  68. # 打印回测结果
  69. print("\n=== Backtest Result ===")
  70. print(result)
  71. # 生成最小基准对比报告
  72. benchmark_returns = (
  73. df.set_index("date")["close"].pct_change().fillna(0.0).rename("SIMPLE_BENCH")
  74. )
  75. result.viz.report(
  76. filename="quickstart_report.html",
  77. show=False,
  78. benchmark=benchmark_returns,
  79. )
  80. ```
  81. 调用 `result.viz.report(..., benchmark=...)` 后,报告会新增“基准对比 (Benchmark Comparison)”区块,展示策略/基准/超额累计收益曲线,以及累计超额收益、年化超额收益、跟踪误差、信息比率、Beta、Alpha 等相对指标。
  82. > **注意:`run_backtest` 默认不计交易成本、不开 T+1。** 不传 `broker_profile` 也不传费率时,佣金、印花税、过户费都是 0,可以当日买卖,最小交易单位是 1 股——这样得到的是理想化的结果,换手越高越偏乐观。A 股回测请像上面这样传 `broker_profile`(内置 `cn_stock_standard`、`cn_stock_t1_low_fee`、`cn_stock_sim_high_slippage`),或者显式传 `commission_rate`、`stamp_tax_rate`、`transfer_fee_rate`、`min_commission`、`t_plus_one=True`、`lot_size=100`。模板的印花税按 2023-08-28 起的现行税率 0.05% 计,回测此前的行情请显式传 `stamp_tax_rate=0.001`。
  83. **运行结果示例(节选):**
  84. ```text
  85. === Backtest Result ===
  86. BacktestResult:
  87. Value
  88. start_time 2025-02-12 15:00:00+08:00
  89. end_time 2026-02-12 15:00:00+08:00
  90. total_bars 249
  91. closed_trade_count 62.0
  92. initial_market_value 100000.0
  93. end_market_value 99379.212386
  94. total_return_pct -0.620788
  95. annualized_return -0.006208
  96. total_commission 409.903014
  97. max_drawdown_pct 0.623393
  98. win_rate 22.580645
  99. sharpe_ratio -2.574886
  100. profit_factor 0.720871
  101. ...
  102. ```
  103. ## 在 AI 助手中使用(MCP)
  104. 先[安装 uv](https://docs.astral.sh/uv/getting-started/installation/),然后在 WorkBuddy、DeepSeek Harness、Codex、Cursor、Claude 等 AI 助手中说一句话即可:
  105. > 安装 AKShare 和 AKQuant 的官方 MCP,项目目录用 /path/to/akquant-demo,运行一个示例策略并报告结果
  106. 助手会自己配置好两个服务,再下载数据、编写策略、运行回测并解读结果,**不需要事先安装 AKQuant**。接入后可以直接说“用平安银行近三年的日线跑一个均线交叉策略,报告收益和最大回撤”。
  107. 需要手动配置时,`--root` 是项目目录(请先建好),策略和数据文件都放在里面:
  108. ```bash
  109. claude mcp add akquant -- uvx --from "akquant[mcp]" akquant-mcp --root /path/to/akquant-demo
  110. codex mcp add akquant -- uvx --from "akquant[mcp]" akquant-mcp --root /path/to/akquant-demo
  111. ```
  112. 使用 JSON 配置的客户端:
  113. ```json
  114. {
  115. "mcpServers": {
  116. "akquant": {
  117. "command": "uvx",
  118. "args": ["--from", "akquant[mcp]", "akquant-mcp", "--root", "/path/to/akquant-demo"]
  119. }
  120. }
  121. }
  122. ```
  123. 其余客户端的配置、工具清单与注意事项见 [MCP 服务文档](https://akquant.akfamily.xyz/guide/mcp/)。和 [AKTools](https://github.com/akfamily/aktools)(AKShare 的官方 MCP)一起接入,助手可以自己查数据。
  124. ## 组合订单(OCO / Bracket)
  125. * `place_oco(first_order_id, second_order_id, group_id=None)`:将两个订单绑定为 OCO,任一成交后自动撤销另一单。
  126. * `place_bracket(symbol, quantity, entry_price=None, stop_trigger_price=None, take_profit_price=None, ...)`:一次性提交 Bracket 结构;进场成交后自动挂出止损/止盈,并在双退出单场景下自动绑定 OCO。
  127. ```python
  128. from akquant import OrderStatus, Strategy
  129. class BracketHelperStrategy(Strategy):
  130. def __init__(self):
  131. self.entry_order_id = ""
  132. def on_bar(self, bar):
  133. if self.get_position(bar.symbol) > 0 or self.entry_order_id:
  134. return
  135. self.entry_order_id = self.place_bracket(
  136. symbol=bar.symbol,
  137. quantity=100,
  138. stop_trigger_price=bar.close * 0.98,
  139. take_profit_price=bar.close * 1.04,
  140. entry_tag="entry",
  141. stop_tag="stop",
  142. take_profit_tag="take",
  143. )
  144. def on_order(self, order):
  145. if order.id == self.entry_order_id and order.status in (
  146. OrderStatus.Cancelled,
  147. OrderStatus.Rejected,
  148. ):
  149. self.entry_order_id = ""
  150. ```
  151. 可直接运行完整示例:
  152. ```bash
  153. python examples/06_complex_orders.py
  154. ```
  155. ## 流式回测
  156. 如果你希望在回测执行过程中实时消费事件,可直接使用 `run_backtest` 并传入 `on_event`:
  157. ```python
  158. def on_event(event):
  159. if event["event_type"] == "finished":
  160. payload = event["payload"]
  161. print("status:", payload.get("status"))
  162. print("callback_error_count:", payload.get("callback_error_count"))
  163. result = aq.run_backtest(
  164. data=df,
  165. strategy=MyStrategy,
  166. symbols="sh600000",
  167. on_event=on_event,
  168. show_progress=False,
  169. stream_progress_interval=10,
  170. stream_equity_interval=10,
  171. stream_batch_size=32,
  172. stream_max_buffer=256,
  173. stream_error_mode="continue",
  174. )
  175. ```
  176. `on_event` 为可选参数:不传时保持传统阻塞语义,传入时可实时消费事件。
  177. 关键参数:
  178. * `stream_progress_interval` / `stream_equity_interval`: 进度与权益事件采样间隔
  179. * `stream_batch_size` / `stream_max_buffer`: 缓冲与批量刷新控制
  180. * `stream_error_mode`: 回调异常策略,支持 `"continue"` 与 `"fail_fast"`
  181. ## 可视化与分析
  182. `result.viz.report()` 生成基于 Plotly 的交互式 HTML 报告,包含权益曲线、回撤分析、月度热力图等:
  183. ```python
  184. # 生成交互式 HTML 报告,自动在浏览器中打开
  185. result.viz.report(
  186. show=True,
  187. compact_currency=True, # 金额列按 K/M/B 紧凑显示(默认 True)
  188. )
  189. # 如果你希望金额列保留原始数值精度(不缩写),可关闭:
  190. result.viz.report(
  191. show=False,
  192. filename="report_raw_amount.html",
  193. compact_currency=False,
  194. )
  195. ```
  196. 你也可以直接复用结构化分析结果做二次研究:
  197. ```python
  198. exposure = result.exposure_df() # 暴露分解(净暴露/总暴露/杠杆)
  199. attr_by_symbol = result.attribution_df(by="symbol")
  200. attr_by_tag = result.attribution_df(by="tag")
  201. capacity = result.capacity_df() # 容量代理(成交率/换手等)
  202. orders_by_strategy = result.orders_by_strategy() # 按策略归属聚合订单
  203. executions_by_strategy = result.executions_by_strategy() # 按策略归属聚合成交
  204. ```
  205. <p align="center">
  206. <img src="https://raw.githubusercontent.com/akfamily/akquant/main/assets/dashboard_preview.png" alt="Strategy Dashboard" width="800">
  207. </p>
  208. ## 文档索引
  209. * **[安装指南](https://akquant.akfamily.xyz/start/installation/)**:详细的安装步骤(含源码编译)。
  210. * **[快速入门](https://akquant.akfamily.xyz/start/quickstart/)**:更多示例与基础用法。
  211. * **[策略编写](https://akquant.akfamily.xyz/guide/strategy/)**:回调、下单接口与声明式指标。
  212. * **[实盘上手](https://akquant.akfamily.xyz/advanced/live_functional_quickstart/)**:`run_live` 的仿真与实盘模式,另见[券商能力矩阵](https://akquant.akfamily.xyz/advanced/broker_capability_matrix/)。
  213. * **[因子表达式](https://akquant.akfamily.xyz/guide/factor/)** 与 **[参数优化](https://akquant.akfamily.xyz/guide/optimization/)**。
  214. * **[机器学习指南](https://akquant.akfamily.xyz/advanced/ml/)**:使用内置框架做滚动训练。
  215. * **[可视化](https://akquant.akfamily.xyz/guide/visualization/)**:报告、K 线复盘与指标绘制。
  216. * **[MCP 服务](https://akquant.akfamily.xyz/guide/mcp/)**:在 AI 助手中查 API、写策略、跑回测。
  217. * **[API 参考](https://akquant.akfamily.xyz/reference/api/)**:类与函数文档。
  218. ## 开发与测试
  219. 回测引擎的正确性由 Rust 与 Python 两侧的单元测试,以及黄金测试(Golden Tests)保证:后者用合成数据验证 T+1、涨跌停、保证金、期权希腊值等关键规则,并与锁定的基线结果比对。
  220. 从源码构建、运行测试和提交代码的步骤见[贡献指南](https://github.com/akfamily/akquant/blob/main/CONTRIBUTING.md)。
  221. ## 引用
  222. 如需在论文或报告中引用本项目,可使用以下 BibTeX:
  223. ```bibtex
  224. @misc{akquant,
  225. author = {Albert King and Yaojie Zhang and Chao Liang},
  226. title = {AKQuant},
  227. year = {2026},
  228. publisher = {GitHub},
  229. journal = {GitHub repository},
  230. howpublished = {\url{https://github.com/akfamily/akquant}},
  231. }
  232. ```
  233. ## 许可证
  234. [MIT License](LICENSE)

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.