pair-trade-screener by tradermonty/claude-trading-skills
npx skills add https://github.com/tradermonty/claude-trading-skills --skill pair-trade-screener此技能通过配对交易识别和分析统计套利机会。配对交易是一种市场中立策略,无论整体市场方向如何,都能从两种相关证券的相对价格变动中获利。该技能使用严格的统计方法,包括相关性分析和协整检验,以寻找稳健的交易配对。
核心方法论:
关键优势:
在以下情况使用此技能:
示例用户请求:
目标: 建立用于分析配对关系的股票池。
选项 A:基于板块的筛选(推荐)
选择特定板块进行筛选:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
选项 B:自定义股票列表
用户提供特定的股票代码进行分析:
示例:["AAPL", "MSFT", "GOOGL", "META", "NVDA"]
选项 C:特定行业
将焦点缩小到板块内的特定行业:
筛选标准:
目标: 获取用于相关性和协整分析的价格历史数据。
数据要求:
FMP API 端点:
GET /v3/historical-price-full/{symbol}?apikey=YOUR_API_KEY
数据验证:
脚本执行:
python scripts/fetch_price_data.py --sector Technology --lookback 730
目标: 识别具有强线性关系的候选配对。
相关性分析:
对于范围中的每对股票 (i, j):
相关性解读:
Beta 值计算:
对于每个候选配对(股票 A,股票 B):
Beta = Covariance(A, B) / Variance(B)
Beta 值表示对冲比率:
相关性稳定性检查:
目标: 统计验证长期均衡关系。
协整的重要性:
增广迪基-富勒检验:
对于每个相关配对:
价差 = 价格_A - (Beta × 价格_B)协整解读:
半衰期计算:
估计均值回归速度:
半衰期 = -log(2) / log(均值回归系数)
Python 实现:
from statsmodels.tsa.stattools import adfuller
# 计算价差
spread = price_a - (beta * price_b)
# ADF 检验
result = adfuller(spread)
adf_stat = result[0]
p_value = result[1]
# 解读
is_cointegrated = p_value < 0.05
目标: 量化当前价差与均衡状态的偏离程度。
价差计算:
两种常用方法:
方法 1:价格差(加法)
价差 = 价格_A - (Beta × 价格_B)
最适合:价格水平相似的股票
方法 2:价格比(乘法)
价差 = 价格_A / 价格_B
最适合:价格水平不同的股票,更易于解读
Z 分数计算:
衡量价差偏离其均值多少个标准差:
Z 分数 = (当前价差 - 平均价差) / 价差标准差
Z 分数解读:
历史价差分析:
目标: 提供具有明确规则的可操作交易信号。
入场条件:
保守方法(Z ≥ ±2.0):
做多信号:
- Z 分数 < -2.0(价差低于均值 2+ 个标准差)
- 价差均值回归(协整 p 值 < 0.05)
- 半衰期 < 60 天
→ 操作:买入股票 A,做空股票 B(对冲比率 = beta)
做空信号:
- Z 分数 > +2.0(价差高于均值 2+ 个标准差)
- 价差均值回归(协整 p 值 < 0.05)
- 半衰期 < 60 天
→ 操作:做空股票 A,买入股票 B(对冲比率 = beta)
激进方法(Z ≥ ±1.5):
出场条件:
主要出场:均值回归(Z = 0)
当价差回归到均值时出场(Z 分数穿越 0)
→ 同时平仓两个头寸
次要出场:部分获利了结
当 Z 分数达到 ±1.0 时平仓 50%
在 Z 分数 = 0 时平仓剩余的 50%
止损:
如果 Z 分数超出 ±3.0(极端偏离)则出场
风险:关系可能出现结构性断点
基于时间的出场:
如果 90 天后没有均值回归,则出场
防止无限期持有失效的配对
目标: 确定市场中立敞口的美元金额。
市场中立规模调整:
对于一个配对(股票 A,股票 B),beta = β:
等额美元敞口:
如果分配给此配对的组合规模 = 10,000 美元:
- 做多 5,000 美元的股票 A
- 做空 5,000 美元 × β 的股票 B
示例(β = 1.2):
- 做多 5,000 美元股票 A
- 做空 6,000 美元股票 B
→ 市场中立,beta = 0
仓位规模考虑因素:
风险指标:
目标: 创建结构化的 Markdown 报告,包含发现和建议。
报告部分:
执行摘要
协整配对表格
详细分析(前 10 个配对)
价差图表(基于文本)
风险警告
文件命名约定:
pair_trade_analysis_[板块]_[YYYY-MM-DD].md
示例:pair_trade_analysis_Technology_2025-11-08.md
有效配对的最低要求:
危险信号(排除配对):
交易成本:
卖空:
执行:
目的: 在板块或自定义列表中筛选协整配对。
用法:
# 基于板块的筛选
python scripts/find_pairs.py --sector Technology --min-correlation 0.70
# 自定义股票列表
python scripts/find_pairs.py --symbols AAPL,MSFT,GOOGL,META --min-correlation 0.75
# 完整选项
python scripts/find_pairs.py \
--sector Financials \
--min-correlation 0.70 \
--min-market-cap 2000000000 \
--lookback-days 730 \
--output pairs_analysis.json
参数:
--sector:板块名称(Technology, Financials 等)--symbols:逗号分隔的股票代码列表(替代板块)--min-correlation:最小相关性阈值(默认:0.70)--min-market-cap:最低市值过滤器(默认:20亿美元)--lookback-days:历史数据周期(默认:730天)--output:输出 JSON 文件(默认:stdout)--api-key:FMP API 密钥(或设置 FMP_API_KEY 环境变量)输出:
[
{
"pair": "AAPL/MSFT",
"stock_a": "AAPL",
"stock_b": "MSFT",
"correlation": 0.87,
"beta": 1.15,
"cointegration_pvalue": 0.012,
"adf_statistic": -3.45,
"half_life_days": 42,
"current_zscore": -2.3,
"signal": "LONG",
"strength": "Strong"
}
]
目的: 分析特定配对的价差行为并生成交易信号。
用法:
# 分析特定配对
python scripts/analyze_spread.py --stock-a AAPL --stock-b MSFT
# 自定义回看周期
python scripts/analyze_spread.py \
--stock-a JPM \
--stock-b BAC \
--lookback-days 365 \
--entry-zscore 2.0 \
--exit-zscore 0.5
参数:
--stock-a:第一个股票代码--stock-b:第二个股票代码--lookback-days:分析周期(默认:365)--entry-zscore:入场的 Z 分数阈值(默认:2.0)--exit-zscore:出场的 Z 分数阈值(默认:0.0)--api-key:FMP API 密钥输出:
统计套利和配对交易的全面指南:
协整检验的深入探讨:
板块分析师集成:
技术分析师集成:
回测专家集成:
市场环境分析集成:
组合经理集成:
用例 1:科技板块配对
用户:"在科技股中寻找配对交易机会"
工作流程:
1. 筛选市值 > 100亿美元的科技板块股票
2. 计算所有配对相关性
3. 筛选相关性 ≥ 0.75 的配对
4. 运行协整检验
5. 识别当前 Z 分数极端值(|z| > 2.0)
6. 生成前 10 个配对报告
用例 2:特定配对分析
用户:"分析 AAPL 和 MSFT 作为配对交易"
工作流程:
1. 获取 AAPL 和 MSFT 的 2 年价格历史
2. 计算相关性和 beta 值
3. 进行协整检验
4. 计算当前价差和 Z 分数
5. 生成入场/出场建议
6. 提供仓位规模指导
用例 3:区域性银行配对
用户:"在区域性银行中筛选配对"
工作流程:
1. 筛选金融板块中行业 = "Regional Banks" 的股票
2. 排除市值 < 50亿美元的银行
3. 计算配对统计量
4. 按协整强度排名
5. 关注半衰期 < 45 天的配对
6. 报告前 5 个均值回归配对
问题:未找到协整配对
解决方案:
问题:所有 Z 分数接近零(无交易信号)
解决方案:
问题:配对相关性失效
解决方案:
版本:1.0 最后更新:2025-11-08 依赖项:Python 3.8+, pandas, numpy, scipy, statsmodels, requests
每周安装次数
134
代码仓库
GitHub 星标数
394
首次出现
Jan 26, 2026
安全审计
安装于
gemini-cli125
opencode122
cursor122
codex120
github-copilot119
amp117
This skill identifies and analyzes statistical arbitrage opportunities through pair trading. Pair trading is a market-neutral strategy that profits from the relative price movements of two correlated securities, regardless of overall market direction. The skill uses rigorous statistical methods including correlation analysis and cointegration testing to find robust trading pairs.
Core Methodology:
Key Advantages:
Use this skill when:
Example user requests:
Objective: Establish the pool of stocks to analyze for pair relationships.
Option A: Sector-Based Screening (Recommended)
Select a specific sector to screen:
Option B: Custom Stock List
User provides specific tickers to analyze:
Example: ["AAPL", "MSFT", "GOOGL", "META", "NVDA"]
Option C: Industry-Specific
Narrow focus to specific industry within sector:
Filtering Criteria:
Objective: Fetch price history for correlation and cointegration analysis.
Data Requirements:
FMP API Endpoint:
GET /v3/historical-price-full/{symbol}?apikey=YOUR_API_KEY
Data Validation:
Script Execution:
python scripts/fetch_price_data.py --sector Technology --lookback 730
Objective: Identify candidate pairs with strong linear relationships.
Correlation Analysis:
For each pair of stocks (i, j) in the universe:
Correlation Interpretation:
Beta Calculation:
For each candidate pair (Stock A, Stock B):
Beta = Covariance(A, B) / Variance(B)
Beta indicates the hedge ratio:
Correlation Stability Check:
Objective: Statistically validate long-term equilibrium relationship.
Why Cointegration Matters:
Augmented Dickey-Fuller (ADF) Test:
For each correlated pair:
Spread = Price_A - (Beta × Price_B)Cointegration Interpretation:
Half-Life Calculation:
Estimate mean-reversion speed:
Half-Life = -log(2) / log(mean_reversion_coefficient)
Python Implementation:
from statsmodels.tsa.stattools import adfuller
# Calculate spread
spread = price_a - (beta * price_b)
# ADF test
result = adfuller(spread)
adf_stat = result[0]
p_value = result[1]
# Interpret
is_cointegrated = p_value < 0.05
Objective: Quantify current spread deviation from equilibrium.
Spread Calculation:
Two common methods:
Method 1: Price Difference (Additive)
Spread = Price_A - (Beta × Price_B)
Best for: Stocks with similar price levels
Method 2: Price Ratio (Multiplicative)
Spread = Price_A / Price_B
Best for: Stocks with different price levels, easier interpretation
Z-Score Calculation:
Measures how many standard deviations spread is from its mean:
Z-Score = (Current_Spread - Mean_Spread) / Std_Dev_Spread
Z-Score Interpretation:
Historical Spread Analysis:
Objective: Provide actionable trading signals with clear rules.
Entry Conditions:
Conservative Approach (Z ≥ ±2.0):
LONG Signal:
- Z-score < -2.0 (spread 2+ std devs below mean)
- Spread is mean-reverting (cointegration p < 0.05)
- Half-life < 60 days
→ Action: Buy Stock A, Short Stock B (hedge ratio = beta)
SHORT Signal:
- Z-score > +2.0 (spread 2+ std devs above mean)
- Spread is mean-reverting (cointegration p < 0.05)
- Half-life < 60 days
→ Action: Short Stock A, Buy Stock B (hedge ratio = beta)
Aggressive Approach (Z ≥ ±1.5):
Exit Conditions:
Primary Exit: Mean Reversion (Z = 0)
Exit when spread returns to mean (z-score crosses 0)
→ Close both legs simultaneously
Secondary Exit: Partial Profit Take
Exit 50% when z-score reaches ±1.0
Exit remaining 50% at z-score = 0
Stop Loss:
Exit if z-score extends beyond ±3.0 (extreme divergence)
Risk: Possible structural break in relationship
Time-Based Exit:
Exit after 90 days if no mean-reversion
Prevents holding broken pairs indefinitely
Objective: Determine dollar amounts for market-neutral exposure.
Market Neutral Sizing:
For a pair (Stock A, Stock B) with beta = β:
Equal Dollar Exposure:
If portfolio size = $10,000 allocated to this pair:
- Long $5,000 of Stock A
- Short $5,000 × β of Stock B
Example (β = 1.2):
- Long $5,000 Stock A
- Short $6,000 Stock B
→ Market neutral, beta = 0
Position Sizing Considerations:
Risk Metrics:
Objective: Create structured markdown report with findings and recommendations.
Report Sections:
Executive Summary
Cointegrated Pairs Table
Detailed Analysis (Top 10 Pairs)
Spread Charts (Text-Based)
Risk Warnings
File Naming Convention:
pair_trade_analysis_[SECTOR]_[YYYY-MM-DD].md
Example: pair_trade_analysis_Technology_2025-11-08.md
Minimum Requirements for Valid Pair:
Red Flags (Exclude Pair):
Transaction Costs:
Short Selling:
Execution:
Purpose: Screen for cointegrated pairs within a sector or custom list.
Usage:
# Sector-based screening
python scripts/find_pairs.py --sector Technology --min-correlation 0.70
# Custom stock list
python scripts/find_pairs.py --symbols AAPL,MSFT,GOOGL,META --min-correlation 0.75
# Full options
python scripts/find_pairs.py \
--sector Financials \
--min-correlation 0.70 \
--min-market-cap 2000000000 \
--lookback-days 730 \
--output pairs_analysis.json
Parameters:
--sector: Sector name (Technology, Financials, etc.)--symbols: Comma-separated list of tickers (alternative to sector)--min-correlation: Minimum correlation threshold (default: 0.70)--min-market-cap: Minimum market cap filter (default: $2B)--lookback-days: Historical data period (default: 730 days)--output: Output JSON file (default: stdout)--api-key: FMP API key (or set FMP_API_KEY env var)Output:
[
{
"pair": "AAPL/MSFT",
"stock_a": "AAPL",
"stock_b": "MSFT",
"correlation": 0.87,
"beta": 1.15,
"cointegration_pvalue": 0.012,
"adf_statistic": -3.45,
"half_life_days": 42,
"current_zscore": -2.3,
"signal": "LONG",
"strength": "Strong"
}
]
Purpose: Analyze a specific pair's spread behavior and generate trading signals.
Usage:
# Analyze specific pair
python scripts/analyze_spread.py --stock-a AAPL --stock-b MSFT
# Custom lookback period
python scripts/analyze_spread.py \
--stock-a JPM \
--stock-b BAC \
--lookback-days 365 \
--entry-zscore 2.0 \
--exit-zscore 0.5
Parameters:
--stock-a: First stock ticker--stock-b: Second stock ticker--lookback-days: Analysis period (default: 365)--entry-zscore: Z-score threshold for entry (default: 2.0)--exit-zscore: Z-score threshold for exit (default: 0.0)--api-key: FMP API keyOutput:
Comprehensive guide to statistical arbitrage and pair trading:
Deep dive into cointegration testing:
Sector Analyst Integration:
Technical Analyst Integration:
Backtest Expert Integration:
Market Environment Analysis Integration:
Portfolio Manager Integration:
Use Case 1: Technology Sector Pairs
User: "Find pair trading opportunities in tech stocks"
Workflow:
1. Screen Technology sector for stocks with market cap > $10B
2. Calculate all pairwise correlations
3. Filter pairs with correlation ≥ 0.75
4. Run cointegration tests
5. Identify current z-score extremes (|z| > 2.0)
6. Generate top 10 pairs report
Use Case 2: Specific Pair Analysis
User: "Analyze AAPL and MSFT as a pair trade"
Workflow:
1. Fetch 2-year price history for AAPL and MSFT
2. Calculate correlation and beta
3. Test for cointegration
4. Calculate current spread and z-score
5. Generate entry/exit recommendation
6. Provide position sizing guidance
Use Case 3: Regional Bank Pairs
User: "Screen for pairs among regional banks"
Workflow:
1. Filter Financials sector for industry = "Regional Banks"
2. Exclude banks with <$5B market cap
3. Calculate pairwise statistics
4. Rank by cointegration strength
5. Focus on pairs with half-life < 45 days
6. Report top 5 mean-reverting pairs
Problem: No cointegrated pairs found
Solutions:
Problem: All z-scores near zero (no trade signals)
Solutions:
Problem: Pair correlation broke down
Solutions:
Version : 1.0 Last Updated : 2025-11-08 Dependencies : Python 3.8+, pandas, numpy, scipy, statsmodels, requests
Weekly Installs
134
Repository
GitHub Stars
394
First Seen
Jan 26, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
gemini-cli125
opencode122
cursor122
codex120
github-copilot119
amp117
Python PDF处理教程:合并拆分、提取文本表格、创建PDF文件
62,000 周安装
网站克隆技能:多智能体工作流实现像素级网站克隆,支持React + Tailwind CSS
132 周安装
Kernel Auth:为任何网站设置托管认证连接,自动化登录与安全检查
132 周安装
前端设计终极指南:用React+TypeScript+Tailwind CSS打造独特静态站点,无需设计稿
132 周安装
Nginx配置优化器 - 高性能Web服务与反向代理配置生成工具
132 周安装
Go设计模式与最佳实践指南:函数式选项、资源管理与代码审查
132 周安装
Tiptap Editor API 使用指南与最佳实践 - vmark 开发经验总结
132 周安装