Agent skill

Qmt Inner Backtest

by dfkai in dfkai/xtquantai

根据策略描述、研报 PDF 或截图,解读因子/选股逻辑,基于 scripts/daily-factors-backtest.py 框架生成 QMT 内置日频因子回测脚本。用户提到 QMT 内置回测、因子选股回测、截面因子、 研报复现、handlebar 回测、afterinit 预计算信号时使用。

MITAuto-check passedBusiness, Finance & HR

Install Qmt Inner Backtest

skills CLI
$ npx skills add dfkai/xtquantai --skill qmt-inner-backtest -a claude-code

Project install by default; add -g for ~/.claude/skills/.

GitHub CLI
$ gh skill install dfkai/xtquantai qmt-inner-backtest --agent claude-code

Project scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).

Manual copy
$ git clone --depth 1 https://github.com/dfkai/xtquantai.git skills-src && mkdir -p .claude/skills && cp -r skills-src/skills/qmt-inner-backtest .claude/skills/qmt-inner-backtest && rm -rf skills-src

Use ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.

Claude Code skills documentation · loads skills from .claude/skills/

Facts

Skill name
qmt-inner-backtest
GitHub stars
164
Token cost
~1.8k tokens
SKILL.md length
457 words
Files
2 (incl. scripts)
Skills in repo
1
Repo updated
First seen
Licence
MIT

At a glance

根据策略描述、研报 PDF 或截图,解读因子/选股逻辑,基于 scripts/daily-factors-backtest.py 框架生成 QMT 内置日频因子回测脚本。用户提到 QMT 内置回测、因子选股回测、截面因子、 研报复现、handlebar 回测、afterinit 预计算信号时使用。

  • Works in 4 steps: 解读策略输入 → 复制母版并替换 → 因子替换模式 → …
  • Tasks that involve Trading and backtesting
  • SKILL.md covers 概述, 适用场景, 母版架构(必须理解再改) and Agent 工作流, plus 6 more sections
  • Runs Python scripts from its folder

What it does

Qmt Inner Backtest is an agent skill from dfkai/xtquantai. 根据策略描述、研报 PDF 或截图,解读因子/选股逻辑,基于 scripts/daily-factors-backtest.py 框架生成 QMT 内置日频因子回测脚本。用户提到 QMT 内置回测、因子选股回测、截面因子、 研报复现、handlebar 回测、afterinit 预计算信号时使用。

Its SKILL.md is about 1.8k tokens, which your agent loads only when the skill is triggered. The skill folder holds 2 other files, including scripts (for example `scripts/daily-factors-backtest.py`).

It sits in Business, Finance & HR, covering Trading and backtesting. It works with Python. The repository describes itself as: 迅投 QMT 量化 AI 技能集(Agent Skills):研报因子回测脚本生成等,适用于 Claude Code / Cursor / Codex / Kimi 等 70+ AI 编程工具. The licence is MIT.

When your agent uses it

  • Tasks that involve Trading and backtesting

Example prompts

  • “/qmt-inner-backtest”

Requirements

  • Python 3

Workflow steps

4 steps, taken from the step headings in SKILL.md.

  1. 解读策略输入
  2. 复制母版并替换
  3. 因子替换模式
  4. 生成后自检

What it can do on your machine

Read from SKILL.md and the folder at commit 9f869f0. It shows what the files ask for, not the result of running them.

  • Tool permissions

    Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.

    From allowed-tools in the SKILL.md frontmatter.

  • Runs code

    Ships 1 file in scripts/ (Python), which the agent can run.

    From the folder's file list and the shell code blocks in SKILL.md.

  • Network

    No URLs in SKILL.md.

    From URLs in SKILL.md, links to its own repository left out.

  • Credentials

    Names no API keys, tokens, secrets or passwords.

    From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.

Context cost

Qmt Inner Backtest loads about 1.8k tokens when it runs. Until then it costs about 42 tokens; SKILL.md has 457 words of instructions outside code blocks.

Always · name and description, kept in context so the agent knows when to use it
~42
When it runs · the whole SKILL.md, loaded when a task matches
~1.8k

Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.

Safety

Auto-check passed

The automated check found no risky patterns in SKILL.md.

Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); the scripts in this folder are not scanned.

SKILL.md

The full file from dfkai/xtquantai at commit 9f869f0, republished under its MIT licence (© dfkai). 457 words, ~1,792 tokens.

