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 2025-03-31 45.7 47.3 45.6 47.0 15878600
## 2 GM 2025-04-01 47.0 47.8 46.5 47.3 14065600
## 3 GM 2025-04-02 46.9 48.3 46.9 48.0 12305600
## 4 GM 2025-04-03 47.0 47.8 45.8 45.9 18871200
## 5 GM 2025-04-04 44.5 44.7 42.7 44.2 26717400
## 6 GM 2025-04-07 41.8 45.3 41.6 43.5 25522100
## # ℹ 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 2025-01-21 51.8 54.1 51.3 53.9 12962100
## 2 GM 2025-01-22 53.3 53.5 52.4 52.8 7778100
## 3 GM 2025-01-23 53.0 54.3 52.7 54.2 7452900
## 4 GM 2025-01-24 54.2 54.6 53.7 53.9 6619600
## 5 GM 2025-01-27 54.3 55.1 54.1 54.9 12075400
## 6 GM 2025-01-28 51.0 51.0 48.7 50.0 33776700
## 7 GM 2025-01-29 50.3 50.6 49.1 49.8 15123000
## 8 GM 2025-01-30 50.5 51.0 47.8 49.5 16443200
## 9 GM 2025-01-31 49.7 51.7 49.2 49.5 20578700
## 10 GM 2025-02-03 46.5 49.2 46.4 47.9 28555900
## # ℹ 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 2025-01-21 53.8 146. 424.
## 2 2025-01-22 52.6 148. 415.
## 3 2025-01-23 54.1 149. 412.
## 4 2025-01-24 53.8 149. 407.
## 5 2025-01-27 54.8 151. 397.
## 6 2025-01-28 49.9 151. 398.