• 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, fintech, accounting and more

  • Home
  • Finance Exams
    • CFA Exam
    • CAIA Exam
    • ERP Exam
    • FRM Exam
    • PRM Exam
  • Tutorials
  • Careers
  • Calculators
  • Products

Downloading Stock Data in R Using QuantMod

Data Science, Portfolio Management

This lesson is part 2 of 5 in the course Portfolio Analysis in R

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

1
2
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.

1
2
3
4
5
6
7
8
9
10
11
12
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. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.

1
2
3
4
5
6
7
8
9
10
11
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
 
Series Navigation‹ R Financial Packages for Portfolio AnalysisCalculating Stock Returns and Portfolio Returns in R ›
Join Our Facebook Group - Finance, Risk and Data Science

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • 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

Finance Exam Products

  • CFA Level I Mock Exam
  • CFA Level I Practice Questions
  • CFA Level I Authority
  • PRM Exam I Practice Questions
View All Products

Latest Tutorials

    • Machine Learning in Finance Using Python
    • Portfolio Analysis in R
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Prime Brokerage
    • Financial Reporting Standards
    • Fraud
Facebook Group

Footer

Recent Posts

  • Social media reporting – it is not as difficult as you think
  • Model Selection in Machine Learning
  • Evaluate Model Performance – Loss Function
  • Train-Test Datasets in Machine Learning
  • Feature Selection in Machine Learning

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

Copyright © 2019 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us