21 Stock data with tidyquant

This chapter originated as a community contribution created by naotominakawa

This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider contributing to our repo.

21.1 Overview

This section covers how to use the tidyquant package to conduct timeseries analysis.

21.2 What is tidyquant?

tidyquant is an one-stop shop for financial analysis. It is suitable for analyzing timeseries data, such as financial and economic data. tidyquant connects to various data sources such as Yahoo! Finance, Morning Star, Bloomberg market data, etc. It also behaves well with other Tidyverse packages.

21.3 Installing tidyquant

You can install tidyquant from CRAN:

install.packages("tidyquant")

If you want to see which functions are available, you can run the following:

# to see which functions are available (not run)
library(tidyquant)
tq_transmute_fun_options()

21.4 Single timeseries

Obtain historical data for single stock (for example, Google):

# get historical data for single stock. e.g. google
library(tidyquant)
tq_get("GOOGL", get="stock.prices")
## # A tibble: 2,690 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 GOOGL  2011-01-03  299.  303.  299.  302. 4725670     302.
##  2 GOOGL  2011-01-04  303.  303.  300.  301. 3645351     301.
##  3 GOOGL  2011-01-05  300.  305.  300.  305. 5059535     305.
##  4 GOOGL  2011-01-06  306.  310.  305.  307. 4111484     307.
##  5 GOOGL  2011-01-07  308.  309.  305.  309. 4198198     309.
##  6 GOOGL  2011-01-10  308.  308.  305.  307. 3155242     307.
##  7 GOOGL  2011-01-11  309.  310.  308.  308. 2875721     308.
##  8 GOOGL  2011-01-12  310.  310.  308.  309. 3262135     309.
##  9 GOOGL  2011-01-13  309.  310.  307.  309. 2665332     309.
## 10 GOOGL  2011-01-14  309.  312.  309.  312. 4726469     312.
## # … with 2,680 more rows

Calculate monthly return of single stock:

library(dplyr)
# calculate monthly return of single stock
tq_get(c("GOOGL"), get="stock.prices") %>%
  tq_transmute(select=adjusted,
               mutate_fun=periodReturn,
               period="monthly",
               col_rename = "monthly_return")
## # A tibble: 129 × 2
##    date       monthly_return
##    <date>              <dbl>
##  1 2011-01-31       -0.00660
##  2 2011-02-28        0.0217 
##  3 2011-03-31       -0.0434 
##  4 2011-04-29       -0.0727 
##  5 2011-05-31       -0.0277 
##  6 2011-06-30       -0.0428 
##  7 2011-07-29        0.192  
##  8 2011-08-31       -0.104  
##  9 2011-09-30       -0.0479 
## 10 2011-10-31        0.151  
## # … with 119 more rows

Create a line chart of the closing price for single stock:

# showing closing price for single stock
library(ggplot2)
tq_get(c("GOOGL"), get="stock.prices") %>%
  ggplot(aes(date, close)) +
  geom_line()

Create a line chart of the monthly return for single stock:

# showing monthly return for single stock
tq_get(c("GOOGL"), get="stock.prices") %>%
  tq_transmute(select=adjusted,
               mutate_fun=periodReturn,
               period="monthly",
               col_rename = "monthly_return") %>%
  ggplot(aes(date, monthly_return)) +
  geom_line()

21.5 Multiple timeseries

Obtain historical data for multiple stocks (for example, GAFA):

# get historical data for multiple stocks. e.g. GAFA
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices")
## # A tibble: 10,413 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 GOOGL  2011-01-03  299.  303.  299.  302. 4725670     302.
##  2 GOOGL  2011-01-04  303.  303.  300.  301. 3645351     301.
##  3 GOOGL  2011-01-05  300.  305.  300.  305. 5059535     305.
##  4 GOOGL  2011-01-06  306.  310.  305.  307. 4111484     307.
##  5 GOOGL  2011-01-07  308.  309.  305.  309. 4198198     309.
##  6 GOOGL  2011-01-10  308.  308.  305.  307. 3155242     307.
##  7 GOOGL  2011-01-11  309.  310.  308.  308. 2875721     308.
##  8 GOOGL  2011-01-12  310.  310.  308.  309. 3262135     309.
##  9 GOOGL  2011-01-13  309.  310.  307.  309. 2665332     309.
## 10 GOOGL  2011-01-14  309.  312.  309.  312. 4726469     312.
## # … with 10,403 more rows

Create a multiple line chart of the closing prices of multiple stocks (again, GAFA). We can show each stock in a different color on the same graph:

# Create a multiple line chart of the closing prices of the four stocks,
# showing each stock in a different color on the same graph.
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
  ggplot(aes(date, close, color=symbol)) +
  geom_line()

Transform the data so each stock begins at 100 and replot (Standardize the data so that we can compare timeseries):

# Create a multiple line chart of the closing prices of the four stocks,
# showing each stock in a different color on the same graph.
# Transform the data so each stock begins at 100 and replot.
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
  group_by(symbol) %>%
  mutate(close = 100*close/first(close)) %>%
  ggplot(aes(date, close, color=symbol)) +
  geom_line()

Calculate monthly return of multiple stocks (again, GAFA):

# calculate monthly return of multiple stocks
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
  group_by(symbol) %>%
  tq_transmute(select=adjusted,
               mutate_fun=periodReturn,
               period="monthly",
               col_rename = "monthly_return")
## # A tibble: 500 × 3
## # Groups:   symbol [4]
##    symbol date       monthly_return
##    <chr>  <date>              <dbl>
##  1 GOOGL  2011-01-31       -0.00660
##  2 GOOGL  2011-02-28        0.0217 
##  3 GOOGL  2011-03-31       -0.0434 
##  4 GOOGL  2011-04-29       -0.0727 
##  5 GOOGL  2011-05-31       -0.0277 
##  6 GOOGL  2011-06-30       -0.0428 
##  7 GOOGL  2011-07-29        0.192  
##  8 GOOGL  2011-08-31       -0.104  
##  9 GOOGL  2011-09-30       -0.0479 
## 10 GOOGL  2011-10-31        0.151  
## # … with 490 more rows

Create a multiple line chart of monthly return of the four stocks. Again, we can show each stock in a different color on the same graph:

# Create a multiple line chart of monthly return of the four stocks,
# showing each stock in a different color on the same graph
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
  group_by(symbol) %>%
  tq_transmute(select=adjusted,
               mutate_fun=periodReturn,
               period="monthly",
               col_rename = "monthly_return") %>%
  ggplot(aes(date, monthly_return, color=symbol)) +
  geom_line()

21.6 External Resources







with