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:
- check cache files for existing data
- if not in cache, fetch stock prices from YF and clean up the raw data
- write cache file if not available
- calculate all returns
- build diagnostics
- 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 2026-05-15 76.6 77.2 74.8 74.9 8477500
## 2 GM 2026-05-18 74.8 75.0 73.1 73.1 5651700
## 3 GM 2026-05-19 72.2 72.9 70.4 72.6 6995600
## 4 GM 2026-05-20 73.4 76.2 73.2 76.1 8069100
## 5 GM 2026-05-21 75.4 77.4 75 77.2 6362600
## 6 GM 2026-05-22 78 79.8 77.7 78.8 6440900
## # ℹ 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: 207 × 11
## ticker ref_date price_open price_high price_low price_close volume
## * <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2026-03-06 74.7 75.2 73.3 75.2 8190700
## 2 GM 2026-03-09 73.4 74.7 71.9 74.7 8829600
## 3 GM 2026-03-10 74.8 77.2 74.8 74.9 8915600
## 4 GM 2026-03-11 75.5 76.6 74.4 74.8 5636100
## 5 GM 2026-03-12 73.2 74.0 73.1 73.4 7341000
## 6 GM 2026-03-13 73.6 73.9 72.1 72.4 7229700
## 7 GM 2026-03-16 73.3 73.8 72.4 72.9 7471500
## 8 GM 2026-03-17 73.6 74.6 73.6 74.0 4689900
## 9 GM 2026-03-18 73.5 74.8 73.4 73.5 6000500
## 10 GM 2026-03-19 72.5 74.4 72.5 73.8 9495200
## # ℹ 197 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 2026-03-06 75.0 153. 397.
## 2 2026-03-09 74.5 151. 399.
## 3 2026-03-10 74.8 154. 399.
## 4 2026-03-11 74.6 154. 408.
## 5 2026-03-12 73.3 148. 395.
## 6 2026-03-13 72.2 150. 391.
