Downloading Stock Data in R Using QuantMod
We will use QuantMod R package to download stock data. This allows for downloading stock data from multiple sources, although Yahoo is the default option.
To start using the Quantmod library, you can install and load it in your R environment using the following commands in R console or R Studio (Preferred).
1install.packages('quantmod')
2library(quantmod)
3
Once the package is installed, you would use the getSymbols function to download the data. Below is an example that gets the daily history of AAPL from 2016/12/31 to 2018/12/31.
1getSymbols("AAPL",
2 from = "2016/12/31",
3 to = "2018/12/31",
4 periodicity = "daily")
5## 'getSymbols' currently uses auto.assign=TRUE by default, but will
6## use auto.assign=FALSE in 0.5-0. You will still be able to use
7## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
8## and getOption("getSymbols.auto.assign") will still be checked for
9## alternate defaults.
10##
11## [1] "AAPL"
12
The data will be loaded in the AAPL variable. You can preview the first few rows of the data using the head()
function.
1head(AAPL)
2
3## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
4## 2017-01-03 115.80 116.33 114.76 116.15 28781900
5## 2017-01-04 115.85 116.51 115.75 116.02 21118100
6## 2017-01-05 115.92 116.86 115.81 116.61 22193600
7## 2017-01-06 116.78 118.16 116.47 117.91 31751900
8## 2017-01-09 117.95 119.43 117.94 118.99 33561900
9## 2017-01-10 118.77 119.38 118.30 119.11 24462100
10## AAPL.Adjusted
11## 2017-01-03 111.2870
12## 2017-01-04 111.1624
13## 2017-01-05 111.7277
14## 2017-01-06 112.9733
15## 2017-01-09 114.0081
16## 2017-01-10 114.1230
17
You might notice that one does not need to save it as a variable. getSymbols, by default, will save the ticker history as a variable with the ticker name. If you want to disable this function, you would set auto.assign to FALSE. The variable is also saved as an xts object, which is useful as it allows for easy time-manipulation.
1AAPL <- getSymbols("AAPL",
2 from = "2016/12/31",
3 to = "2018/12/31",
4 periodicity = "daily",
5 auto.assign = FALSE)
6head(AAPL)
7## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
8## 2017-01-03 115.80 116.33 114.76 116.15 28781900
9## 2017-01-04 115.85 116.51 115.75 116.02 21118100
10## 2017-01-05 115.92 116.86 115.81 116.61 22193600
11## 2017-01-06 116.78 118.16 116.47 117.91 31751900
12## 2017-01-09 117.95 119.43 117.94 118.99 33561900
13## 2017-01-10 118.77 119.38 118.30 119.11 24462100
14## AAPL.Adjusted
15## 2017-01-03 111.2870
16## 2017-01-04 111.1624
17## 2017-01-05 111.7277
18## 2017-01-06 112.9733
19## 2017-01-09 114.0081
20## 2017-01-10 114.1230
21
You may also want to get more than one variable. To do this, you would need to use lapply in combination with getSymbols. Here is how you would get the data for AAPL and GOOG.
1myStocks <-lapply(c("AAPL", "GOOG"), function(x) {getSymbols(x,
2 from = "2016/12/31",
3 to = "2018/12/31",
4 periodicity = "daily",
5 auto.assign=FALSE)} )
6names(myStocks) <- c("AAPL", "GOOG")
7head(myStocks$AAPL)
8## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
9## 2017-01-03 115.80 116.33 114.76 116.15 28781900
10## 2017-01-04 115.85 116.51 115.75 116.02 21118100
11## 2017-01-05 115.92 116.86 115.81 116.61 22193600
12## 2017-01-06 116.78 118.16 116.47 117.91 31751900
13## 2017-01-09 117.95 119.43 117.94 118.99 33561900
14## 2017-01-10 118.77 119.38 118.30 119.11 24462100
15## AAPL.Adjusted
16## 2017-01-03 111.2870
17## 2017-01-04 111.1624
18## 2017-01-05 111.7277
19## 2017-01-06 112.9733
20## 2017-01-09 114.0081
21## 2017-01-10 114.1230
22head(myStocks$GOOG)
23## GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume
24## 2017-01-03 778.81 789.630 775.800 786.14 1657300
25## 2017-01-04 788.36 791.340 783.160 786.90 1073000
26## 2017-01-05 786.08 794.480 785.020 794.02 1335200
27## 2017-01-06 795.26 807.900 792.204 806.15 1640200
28## 2017-01-09 806.40 809.966 802.830 806.65 1274600
29## 2017-01-10 807.86 809.130 803.510 804.79 1176800
30## GOOG.Adjusted
31## 2017-01-03 786.14
32## 2017-01-04 786.90
33## 2017-01-05 794.02
34## 2017-01-06 806.15
35## 2017-01-09 806.65
36## 2017-01-10 804.79
37
Finally, you may only want a particular column of data. As this tutorial will focus on portfolio optimisation, the adjusted price, which takes into account corporate actions will be the focus here. To get an object with only the adjusted values of AAPL and GOOG, you would need to add this code after the above.
1adjustedPrices <- lapply(myStocks, Ad)
2adjustedPrices <- do.call(merge, adjustedPrices)
3head(adjustedPrices)
4## AAPL.Adjusted GOOG.Adjusted
5## 2017-01-03 111.2870 786.14
6## 2017-01-04 111.1624 786.90
7## 2017-01-05 111.7277 794.02
8## 2017-01-06 112.9733 806.15
9## 2017-01-09 114.0081 806.65
10## 2017-01-10 114.1230 804.79
11
Unlock Premium Content
Upgrade your account to access the full article, downloads, and exercises.
You'll get access to:
- Access complete tutorials and examples
- Download source code and resources
- Follow along with practical exercises
- Get in-depth explanations