Skip to content
All library documents

Constructing a Cointegrated Pairs Trading Strategy with Spread Z-Scores

Article Quant Q&A · Author: Eka

Summary

The document explains how to turn a cointegrated pair into a mean-reversion strategy. Rather than treating the ratio of two prices as the spread, it proposes estimating a hedge coefficient, forming a residual as one asset’s price minus the coefficient times the other’s price, and measuring that residual with a z-score. The coefficient and spread can be recalculated on each new bar.

The example opens a short spread position when the z-score is sufficiently positive and a long spread position when it is sufficiently negative. Each trade combines opposing positions in the two assets, scaled by the hedge coefficient. It closes positions when the z-score moves back toward the mean, using separate thresholds for entry and exit. The answer points to an external example of an intraday strategy, but the document itself provides no performance evidence. It does not specify how to test cointegration, choose estimation windows or thresholds, size risk, or account for transaction costs and changing relationships between assets.

Key ideas

  • A cointegrated pair can be modeled with a hedge coefficient and a residual spread rather than a simple price ratio.
  • The spread’s z-score measures its distance from its estimated mean in standard deviation units.
  • A high positive z-score signals a short spread position, while a sufficiently negative one signals a long spread position.
  • Each spread trade holds offsetting positions in both assets, with the hedge coefficient setting their relative scale.
  • Positions are closed as the spread returns toward its mean, and the document leaves model selection and trading costs unspecified.

Tags

Full text
# What is the pseudo code for a pairs trading strategy?


# What is the pseudo code for a pairs trading strategy?












I am trying to learn about pairs trading strategy. I know that we have to long and short cointegrated assests simultaneously. But I still have some confusion in how the strategy works. I wrote the pseudo code for what I think pairs trading strategy is?

```
x=price data of asset x
y=price data of asset y
if x and y are correlated and cointegrated

 calculate pair ratio(spread) x/y or y/x?
 calculate average of pair ratio(spread)

     if spread > mean
     sell asset ?
     buy asset ?
     else spread < mean
     sell asset ?
     buy asset ?
     close if

else

 find new pair of assets x and y
 go to line 1 with new x and y

close if
```

Here I am taking pair ratio(x/y or y/x) as the spread? My first question is which pair ratio should I take x/y or y/x?

if I take `x/y` as spread then what assests should I `buy` and `sell` if `spread>mean`.

If I am wrong in my assesment of pseudo code of pair trading then feel free to correct me.

## Answer by KarolisR (score 11, accepted)

https://quant.stackexchange.com/a/28043

The following link has a good summary of a typical pair trading strategy:

https://www.quantstart.com/articles/Backtesting-An-Intraday-Mean-Reversion-Pairs-Strategy-Between-SPY-And-IWM

It actually has full python code as well. It doesn't include a cointegration check though.

Edit:

```
if X and Y are cointegrated:
    calculate Beta between X and Y 
    calculate spread as X - Beta * Y
    calculate z-score of spread

    # entering trade (spread is away from mean by two sigmas):
    if z-score > 2:
        sell spread (sell 1000 of X, buy 1000 * Beta of Y)
    if z-score < -2:
        buy spread (buy 1000 of X, sell 1000 * Beta of Y)

    # exiting trade (spread converged close to mean):
    if we're short spread and z-score < 1:
        close the trades
    if we're long spread and z-score > -1:
        close the trades

# repeat above on each new bar, recalculating rolling Beta and spread etc.
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.