Skip to content
Skillpedia
Analysis & spreadsheets37k · #7540

pair-trading

What it does

This skill tests a strategy that bets two related assets will move back toward their usual price relationship. It is for traders who study statistical gaps between assets.

When to use it

  • ✓When you have two closely related stocks or coins and want to test a mean reversion strategy.
  • ✓When the price ratio between two assets moves far from its average.
  • ✓When you want to test a simple two-position trading idea.

Skill link

https://github.com/HKUDS/Vibe-Trading/blob/main/agent/src/skills/pair-trading/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/HKUDS/Vibe-Trading/blob/main/agent/src/skills/pair-trading/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“Test a pair trade between Coca-Cola and Pepsi using a 60-day lookback.”

5 new skills a week. 1 minute to read.

How it works

It tracks the price ratio between two assets and converts it into a Z-score, which shows how unusual the gap is. It opens trades when the gap is large and closes them when it returns near the average.

Technical content (original SKILL.md)

Original text by the author, straight from 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

  1. Compute the price ratio: ratio = close_A / close_B
  2. Rolling mean and standard deviation: mean = ratio.rolling(lookback).mean(), std = ratio.rolling(lookback).std()
  3. Z-score: z = (ratio - mean) / std
  4. 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 (codes array 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

  • codes must 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
FAQ

How many instruments do I need?

Exactly two. The strategy splits capital equally between them.

What does a Z-score mean?

It shows how many standard deviations the current price ratio is from its average.

Does it size positions precisely?

No. It uses an equal split of capital and does not calculate an exact hedge ratio.

Details

Original description by the author

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

Popularity

GitHub stars
37k
Ranking position
#7540
License
MIT
Updated
11/1/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills