Skip to contents

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     2023-01-03       34.0      34.3    33.4    33.8 1.18e7    33.8 NA      
## 2 GM     2023-01-04       34.3      35.0    34.1    34.7 1.13e7    34.7  2.57e-2
## 3 GM     2023-01-05       34.2      35.4    34.1    35   1.19e7    35    8.94e-3
## 4 GM     2023-01-06       34.7      36.0    34.5    35.9 9.78e6    35.9  2.60e-2
## 5 GM     2023-01-09       36.5      36.8    35.8    35.9 1.16e7    35.9  2.78e-4
## 6 GM     2023-01-10       36.1      37.1    35.9    37.1 1.03e7    37.1  3.31e-2
## # … 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

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: 198 × 11
##    ticker ref_date   price_open price_…¹ price…² price…³ volume price…⁴ ret_ad…⁵
##  * <chr>  <date>          <dbl>    <dbl>   <dbl>   <dbl>  <dbl>   <dbl>    <dbl>
##  1 GM     2022-10-24       34.9     35.9    34.5    35.7 1.86e7    35.6 NA      
##  2 GM     2022-10-25       36.5     37.5    35.8    37.0 2.55e7    36.9  3.61e-2
##  3 GM     2022-10-26       37.4     38.3    37.2    37.9 1.96e7    37.8  2.30e-2
##  4 GM     2022-10-27       38.0     38.6    37.4    38.2 1.38e7    38.1  7.92e-3
##  5 GM     2022-10-28       38.2     38.9    38.1    38.8 1.05e7    38.8  1.81e-2
##  6 GM     2022-10-31       38.5     39.7    38.4    39.2 1.40e7    39.2  1.03e-2
##  7 GM     2022-11-01       39.9     40.1    38.8    39.3 1.08e7    39.3  2.55e-3
##  8 GM     2022-11-02       39.2     40.1    38.5    38.5 1.33e7    38.4 -2.11e-2
##  9 GM     2022-11-03       37.8     38.7    37.7    38.5 1.21e7    38.4 -2.60e-4
## 10 GM     2022-11-04       39.4     39.7    38.5    39   1.51e7    38.9  1.27e-2
## # … with 188 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
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      GM   MMM  TSLA
##   <date>     <dbl> <dbl> <dbl>
## 1 2022-10-24  35.6  117.  211.
## 2 2022-10-25  36.9  117.  222.
## 3 2022-10-26  37.8  121.  225.
## 4 2022-10-27  38.1  121.  225.
## 5 2022-10-28  38.8  125.  229.
## 6 2022-10-31  39.2  124.  228.