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 2024-10-22 50.0 54.2 50.0 53.7 42657700
## 2 GM 2024-10-23 54.0 54.2 52.5 52.9 16109800
## 3 GM 2024-10-24 53.7 54.3 52.5 52.7 11800700
## 4 GM 2024-10-25 53.2 53.4 51.9 52.1 8445100
## 5 GM 2024-10-28 52.2 52.9 52.0 52.7 11504900
## 6 GM 2024-10-29 52.1 52.2 51.0 51.5 13807600
## # ℹ 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: 213 × 11
## ticker ref_date price_open price_high price_low price_close volume
## * <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2024-08-13 43.1 43.3 42.5 43.3 10304300
## 2 GM 2024-08-14 43.6 44.0 43.5 43.6 10772200
## 3 GM 2024-08-15 44.8 45.2 44.6 44.9 12913900
## 4 GM 2024-08-16 44.8 45.4 44.7 45.3 10592600
## 5 GM 2024-08-19 45.5 45.9 45.2 45.8 9598100
## 6 GM 2024-08-20 45.7 46.5 45.7 46.0 9188700
## 7 GM 2024-08-21 46.2 46.9 46.2 46.6 11897000
## 8 GM 2024-08-22 46.7 47.0 46.4 46.5 6629400
## 9 GM 2024-08-23 47.3 48.6 47.2 48.6 12918100
## 10 GM 2024-08-26 48.9 49.2 48.6 48.8 8800700
## # ℹ 203 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-08-13 43.2 124. 208.
## 2 2024-08-14 43.5 125. 201.
## 3 2024-08-15 44.7 126. 214.
## 4 2024-08-16 45.2 126. 216.
## 5 2024-08-19 45.6 126. 223.
## 6 2024-08-20 45.9 126. 221.