Skip to content
All library documents

Backtesting Intraday Pairs Trading Without Treating the Spread as an Asset

Article Quant Q&A · Author: nikke

Summary

The document describes difficulties backtesting a simple intraday pairs strategy on 30-minute bars. The author has calculated a spread, hedge ratio, and standard deviation, and uses spread thresholds to create long and short signals. Treating the spread itself as a traded instrument fails because it crosses zero, making percentage returns undefined or misleading. A package that handles daily pair trading also appears to calculate intraday leg returns incorrectly in this example.

The code shown carries signals and hedge ratios forward, calls a package return function, and compounds the resulting returns, but the plotted performance keeps rising even while the spread continues to widen. This is presented as an unresolved implementation question, not as a validated backtest or strategy result. The document offers no corrected return calculation and does not detail costs, execution assumptions, or position sizing, so those remain necessary considerations for a reliable evaluation.

Key ideas

  • A spread that crosses zero cannot be treated as a conventional asset for percentage-return calculations.
  • Pairs strategies require returns to be calculated from the two underlying legs and their hedge weights.
  • The example reports suspect intraday package output, with compounded returns rising while the spread widens.
  • The document asks for a suitable calculation method but provides no confirmed fix or validated performance evidence.

Tags

Full text
# Backtest pair trade strategy in R


# Backtest pair trade strategy in R












I am looking for some tips on how to run a simple backtest on a pairtrading strategy intraday using eg. 30minute bars.

I have calculated the spread, `beta(=ratio/hedgeweight)`, and standard deviation.

I tried treating the spread as a simple instrument which I buy and sell, but realised I cannot do it because the returns from eg. 0 to 0.2 becomes infinite. The spread crosses zero multiple times.

I tried using the quantstrat package, but unfortunately it seems a bit complex and overkill at this point.

After lots of looking around and trial and error I ran into the PairTrading package. This works great for daily data and especially with the included data. With intraday data it seems to have problems calculating the returns correctly. It calculates the returns for the legs independently but somewhere it goes wrong.

What would be the easiest way to get the returns from a simple intraday pairtrading strategy?

Here is some sample code I have where I try to use the PairTrading package.

## Backtesting code below

```
sig <- ifelse(sprd < -2*standarddev, 1, NA)
sig <- ifelse(sprd > 2*standarddev, -1, sig)
sig <- na.locf(sig)

ret <- Return(pair, lag(sig), lag(beta))
ret <- (100 * cumprod(1 + ret))
plot(ret)
```

Unfortunately this doesnt give right results. In the picture can be seen how the returns just continue up although spread still increases.

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.