Using R to Calculate Multiple Stock Correlations

Download the Jupyter Notebook

In [2]:
#1. install quantmod if needed
# install.packages('quantmod')
In [3]:
#2. load quantmod
library('quantmod')
Loading required package: xts
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
In [4]:
#3. get some data (you may get a warning here about the hyperparameter auto.assign)
amzn = getSymbols('AMZN', src='google', from='2016-06-15', auto.assign=FALSE)
‘getSymbols’ currently uses auto.assign=TRUE by default, but will
use auto.assign=FALSE in 0.5-0. You will still be able to use
‘loadSymbols’ to automatically load data. getOption("getSymbols.env")
and getOption("getSymbols.auto.assign") will still be checked for
alternate defaults.

This message is shown once per session and may be disabled by setting 
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
In [5]:
#4. quick exploration of quantmod charting capabilities
chartSeries(amzn, theme=chartTheme('white'), TA = 'addSMA(50)')
In [6]:
#5. get comparison data for FB, GOOG, NFLX
fb = getSymbols('FB', src='google', from='2016-06-15', auto.assign=FALSE)
goog = getSymbols('GOOG', src='google', from='2016-06-15', auto.assign=FALSE)
nflx = getSymbols('NFLX', src='google', from='2016-06-15', auto.assign=FALSE)
In [7]:
#6. take a quick look at what the data looks like
head(nflx)
           NFLX.Open NFLX.High NFLX.Low NFLX.Close NFLX.Volume
2016-06-15     94.61     95.46    93.50      94.29     7675449
2016-06-16     94.18     95.56    93.25      95.44     7550774
2016-06-17     95.75     95.77    94.20      94.45     8046841
2016-06-20     95.22     95.88    93.65      93.80     9026376
2016-06-21     93.87     93.88    90.77      90.99    15199727
2016-06-22     90.78     91.98    89.77      90.01     9454685
In [8]:
#7. merge data we want into new object, cast as data.frame 
stocks = as.data.frame(merge(fb$FB.Close, amzn$AMZN.Close,nflx$NFLX.Close, goog$GOOG.Close))
In [9]:
#8. see what we have
head(stocks)
FB.CloseAMZN.CloseNFLX.CloseGOOG.Close
2016-06-15114.60714.2694.29 718.92
2016-06-16114.39717.5195.44 710.36
2016-06-17113.02706.3994.45 691.72
2016-06-20113.37714.0193.80 693.71
2016-06-21114.38715.8290.99 695.94
2016-06-22113.91710.6090.01 697.46
In [10]:
#9. change column names
names(stocks) = c('FB', 'AMZN', 'NFLX', 'GOOG')
In [11]:
#10. store daily returns in new dataframe
# quantmod also has a function periodReturn(stock, period='daily, monthly, etc.') and allReturns() 
stocks_returns = data.frame(diff(as.matrix(log(stocks))))
In [12]:
#11. check transformed data
head(stocks_returns)
FBAMZNNFLXGOOG
2016-06-16-0.001834142 0.004539843 0.012122638-0.011978202
2016-06-17-0.012048868-0.015619392-0.010427184-0.026590634
2016-06-20 0.003092012 0.010729475-0.006905738 0.002872756
2016-06-21 0.008869432 0.002531771-0.030415246 0.003209444
2016-06-22-0.004117576-0.007319055-0.010828835 0.002181715
2016-06-23 0.010218876 0.016026252 0.018165304 0.006303037
In [13]:
#12. get correlations
cor(stocks_returns)
FBAMZNNFLXGOOG
FB1.00000000.60162210.31164200.6701603
AMZN0.60162211.00000000.31536730.6358295
NFLX0.31164200.31536731.00000000.3452923
GOOG0.67016030.63582950.34529231.0000000
In [19]:
#13. plot scatter matrix, top row second col x = fb, second row first col x = amzn, etc
In [14]:
plot(stocks_returns)
In [ ]: