Getting Started

Marcelo Perlin

2022-08-15

Examples

Here you’ll find a series of example of calls to yf_get(). Most arguments are self-explanatory, but you can find more details at the help files.

The steps of the algorithm are:

  1. check cache files for existing data
  2. if not in cache, fetch stock prices from YF and clean up the raw data
  3. write cache file if not available
  4. calculate all returns
  5. build diagnostics
  6. return the data to the user

Fetching a single stock price

library(yfR)

# set options for algorithm
my_ticker <- 'GM'
first_date <- Sys.Date() - 30
last_date <- Sys.Date()

# fetch data
df_yf <- yf_get(tickers = my_ticker, 
                first_date = first_date,
                last_date = last_date)

# output is a tibble with data
head(df_yf)
## # A tibble: 6 × 11
##   ticker ref_date   price_open price_h…¹ price…² price…³ volume price…⁴ ret_ad…⁵
##   <chr>  <date>          <dbl>     <dbl>   <dbl>   <dbl>  <dbl>   <dbl>    <dbl>
## 1 GM     2022-07-18       33.3      33.6    32.5    32.6 1.27e7    32.6 NA      
## 2 GM     2022-07-19       33.3      34.6    33.3    34.4 1.32e7    34.4  0.0546 
## 3 GM     2022-07-20       34.2      35.0    34.0    34.8 1.01e7    34.8  0.0105 
## 4 GM     2022-07-21       34.8      35.1    34.4    35.1 1.09e7    35.1  0.0106 
## 5 GM     2022-07-22       35.2      35.2    34.3    34.7 1.36e7    34.7 -0.0131 
## 6 GM     2022-07-25       34.9      35.0    34.1    34.5 1.48e7    34.5 -0.00433
## # … with 2 more variables: ret_closing_prices <dbl>,
## #   cumret_adjusted_prices <dbl>, and abbreviated variable names ¹​price_high,
## #   ²​price_low, ³​price_close, ⁴​price_adjusted, ⁵​ret_adjusted_prices
## # ℹ Use `colnames()` to see all variable names

Fetching many stock prices

library(yfR)
library(ggplot2)

my_ticker <- c('TSLA', 'GM', 'MMM')
first_date <- Sys.Date() - 100
last_date <- Sys.Date()

df_yf_multiple <- yf_get(tickers = my_ticker, 
                         first_date = first_date,
                         last_date = last_date)


p <- ggplot(df_yf_multiple, aes(x = ref_date, y = price_adjusted,
                                color = ticker)) + 
  geom_line()

p

Fetching daily/weekly/monthly/yearly price data

library(yfR)
library(ggplot2)
library(dplyr)

my_ticker <- 'GE'
first_date <- '2005-01-01'
last_date <- Sys.Date()

df_dailly <- yf_get(tickers = my_ticker, 
                    first_date, last_date, 
                    freq_data = 'daily') %>%
  mutate(freq = 'daily')

df_weekly <- yf_get(tickers = my_ticker, 
                    first_date, last_date, 
                    freq_data = 'weekly') %>%
  mutate(freq = 'weekly')

df_monthly <- yf_get(tickers = my_ticker, 
                     first_date, last_date, 
                     freq_data = 'monthly') %>%
  mutate(freq = 'monthly')

df_yearly <- yf_get(tickers = my_ticker, 
                    first_date, last_date, 
                    freq_data = 'yearly') %>%
  mutate(freq = 'yearly')

# bind it all together for plotting
df_allfreq <- bind_rows(
  list(df_dailly, df_weekly, df_monthly, df_yearly)
) %>%
  mutate(freq = factor(freq, 
                       levels = c('daily', 
                                  'weekly',
                                  'monthly',
                                  'yearly'))) # make sure the order in plot is right

p <- ggplot(df_allfreq, aes(x = ref_date, y = price_adjusted)) + 
  geom_line() + 
  facet_grid(freq ~ ticker) + 
  theme_minimal() + 
  labs(x = '', y = 'Adjusted Prices')

print(p)

Changing format to wide

library(yfR)
library(ggplot2)

my_ticker <- c('TSLA', 'GM', 'MMM')
first_date <- Sys.Date() - 100
last_date <- Sys.Date()

df_yf_multiple <- yf_get(tickers = my_ticker, 
                         first_date = first_date,
                         last_date = last_date)

print(df_yf_multiple)
## # A tibble: 201 × 11
##    ticker ref_date   price_open price_…¹ price…² price…³ volume price…⁴ ret_ad…⁵
##  * <chr>  <date>          <dbl>    <dbl>   <dbl>   <dbl>  <dbl>   <dbl>    <dbl>
##  1 TSLA   2022-05-09       836.     846.    781.    787. 3.03e7    787. NA      
##  2 TSLA   2022-05-10       819.     825.    774.    800. 2.81e7    800.  1.64e-2
##  3 TSLA   2022-05-11       795      810.    727.    734  3.24e7    734  -8.25e-2
##  4 TSLA   2022-05-12       701      760.    680     728  4.68e7    728  -8.17e-3
##  5 TSLA   2022-05-13       773.     787.    752.    770. 3.07e7    770.  5.71e-2
##  6 TSLA   2022-05-16       767.     770.    719.    724. 2.87e7    724. -5.88e-2
##  7 TSLA   2022-05-17       747.     764.    729.    762. 2.67e7    762.  5.14e-2
##  8 TSLA   2022-05-18       745.     760.    701.    710. 2.93e7    710. -6.80e-2
##  9 TSLA   2022-05-19       707      734     694.    709. 3.01e7    709. -5.49e-4
## 10 TSLA   2022-05-20       714.     722.    633     664. 4.83e7    664. -6.42e-2
## # … with 191 more rows, 2 more variables: ret_closing_prices <dbl>,
## #   cumret_adjusted_prices <dbl>, and abbreviated variable names ¹​price_high,
## #   ²​price_low, ³​price_close, ⁴​price_adjusted, ⁵​ret_adjusted_prices
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
l_wide <- yf_convert_to_wide(df_yf_multiple)

names(l_wide)
## [1] "price_open"             "price_high"             "price_low"             
## [4] "price_close"            "volume"                 "price_adjusted"        
## [7] "ret_adjusted_prices"    "ret_closing_prices"     "cumret_adjusted_prices"
prices_wide <- l_wide$price_adjusted
head(prices_wide)
## # A tibble: 6 × 4
##   ref_date    TSLA    GM   MMM
##   <date>     <dbl> <dbl> <dbl>
## 1 2022-05-09  787.  38.3  151.
## 2 2022-05-10  800.  38.7  148.
## 3 2022-05-11  734   37.3  145.
## 4 2022-05-12  728   35.6  148.
## 5 2022-05-13  770.  38.2  148.
## 6 2022-05-16  724.  37.1  149.