Getting Started

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-09-23       47.6       48.3      47.0        48.0 14181200
## 2 GM     2024-09-24       48.5       48.8      47.6        48.1  9501000
## 3 GM     2024-09-25       46.0       46.4      45.0        45.7 22910600
## 4 GM     2024-09-26       45.7       46.1      45.5        45.8 11892100
## 5 GM     2024-09-27       46.4       47.1      46.0        46.5 13298600
## 6 GM     2024-09-30       44.9       45.9      44.5        44.8 20251000
## # ℹ 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: 210 × 11
##    ticker ref_date   price_open price_high price_low price_close   volume
##  * <chr>  <date>          <dbl>      <dbl>     <dbl>       <dbl>    <dbl>
##  1 GM     2024-07-15       49.5       50        49.2        49.3 11073400
##  2 GM     2024-07-16       49.3       50.0      48.8        49.8 10192500
##  3 GM     2024-07-17       49.4       50.0      49.1        49.9  9158500
##  4 GM     2024-07-18       50         50.5      49.2        49.7 12268000
##  5 GM     2024-07-19       49.5       49.6      48.2        48.3 16931000
##  6 GM     2024-07-22       48.5       49.7      48.3        49.6 17064700
##  7 GM     2024-07-23       49.3       49.5      45.8        46.4 41152100
##  8 GM     2024-07-24       45.3       46.6      45.1        46.5 21134900
##  9 GM     2024-07-25       45.7       45.8      44.1        44.1 27256400
## 10 GM     2024-07-26       44.3       44.5      43.7        44.1 18411100
## # ℹ 200 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-07-15  49.2  102.  253.
## 2 2024-07-16  49.6  103.  257.
## 3 2024-07-17  49.8  104.  248.
## 4 2024-07-18  49.5  103.  249.
## 5 2024-07-19  48.2  103.  239.
## 6 2024-07-22  49.4  104.  252.