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_high price_low price_close   volume
##   <chr>  <date>          <dbl>      <dbl>     <dbl>       <dbl>    <dbl>
## 1 GM     2024-04-15       43.7       44.0      42.5        42.7 13499600
## 2 GM     2024-04-16       42.5       42.9      42.1        42.7 12384900
## 3 GM     2024-04-17       42.9       43.2      42.3        42.5 10663300
## 4 GM     2024-04-18       42.8       42.8      42.0        42.4 14180500
## 5 GM     2024-04-19       42.5       43.0      42.2        42.4 15469300
## 6 GM     2024-04-22       42.7       43.6      42.2        43.2 21315100
## # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
## #   ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>

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: 204 × 11
##    ticker ref_date   price_open price_high price_low price_close   volume
##  * <chr>  <date>          <dbl>      <dbl>     <dbl>       <dbl>    <dbl>
##  1 GM     2024-02-05       38.5       38.7      37.7        37.8 19722400
##  2 GM     2024-02-06       37.6       38.7      37.6        38.0 16280200
##  3 GM     2024-02-07       38.5       39.1      38.1        38.7 19093800
##  4 GM     2024-02-08       38.8       38.9      38.4        38.7 15864500
##  5 GM     2024-02-09       38.6       39.0      38.5        38.6 13066000
##  6 GM     2024-02-12       38.5       39.5      38.5        39.3 18113100
##  7 GM     2024-02-13       38.8       38.9      37.8        38.3 18979000
##  8 GM     2024-02-14       38.6       38.8      38.0        38.4 13278500
##  9 GM     2024-02-15       38.7       39.5      38.6        39.0 15688600
## 10 GM     2024-02-16       38.8       39.1      38.6        38.7 12392900
## # ℹ 194 more rows
## # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
## #   ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>
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 2024-02-05  37.7  76.0  181.
## 2 2024-02-06  37.9  76.9  185.
## 3 2024-02-07  38.6  76.9  188.
## 4 2024-02-08  38.5  76.4  190.
## 5 2024-02-09  38.4  76.2  194.
## 6 2024-02-12  39.2  77.6  188.