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).

install.packages('quantmod') 
library(quantmod) 

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.

getSymbols("AAPL",
           from = "2016/12/31",
           to = "2018/12/31",
           periodicity = "daily")
## '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.
## 
## [1] "AAPL"

The data will be loaded in the AAPL variable. You can preview the first few rows of the data using the head() function. 

head(AAPL)

##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2017-01-03    115.80    116.33   114.76     116.15    28781900
## 2017-01-04    115.85    116.51   115.75     116.02    21118100
## 2017-01-05    115.92    116.86   115.81     116.61    22193600
## 2017-01-06    116.78    118.16   116.47     117.91    31751900
## 2017-01-09    117.95    119.43   117.94     118.99    33561900
## 2017-01-10    118.77    119.38   118.30     119.11    24462100
##            AAPL.Adjusted
## 2017-01-03      111.2870
## 2017-01-04      111.1624
## 2017-01-05      111.7277
## 2017-01-06      112.9733
## 2017-01-09      114.0081
## 2017-01-10      114.1230

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.

AAPL <- getSymbols("AAPL",
           from = "2016/12/31",
           to = "2018/12/31",
           periodicity = "daily",
           auto.assign = FALSE)
head(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2017-01-03    115.80    116.33   114.76     116.15    28781900
## 2017-01-04    115.85    116.51   115.75     116.02    21118100
## 2017-01-05    115.92    116.86   115.81     116.61    22193600
## 2017-01-06    116.78    118.16   116.47     117.91    31751900
## 2017-01-09    117.95    119.43   117.94     118.99    33561900
## 2017-01-10    118.77    119.38   118.30     119.11    24462100
##            AAPL.Adjusted
## 2017-01-03      111.2870
## 2017-01-04      111.1624
## 2017-01-05      111.7277
## 2017-01-06      112.9733
## 2017-01-09      114.0081
## 2017-01-10      114.1230

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.

myStocks <-lapply(c("AAPL", "GOOG"), function(x) {getSymbols(x, 
                                                 from = "2016/12/31", 
                                                 to = "2018/12/31",
                                                 periodicity = "daily",
                                                 auto.assign=FALSE)} )
names(myStocks) <- c("AAPL", "GOOG")
head(myStocks$AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2017-01-03    115.80    116.33   114.76     116.15    28781900
## 2017-01-04    115.85    116.51   115.75     116.02    21118100
## 2017-01-05    115.92    116.86   115.81     116.61    22193600
## 2017-01-06    116.78    118.16   116.47     117.91    31751900
## 2017-01-09    117.95    119.43   117.94     118.99    33561900
## 2017-01-10    118.77    119.38   118.30     119.11    24462100
##            AAPL.Adjusted
## 2017-01-03      111.2870
## 2017-01-04      111.1624
## 2017-01-05      111.7277
## 2017-01-06      112.9733
## 2017-01-09      114.0081
## 2017-01-10      114.1230
head(myStocks$GOOG)
##            GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume
## 2017-01-03    778.81   789.630  775.800     786.14     1657300
## 2017-01-04    788.36   791.340  783.160     786.90     1073000
## 2017-01-05    786.08   794.480  785.020     794.02     1335200
## 2017-01-06    795.26   807.900  792.204     806.15     1640200
## 2017-01-09    806.40   809.966  802.830     806.65     1274600
## 2017-01-10    807.86   809.130  803.510     804.79     1176800
##            GOOG.Adjusted
## 2017-01-03        786.14
## 2017-01-04        786.90
## 2017-01-05        794.02
## 2017-01-06        806.15
## 2017-01-09        806.65
## 2017-01-10        804.79

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.

adjustedPrices <- lapply(myStocks, Ad)
adjustedPrices <- do.call(merge, adjustedPrices)
head(adjustedPrices)
##            AAPL.Adjusted GOOG.Adjusted
## 2017-01-03      111.2870        786.14
## 2017-01-04      111.1624        786.90
## 2017-01-05      111.7277        794.02
## 2017-01-06      112.9733        806.15
## 2017-01-09      114.0081        806.65
## 2017-01-10      114.1230        804.79

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $29 (Regular $57)
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.