Download SKILL.mdSave it as .claude/skills/qmt-inner-backtest/SKILL.md (or your agent's skills folder). This skill also uses 1 other file; get the full folder from GitHub.
name
qmt-inner-backtest
description
根据策略描述、研报 PDF 或截图,解读因子/选股逻辑,基于 scripts/daily-factors-backtest.py 框架生成 QMT 内置日频因子回测脚本。用户提到 QMT 内置回测、因子选股回测、截面因子、 研报复现、handlebar 回测、after_init 预计算信号时使用。

QMT 内置因子回测

概述

基于 scripts/daily-factors-backtest.py 生成 QMT 策略编辑器内置回测 脚本。

核心模式:after_init 预计算全区间因子与买卖信号 → handlebar 按调仓日执行交易。

母版路径:本 skill 目录下的 scripts/daily-factors-backtest.py(相对 SKILL.md 所在目录)

适用场景

适合不适合
日频截面因子选股(Barra 风格处理)Tick/分钟高频
固定持仓数 Top-N 等权调仓期货开平仓(qmt-future-trade,规划中)
研报因子复现、上下影线/价值/动量等目标持仓型期货实盘(qmt-live-strategy-template,规划中)
申万行业 + 市值中性化仅要信号推送(qmt-live-signal-feishu,规划中)

母版架构(必须理解再改)

daily-factors-backtest.py
├── 文件头 # coding:gbk + 策略说明 docstring
├── 因子函数库          ← 【主要替换区】factor_xxx + 中性化/去极值
├── init(C)             ← 【配置区】回测区间、股票池、资金、因子参数
├── after_init(C)       ← 【信号区】拉数据 → 算因子 → 过滤 → 生成 g.buy/sell_signals
├── handlebar(C)        ← 【执行区】调仓日卖出/买入(通常保留)
├── 交易执行函数         ← 通常原样保留
└── 辅助工具函数         ← 通常原样保留(IPO/ST/涨跌停/财务宽表)
各段职责
段函数/变量做什么
全局状态g = G()跨函数共享参数、信号矩阵、持仓
因子库factor_ubl(...)输入 OHLCV/市值等宽表,输出因子 DataFrame(index=日期, columns=股票)
因子后处理filter_extreme_mad_df / neutralize_by_market_cap / neutralize_by_industry_zscore / cross_section_zscoreBarra 风格流水线,按研报需求保留或删减
初始化init(C)设 g.start_date/g.end_date、g.stock_pool、g.max_positions、g.rebalance_days 等;预定义 g.buy_signals/g.sell_signals 防空矩阵
预计算after_init(C)一次性拉全区间行情+财务 → 算因子 → IPO/ST/停牌过滤 → 截面排名 → shift(1) 生成 T+1 信号
执行handlebar(C)每 g.rebalance_days 个交易日调仓:先卖后买,开盘价成交
交易execute_sell/buy_signals涨停不买、跌停不卖;科创板 200 股、其余 100 股整数倍
辅助get_ipo_mask / get_st_mask / get_financial_wide_table上市满 120 天、ST 区间、财务字段宽表
信号时序(防未来函数)
python
df_rank = df_factor_filtered.rank(axis=1, ascending=g.rank_ascending)
df_is_top_n = df_rank <= g.max_positions
g.buy_signals = df_is_top_n.shift(1).fillna(False)   # T 日因子 → T+1 日买入
g.sell_signals = ~g.buy_signals

禁止去掉 .shift(1),除非用户明确要求当日收盘调仓且接受前视偏差。

Agent 工作流

1. 解读策略输入

用户可能提供:文字描述、研报 PDF、截图、已有因子公式。提取并输出 策略规格表(生成前给用户确认):

markdown
## 策略规格(待确认)

| 项 | 内容 |
|----|------|
| 策略名称 | |
| 因子公式 | 逐行写明计算步骤 |
| 所需行情字段 | open/high/low/close/volume/... |
| 所需财务字段 | 如 CAPITALSTRUCTURE.free_float_capital |
| 因子窗口 | std_period / factor_period 等 |
| 排序方向 | ascending=True(值越小越好)或 False |
| 中性化 | 市值 OLS / 申万行业 Z-score / 无 |
| 股票池 | 如 中证1000、沪深300、全 A |
| 持仓数 | max_positions |
| 调仓频率 | rebalance_days(交易日) |
| 回测区间 | start_date ~ end_date |
| 初始资金 | initial_capital |

研报/PDF 解读要点:

  • 区分「因子定义」与「组合构建」(Top 10、5 日调仓等)
  • 注意「蜡烛上影线」「威廉下影线」等术语对应的 OHLC 公式
  • 记录去极值方法(MAD 几倍)、中性化顺序
  • 参数缺省时标注假设,不要静默编造

截图解读要点:

  • 对照图中公式、参数表、回测设置截图
  • 股票池名称必须与 QMT 板块名一致(见下方板块表)
2. 复制母版并替换
  1. 读取 scripts/daily-factors-backtest.py 全文作模板
  2. 输出到用户指定路径,默认 strategies/<策略名>-backtest/backtest.py
  3. 只改必要部分,交易执行与辅助函数原样保留

必改清单:

位置改什么
文件头 docstring策略名、研报来源、因子逻辑、参数说明
logger 名称与策略一致,便于日志过滤
factor_xxx()新因子计算;函数名与 g.factor_name 对应
init()回测区间、股票池、资金、因子参数、rank_ascending
after_init()数据字段获取、调用新因子函数;中性化步骤按研报增删
init 日志文案策略名称与参数摘要

通常不改: handlebar、execute_*、get_ipo_mask、get_st_mask、涨跌停判断、get_df_ex。

3. 因子替换模式

模式 A — 单因子 Top-N(母版默认)

python
def factor_xxx(daily_open, daily_high, daily_low, daily_close, daily_market_cap,
               stock_industry_map=None, **kwargs):
    # 1. 原始特征
    # 2. 滚动统计
    # 3. 去极值 → 市值中性 → 行业中性(可选)
    # 4. 截面 Z-score(单因子可跳过第 4 步)
    return factor_df

模式 B — 多子因子合成

每个子因子独立走 MAD + 中性化,最后 zscore(A) + zscore(B) 或加权求和。

模式 C — 无需中性化

跳过 neutralize_by_*,仅 filter_extreme_mad_df + cross_section_zscore。

模式 D — 需额外财务因子

在 after_init 用 get_financial_wide_table(C, g.stock_pool, 'TABLE.field', ...) 拉宽表,传入 factor_xxx。

常用财务字段示例:

  • CAPITALSTRUCTURE.free_float_capital — 自由流通股本(母版用于市值)
  • PERSHAREINDEX.eps — 每股收益
  • ASHAREINCOME.net_profit_incl_min_int_inc — 净利润
4. 生成后自检
  • 首行 # coding:gbk
  • init 中预定义 g.buy_signals / g.sell_signals 空 DataFrame
  • after_init 行情为空时 return,不抛未捕获异常
  • 信号含 .shift(1)
  • g.start_date 与 g.backtest_start_time 区间一致
  • 股票池 C.get_stock_list_in_sector(...) 名称在 QMT 中存在
  • 因子函数返回值 shape 与 daily_close 对齐(index=日期, columns=股票代码)
  • rank_ascending 与研报「因子越大越好/越小越好」一致

用户必须配置的回测项

生成脚本后,必须提醒用户在 QMT 中核对以下配置(Agent 不代替用户在 QMT GUI 操作):

A. 脚本内 init() 参数
参数格式说明
g.start_date / g.end_date'YYYYMMDD'after_init 拉行情/财务的起止
g.backtest_start_time / g.backtest_end_time'YYYY-MM-DD HH:MM:SS'与上面区间一致
g.stock_pool板块名或代码列表C.get_stock_list_in_sector("中证1000")
g.initial_capital整数初始资金
g.max_positions整数持仓只数 = Top N
g.cash_usage_ratio0~1调仓日可用资金比例,默认 0.95
g.rebalance_days整数每 N 个交易日调仓一次
g.accid'test'回测账号,保持 test
Show full SKILL.md (184 more words)Show less
B. QMT 策略编辑器回测面板

用户需在 QMT 模型交易 / 策略研究 中手动设置:

  1. 回测起止日期 — 与脚本 g.backtest_* 一致
  2. 初始资金 — 与 g.initial_capital 一致
  3. 基准 — 如沪深300、中证1000(便于对比)
  4. 手续费 / 印花税 / 滑点 — 研报有写明则告知用户按研报设
  5. 复权方式 — 脚本内 dividend_type='front_ratio'(前复权),面板需一致
  6. 品种类型 — 股票
C. 常用 QMT 板块名称
用户说法QMT sector 名
中证1000"中证1000"
沪深300"沪深300"
中证500"中证500"
全 A"沪深A股"
创业板"创业板"
科创板"科创板"
申万一级行业get_sector_list('申万一级行业板块') 下各行业

板块名因 QMT 版本可能略有差异;若 get_stock_list_in_sector 失败,提示用户在本机 QMT 板块列表中确认准确名称。

D. 数据前置
  1. QMT 客户端已登录
  2. 在「数据管理」中下载回测区间 日线行情 及所需 财务数据
  3. 股票池成分股越多,after_init 越慢(中证1000 约 1000 只,属正常)

运行方式

QMT 内置回测 不在 conda 命令行运行,流程如下:

  1. 将生成的 .py 复制到 QMT 策略目录,或在策略编辑器新建策略粘贴代码
  2. 保存后点击 编译,确认无语法错误
  3. 打开 回测 面板,设置日期/资金/费率
  4. 运行回测,查看收益曲线、持仓、日志输出
  5. 日志中关注:[数据检查]、[因子] 步骤统计、【最新调仓建议】

若用户需要在项目内留存:

text
strategies/<name>-backtest/
└── backtest.py    # 生成的策略文件

向用户确认的话术模板

策略生成前:

请确认策略规格表中的:股票池、回测区间、持仓数、调仓频率、因子方向。
若有研报未写明的参数(如 MAD 倍数、中性化顺序),我将按母版默认处理并标注。

交付脚本后:

脚本已生成。请在 QMT 中:

  1. 核对回测起止日期与脚本 init() 一致
  2. 确认股票池板块名在本机 QMT 可用
  3. 下载对应区间的日线与财务数据
  4. 设置手续费/滑点(研报有要求请按研报)
  5. 编译运行回测

默认 T 日收盘算因子、T+1 日开盘调仓。如需改调仓逻辑请说明。

与母版示例的对应关系

母版 factor_ubl 实现的是东吴证券上下影线因子:

蜡烛上影线 = High - max(Open, Close)
威廉下影线 = Close - Low
→ 标准化 → 20日 std/mean → MAD去极值 → 市值OLS中性 → 申万行业Z-score → 截面Z-score → 相加
→ 值越小越好 → Top 10 → 每5日调仓

替换其他因子时,保持相同「宽表进、宽表出」接口,其余流水线按研报裁剪。

禁止事项

  • 不要去掉 # coding:gbk
  • 不要去掉 init 中对信号变量的预定义
  • 不要默认帮用户在 QMT 里点运行;只生成脚本并给配置清单
  • 不要把期货下单逻辑混入本框架
  • 不要在因子矩阵中引入未来数据(用 shift(1) 或等价滞后)
  • 未经用户确认不要提交含资金账号的改动

快速示例

用户需求: 复现 20 日动量因子,沪深300成分,Top 20,每月调仓。

Agent 动作:

  1. 输出策略规格表供确认
  2. 新建 factor_momentum(daily_close, lookback=20):daily_close / daily_close.shift(20) - 1
  3. MAD 去极值 + 市值中性(研报若要求)
  4. g.rank_ascending = False(动量越大越好)
  5. g.stock_pool = C.get_stock_list_in_sector("沪深300")
  6. g.max_positions = 20,g.rebalance_days = 20(约月度)
  7. 提醒用户下载沪深300成分日线及设置回测费率

© dfkai, MIT. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file

Files

SKILL.md and 1 other file (scripts) in skills/qmt-inner-backtest of dfkai/xtquantai.

  • SKILL.md
  • scripts/daily-factors-backtest.py

Open the folder on GitHubat commit 9f869f0

Compare with similar skills

Qmt Inner Backtest next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.

Qmt Inner Backtest compared with similar skills
SkillStarsUsed inTokensAuto-checkLicenceRepo updated
Qmt Inner Backtest this skilldfkai/xtquantai164—~1.8kAutomated safety check: PassMIT
Tushare Datazillionare/zillionare3192 repos~2.3kAutomated safety check: PassNone
Kalshi Traderyanfrigo/kalshi-ai-trading-bot612—~3.4kAutomated safety check: PassMIT
Polymarket Tennislivetennisapi/livetennisapi-mcp152—~3kAutomated safety check: PassMIT
Quant Backtestjoemccann/market-data-warehouse183—~2.1kAutomated safety check: PassNone
Openscriptmarketcalls/openalgo2.8k—~2.3kAutomated safety check: NotesAGPL-3.0

Similar skills

  • Tushare Data

    zillionare/zillionare

    面向中文自然语言的 Tushare 数据研究技能。用于把“看看这只股票最近怎么样”“帮我查财报趋势”“最近哪个板块最强”“北向资金在买什么”“给我导出一份行情数据”这类请求,转成可执行的数据获取、清洗、对比、筛选、导出与简要分析流程。适用于 A 股、指数、ETF/基金、财务、估值、资金流、公告新闻、板块概念与宏观数据等研究场景。

    319 GitHub starsUsed in 2 repos~2.3k tokens
    Business, Finance & HRAuto-check passed
  • Kalshi Trade

    ryanfrigo/kalshi-ai-trading-bot

    The disciplined process for autonomously and profitably trading the live Kalshi account on each /loop tick, with Claude as the decision-maker.

    612 GitHub stars~3.4k tokensUpdated today
    Business, Finance & HRAuto-check passed
  • Polymarket Tennis

    livetennisapi/livetennisapi-mcp

    Build observe-only Polymarket and Kalshi tennis market tooling on the polymarket-tennis Python package (MIT) plus the Live Tennis API free tier.

    152 GitHub stars~3k tokensUpdated yesterday
    Business, Finance & HRAuto-check passed
  • Quant Backtest

    joemccann/market-data-warehouse

    Institutional-grade Python backtesting framework builder for Codex.

    183 GitHub stars~2.1k tokensUpdated 6 mo ago
    Business, Finance & HRAuto-check passed
  • Openscript

    marketcalls/openalgo

    Write an OpenScript study or strategy for OpenAlgo, and install it into strategies/openscript/ only after it compiles.

    2.8k GitHub stars~2.3k tokensUpdated today
    Business, Finance & HRAuto-check: notes
  • Backtesting

    gauss314/skills

    Academic backtesting framework for quantitative research. An agent skill from gauss314/skills.

    246 GitHub stars~2.4k tokensUpdated 3 mo ago
    Business, Finance & HRAuto-check passed

Works with

Questions about Qmt Inner Backtest

What does Qmt Inner Backtest do?

根据策略描述、研报 PDF 或截图,解读因子/选股逻辑,基于 scripts/daily-factors-backtest.py 框架生成 QMT 内置日频因子回测脚本。用户提到 QMT 内置回测、因子选股回测、截面因子、 研报复现、handlebar 回测、afterinit 预计算信号时使用。. Qmt Inner Backtest is an agent skill from dfkai/xtquantai.

When should I use Qmt Inner Backtest?

Qmt Inner Backtest fits situations like: tasks that involve Trading and backtesting.

How do I install Qmt Inner Backtest in Claude Code?

Run `npx skills add dfkai/xtquantai --skill qmt-inner-backtest -a claude-code`. Or copy the skill folder (skills/qmt-inner-backtest in dfkai/xtquantai) into .claude/skills/qmt-inner-backtest in your project. Claude Code loads it when a task matches its description.

How do I install Qmt Inner Backtest in Codex?

Run `npx skills add dfkai/xtquantai --skill qmt-inner-backtest -a codex`. Or copy the skill folder (skills/qmt-inner-backtest in dfkai/xtquantai) into .agents/skills/qmt-inner-backtest in your project. Codex loads it when a task matches its description.

Can I use Qmt Inner Backtest in Cursor, Gemini CLI or GitHub Copilot?

Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add dfkai/xtquantai --skill qmt-inner-backtest -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/qmt-inner-backtest, .gemini/skills/qmt-inner-backtest, .github/skills/qmt-inner-backtest and .opencode/skills/qmt-inner-backtest in your project.

What does Qmt Inner Backtest need to run?

Going by SKILL.md and its folder, Qmt Inner Backtest needs Python for the scripts in its folder. Our summary lists: Python 3.

Does Qmt Inner Backtest access the network?

SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.

Is Qmt Inner Backtest safe to install?

Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. The check reads SKILL.md only: the scripts in the folder are not scanned, so read them before running anything.

What licence does Qmt Inner Backtest use?

Qmt Inner Backtest is published under the MIT licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.

How many tokens does Qmt Inner Backtest use?

About 1.8k tokens (SKILL.md is roughly 7.2k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full.

What are the alternatives to Qmt Inner Backtest?

Skills that share tags, products or a category with Qmt Inner Backtest: Tushare Data (zillionare/zillionare, 319 stars), Kalshi Trade (ryanfrigo/kalshi-ai-trading-bot, 612 stars), Polymarket Tennis (livetennisapi/livetennisapi-mcp, 152 stars) and Quant Backtest (joemccann/market-data-warehouse, 183 stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.

Who maintains Qmt Inner Backtest?

dfkai (a GitHub user) maintains it in dfkai/xtquantai, which has 164 GitHub stars. The repository was last updated on June 11, 2026.

Source: dfkai/xtquantai on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.