pair-trading
它能做什么
这项技能测试一种押注两项相关资产会回归其通常价格关系的策略。适合研究资产之间统计价差的交易者。
适用场景
- ✓当你有两只关系紧密的股票或加密货币,并想测试均值回归策略的时候。
- ✓当两项资产之间的价格比率明显偏离其均值的时候。
- ✓当你想测试一个简单的双仓位交易思路的时候。
技能链接
https://github.com/HKUDS/Vibe-Trading/blob/main/agent/src/skills/pair-trading/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请用 60 天的回看期,测试可口可乐和百事之间的配对交易。”
每周 5 个新技能,1 分钟读完。
工作原理
它会跟踪两项资产之间的价格比率,并将其转换为 Z 分数,以显示价差有多不寻常。当价差较大时开仓,当价差回到均值附近时平仓。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Pair Trading Strategy
Purpose
Select two highly correlated instruments (such as stocks from the same industry or BTC/ETH), monitor how far their price ratio (or spread) deviates from the mean, and trade against extreme deviations while waiting for mean reversion.
Signal Logic
- Compute the price ratio:
ratio = close_A / close_B - Rolling mean and standard deviation:
mean = ratio.rolling(lookback).mean(),std = ratio.rolling(lookback).std() - Z-score:
z = (ratio - mean) / std - Signal generation:
- Z < -entry_z → long A, short B (ratio is too low, expected to revert)
- Z > +entry_z → short A, long B (ratio is too high, expected to revert)
- |Z| < exit_z → close the position (reverted back near the mean)
Implementation Notes
- Pair trading requires exactly two instruments (
codesarray length = 2) - The first instrument is A (
leg1), and the second is B (leg2) - Signals for A and B are opposite: when A is long, B is short, and vice versa
- Equal-weight allocation only: A and B each take 50% of capital, with no precise hedge-ratio calculation
Parameters
| Parameter | Default | Description |
|---|---|---|
| lookback | 60 | Lookback window for mean and standard deviation |
| entry_z | 2.0 | Entry Z-score threshold |
| exit_z | 0.5 | Exit Z-score threshold |
Example config.json
{
"source": "tushare",
"codes": ["601318.SH", "601628.SH"],
"start_date": "2023-01-01",
"end_date": "2024-12-31",
"initial_cash": 1000000,
"commission": 0.001,
"extra_fields": null
}
Cryptocurrency version:
{
"source": "okx",
"codes": ["BTC-USDT", "ETH-USDT"],
"start_date": "2024-01-01",
"end_date": "2024-12-31",
"initial_cash": 1000000,
"commission": 0.001,
"extra_fields": null
}
Common Pitfalls
codesmust contain exactly 2 instruments, no more and no less- The date indexes of the two instruments must be aligned (use an inner join), otherwise the ratio calculation will be wrong
- Before the lookback window is filled, Z-scores are
NaN, so fill signals with 0 - Do not generate same-direction signals for both A and B; pair trading is fundamentally a long-short hedge
Dependencies
pip install pandas numpy
Signal Convention
- Instrument A:
0.5= long,-0.5= short,0= flat - Instrument B: direction is opposite to A
常见问题
我需要几个标的?
正好两个。该策略会将资金在两者之间平均分配。
Z 分数是什么意思?
它显示当前价格比率偏离其均值有多少个标准差。
它能精确地调整仓位大小吗?
不能。它使用资金平均分配的方式,不计算精确的对冲比率。
详情
作者的原始描述
Pair trading strategy. Trades mean reversion using the spread/ratio Z-score of two correlated instruments. Requires at least two instruments.
repo: HKUDS/Vibe-Trading
热度
- GitHub 星标
- 37k
- 排名
- #7540
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …