Skip to content
All library documents

Estimating a Pairs Trading Hedge Ratio with Ordinary Least Squares

Article Quant Q&A · Author: x86

Summary

The document describes estimating a hedge ratio for a pairs trading spread from two aligned price series. Ordinary least squares regresses one security’s prices on the other, and the fitted slope is used to scale one leg when calculating the spread. A Java response shows adding paired closing prices to a simple regression implementation configured without an intercept, illustrating one way to obtain the slope.

The material provides a coding example and the intended spread formula, but it does not discuss whether the regression should include an intercept, how to choose a lookback window, or how to test whether the spread is mean reverting. It mentions comparing the regression spread with normalized moving averages but reports no comparison or strategy results. The hedge ratio is an estimation choice and does not by itself establish a profitable or stable pairs trade.

Key ideas

  • OLS can estimate a hedge ratio from paired observations of two securities’ prices.
  • The regression slope scales one asset in the spread calculation.
  • The Java example fits a simple regression without an intercept.
  • A fitted hedge ratio alone does not show that a spread is stable or profitable.

Tags

Full text
# How to calculate the hedge ratio between two securities using the Least Squares model in Java


# How to calculate the hedge ratio between two securities using the Least Squares model in Java












How does one, having two lists of prices Y and X and wishing to fit the Least Squares model, perform this in Java?

The fit is required to calculate the hedge ratio in a pairs trading strategy.

For every period, the spread is calculated as follows:

```
symbol1.close - hedgeRatio * symbol2
```

We intend to compare the performance of this formula versus using normalized moving averages.

The below Python code uses the statsmodels library and the Ordinary Least Squares (OLS) method.

```
import statsmodels.api as sm

def hedge_ratio(Y, X):
    X = sm.add_constant(X)
    model = sm.OLS(Y, X).fit()
    return model.params[1]
```

## Answer by x86 (score 1)

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

The below code works for us:

```
    final boolean includeIntercept = false;
    SimpleRegression simpleRegression = new SimpleRegression(includeIntercept);

    for (int i = 0; i < symbol1Bars.size(); i++) {
        simpleRegression.addData(symbol1Bars.get(i).getClose(), symbol2Bars.get(i).getClose());
    }
```

This uses the Apache Commons Math 3 library

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.