Paired Prices Returns Correlation with R

Last Update: February 6, 2020

Pairs identification consists of searching for paired asset prices which moved together based on fundamental or technical factors and initially tested through their returns’ correlation coefficient.

This topic is part of Pairs Trading Analysis with R course. Feel free to take a look at Course Curriculum.

This tutorial has an educational and informational purpose and doesn’t constitute any type of trading or investment advice. All content, including code and data, is presented for personal educational use exclusively and with no guarantee of exactness of completeness. Past performance doesn’t guarantee future results. Please read full Disclaimer.

Initial pairs identification is done through correlation coefficient which consists of measuring the degree to which paired assets prices returns moved together taking into consideration their standard deviation. It is used for evaluating short-term relationships.

1. Formula notation.

\rho \left ( y,x \right )=\frac{\sum_{t=1}^{n}\left ( y_{t}-\mu y \right )\left ( x_{t}-\mu x \right )}{\sqrt{\sum_{t=1}^{n}\left ( y_{t}-\mu y \right )^{2}\sum_{t=1}^{n}\left ( x_{t}-\mu x \right )^{2}}}

Where \rho\left ( y,x \right ) = paired assets y and x prices arithmetic returns’ correlation coefficient, y_{t} = current period asset y prices arithmetic return, x_{t} = current period asset x prices arithmetic return, \mu y = asset y prices arithmetic returns arithmetic mean and \mu x = asset x prices arithmetic returns arithmetic mean.

2. R script code example.

2.1. Load R package [1].


2.2. Paired prices returns’ correlation data reading, training and testing ranges delimiting.

  • Data: MSCI® Germany and France indexes replicating ETFs (ticker symbols: EWG and EWQ) daily adjusted close prices (2007-2016).
  • Training and testing ranges delimiting not fixed and only included for educational purposes.
data <- read.csv('Paired-Prices-Returns-Correlation-Data.txt',header=T)
data <- xts(data[,2:3],[,1]))
tdata <- data['::2014-12-31']
fdata <- data['2015-01-02::']

2.3. Paired prices chart.

  • Paired prices chart within training range.
tger <- tdata[,1]
tfra <- tdata[,2]
plot(as.zoo(tger),main='Paired Prices Chart',xlab='',ylab='')

2.4. Paired prices returns’ correlation calculation.

  • Paired prices returns’ correlation calculation within training range.
rtger <- dailyReturn(tger)
rtfra <- dailyReturn(tfra)
colnames(rtger) <- 'rtger'
colnames(rtfra) <- 'rtfra'
rtger 0.9514404

3. References.

[1] Jeffrey A. Ryan and Joshua M. Ulrich. “quantmod: Quantitative Financial Modelling Framework”. R package version 0.4-15. 2019.