• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Downloading Stock Data in R Using QuantMod

Data Science, Portfolio Management

Portfolio Analysis in R 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

Previous Lesson
Back to Course
Next Lesson

Primary Sidebar

In this Course

Course Home
R Financial Packages for Portfolio Analysis
Downloading Stock Data in R Using QuantMod
Calculating Stock Returns and Portfolio Returns in R
Modern Portfolio Theory
Portfolio Optimisation in R
Return to Portfolio Analysis in R

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us