Generates executable quantitative strategy code for the akquant framework, covering data interfaces, event-driven logic, risk control, and parameter optimization.
Ships in quant-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
本 Skill 用于辅助 AI 编程智能体生成符合 akquant 框架规范的可执行量化策略代码。能力包括策略设计、回测配置、订单管理、风控规则、参数优化与横截面策略实现。
当用户表达以下意图时触发:
参考 strategy-patterns.md 选择范式:
关键决策点:
使用 assets/strategy-template.py 作为起点:
from akquant import Strategy, Bar
class MyStrategy(Strategy):
warmup_period = 20 # 预热数据长度
def __init__(self, param1=10):
self.param1 = param1
def on_start(self):
self.subscribe("600000")
def on_bar(self, bar: Bar):
# 核心交易逻辑
history = self.get_history(self.param1, bar.symbol, "close")
if len(history) < self.param1:
return
import numpy as np
ma = np.mean(history)
pos = self.get_position(bar.symbol)
if bar.close > ma and pos == 0:
self.buy(bar.symbol, 100)
elif bar.close < ma and pos > 0:
self.sell(bar.symbol, 100)
参考 api-reference.md 设置参数:
from akquant import run_backtest
result = run_backtest(
strategy=MyStrategy,
data=df,
symbol="600000",
initial_cash=500_000.0,
commission_rate=0.0003,
stamp_tax_rate=0.001,
t_plus_one=True, # A 股 T+1 规则
warmup_period=20,
execution_mode="NextOpen",
)
参考 risk-management.md 配置:
from akquant.config import RiskConfig
result = run_backtest(
...,
risk_config=RiskConfig(
max_position_pct=0.10, # 单标的持仓不超过 10%
max_account_drawdown=0.20, # 最大回撤 20%
max_daily_loss=0.05, # 单日亏损 5%
),
)
参考 optimization.md 执行:
from akquant import run_grid_search, run_walk_forward
# 网格搜索
results = run_grid_search(
strategy=MyStrategy,
param_grid={"param1": [10, 20, 30]},
data=df,
sort_by="sharpe_ratio",
)
# 滚动优化(推荐)
wfo_results = run_walk_forward(
strategy=MyStrategy,
param_grid={"param1": [10, 20, 30]},
data=df,
train_period=250,
test_period=60,
metric="sharpe_ratio",
)
参考 cross-section-guide.md 实现多标的轮动:
推荐范式:使用 on_timer 统一触发调仓
class CrossSectionStrategy(Strategy):
def __init__(self):
self.universe = ["sh600519", "sz000858", "sh601318"]
def on_start(self):
self.add_daily_timer("14:55:00", "rebalance")
def on_timer(self, payload):
if payload != "rebalance":
return
# 计算所有标分数
scores = {}
for symbol in self.universe:
history = self.get_history(20, symbol, "close")
scores[symbol] = (history[-1] - history[0]) / history[0]
# 选出最佳标的并调仓
best = max(scores, key=scores.get)
self.order_target_percent(0.95, symbol=best)
| 资源 | 用途 | 何时读取 |
|---|---|---|
| api-reference.md | API 速查 | 查询函数签名与参数 |
| strategy-patterns.md | 策略范式 | 设计策略结构 |
| risk-management.md | 风控配置 | 设置风控规则 |
| optimization.md | 参数优化 | 调优策略参数 |
| cross-section-guide.md | 横截面策略 | 实现多标的轮动 |
| strategy-template.py | 策略模板 | 快速生成代码骨架 |
由于 akquant 依赖 pandas>=3.0.0,全局安装可能与现有项目存在版本冲突。推荐使用 uv 创建隔离环境:
# macOS
brew install uv
# Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# 创建项目目录
mkdir my-quant-strategy
cd my-quant-strategy
# 初始化项目(创建 pyproject.toml)
uv init
# 创建虚拟环境并安装依赖
uv venv
uv add akquant pandas numpy
# 方式一:使用 uv run(推荐)
uv run python my_strategy.py
# 方式二:激活虚拟环境后运行
source .venv/bin/activate # macOS/Linux
# .venv\Scripts\activate # Windows
python my_strategy.py
uv 会自动生成 uv.lock 文件,确保团队依赖一致:
# 安装精确版本(从 lock 文件)
uv sync
# 添加新依赖
uv add scipy # 自动更新 lock 文件
my-quant-strategy/
├── .venv/ # 虚拟环境(uv 自动创建)
├── pyproject.toml # 项目配置
├── uv.lock # 依赖锁定文件
├── strategies/ # 策略脚本
│ ├── ma_strategy.py
│ └── cross_section.py
└── data/ # 数据文件
└── stock_data.csv
# 一键创建并运行策略项目
mkdir quant-project && cd quant-project
uv init
uv venv
uv add akquant pandas numpy
# 创建策略文件(使用模板)
cat > strategy.py << 'EOF'
from akquant import Strategy, Bar, run_backtest
import pandas as pd
import numpy as np
class MyStrategy(Strategy):
warmup_period = 20
def on_bar(self, bar: Bar):
closes = self.get_history(20, bar.symbol, "close")
if len(closes) < 20:
return
ma = np.mean(closes)
pos = self.get_position(bar.symbol)
if bar.close > ma and pos == 0:
self.buy(bar.symbol, 100)
elif bar.close < ma and pos > 0:
self.sell(bar.symbol, pos)
# 准备数据并运行回测
# result = run_backtest(strategy=MyStrategy, data=df, symbol="600000")
EOF
# 运行策略
uv run python strategy.py
from akquant import Strategy, Bar
import numpy as np
class DualMAStrategy(Strategy):
warmup_period = 30
def __init__(self, fast=10, slow=20):
self.fast = fast
self.slow = slow
self.warmup_period = slow + 1
def on_bar(self, bar: Bar):
fast_ma = np.mean(self.get_history(self.fast, bar.symbol, "close"))
slow_ma = np.mean(self.get_history(self.slow, bar.symbol, "close"))
pos = self.get_position(bar.symbol)
if fast_ma > slow_ma and pos == 0:
self.buy(bar.symbol, 100)
elif fast_ma < slow_ma and pos > 0:
self.sell(bar.symbol, pos)
from akquant import Strategy, Bar, run_backtest
from akquant.config import RiskConfig
import numpy as np
class TrendStrategy(Strategy):
warmup_period = 20
def __init__(self, ma_window=20, stop_loss=0.05):
self.ma_window = ma_window
self.stop_loss = stop_loss
def on_bar(self, bar: Bar):
ma = np.mean(self.get_history(self.ma_window, bar.symbol, "close"))
pos = self.get_position(bar.symbol)
if bar.close > ma * 1.02 and pos == 0:
self.buy(bar.symbol, 100)
elif bar.close < ma * 0.98 and pos > 0:
self.sell(bar.symbol, pos)
# 运行回测
result = run_backtest(
strategy=TrendStrategy,
data=df,
symbol="600000",
initial_cash=1_000_000.0,
risk_config=RiskConfig(
max_position_pct=0.20,
max_account_drawdown=0.15,
stop_loss_threshold=0.85,
),
)
from akquant import Strategy, run_backtest
import numpy as np
class MomentumRotation(Strategy):
def __init__(self, lookback=20):
self.lookback = lookback
self.universe = ["sh600519", "sz000858", "sh601318"]
self.warmup_period = lookback + 1
def on_start(self):
for symbol in self.universe:
self.subscribe(symbol)
self.add_daily_timer("14:55:00", "rebalance")
def on_timer(self, payload):
if payload != "rebalance":
return
scores = {}
for symbol in self.universe:
closes = self.get_history(self.lookback, symbol, "close")
if len(closes) < self.lookback:
return
scores[symbol] = (closes[-1] - closes[0]) / closes[0]
# 选出最佳标的,持仓 95%
best = max(scores, key=scores.get)
self.order_target_percent(0.95, symbol=best)
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
6 skills in this plugin
/backtraderBacktrader 开源量化回测框架,支持多数据源、多策略、多周期回测与实盘交易,纯Python实现。当用户需要开发量化策略、进行回测分析、编写交易逻辑、回测参数优化,或提及 backtrader、量化回测框架时使用。若用户仅需数据获取而无回测需求,引导使用 baostock/akshare/tushare 等数据 Skill。/equity-researcher机构级投研报告生成技能。覆盖中国A股、港股、美股上市公司。输出模式:1)投资速览:3-5页,含公司概览、核心财务指标、估值倍数、投资逻辑与风险因素;2)深度研报:≥25页,含行业分析、产业链图谱、三表模型、DCF估值、情景分析与敏感性测试。触发条件:1)直接请求:分析/看看/研究/调研/介绍一下 + 公司名或股票;2)投资询问:你怎么看/能不能买/值得投吗/帮我看看/扒一扒 + 公司或股票;3)关键词:研报、投资简报、投资速览、公司一页纸、深度研报、深度研究、深度分析、个股分析、一页纸、速览、公司速览、投研报告;4)股票代码:标准格式(如 600519.SH、0700.HK、AAPL、TSLA、BYD)。意图不明确时主动询问输出模式,禁止默认推断。/joinquant-strategy聚宽(JoinQuant)官网策略开发指南,涵盖回测、模拟交易、数据 API、交易函数、因子与技术指标。当用户编写聚宽策略、回测、模拟盘,排查模拟盘改代码不生效/替换代码,查询聚宽 API、get_price/order/run_daily、Alpha 因子、技术指标,或提及 joinquant、聚宽、jqdata 策略时使用。本地数据获取请用 jqdatasdk 技能。/qmt-strategyQMT 客户端(迅投极速策略交易系统)内置 Python 策略引擎开发指南,涵盖策略编写、回测、实盘交易、内置 API 参考与代码示例(基于 ContextInfo/passorder/get_trade_detail_data 等,非 xtquant 库)。当用户需要在 QMT 客户端中编写/回测/运行策略、查询 QMT 内置 API、从聚宽迁移至 QMT,或提及 QMT 策略、迅投策略、QMT 回测时使用;直接提及 passorder、ContextInfo、handlebar、get_market_data_ex、get_trade_detail_data 等内置 API,或描述 QMT 策略报错、回测无成交、GBK 编码报错等问题时同样适用。外部 xtquant/xtdata 连接场景不适用,请用 miniqmt 技能。/rqalphaRQAlpha 米筐开源事件驱动回测框架。支持A股和期货,模块化架构,可自由扩展;当用户需要快速回测A股/期货策略、使用内置数据(download-bundle)、开发Mod插件,或提及 rqalpha、米筐时使用。与 backtrader 相比,rqalpha 内置A股日线数据、安装更简便,适合快速验证;backtrader 更灵活、社区更大。若用户仅需数据获取而无回测需求,引导使用 baostock/akshare/tushare 等数据 Skill。/quant-skills:akquantfrom quant-skills
npx claudepluginhub lzwme/finance-quant-skills --plugin quant-skillsOther plugins' skills for the same job