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 2023-11-06 29.8 29.8 29.0 29.1 12412100
## 2 GM 2023-11-07 28.8 29.0 28.1 28.4 18254100
## 3 GM 2023-11-08 28.3 28.4 27.5 27.6 18282600
## 4 GM 2023-11-09 27.7 27.7 26.6 26.6 16045900
## 5 GM 2023-11-10 26.7 27 26.3 26.9 16946700
## 6 GM 2023-11-13 26.7 27.1 26.6 26.9 16394300
## # ℹ 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 2023-08-28 33.3 33.5 32.9 33.1 7393500
## 2 GM 2023-08-29 33.2 33.5 33.1 33.5 7511900
## 3 GM 2023-08-30 33.5 33.7 33.3 33.4 7856500
## 4 GM 2023-08-31 33.5 34.0 33.3 33.5 10168400
## 5 GM 2023-09-01 33.6 33.8 33.3 33.5 8803600
## 6 GM 2023-09-05 33.4 33.5 33.0 33.3 9169600
## 7 GM 2023-09-06 33.0 33.4 32.5 32.8 11731900
## 8 GM 2023-09-07 32.4 32.6 32.0 32.6 12291800
## 9 GM 2023-09-08 32.6 33.1 32.5 33.0 12121900
## 10 GM 2023-09-11 33.1 33.3 32.5 32.6 11777000
## # ℹ 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 2023-08-28 32.9 103. 239.
## 2 2023-08-29 33.3 104. 257.
## 3 2023-08-30 33.2 103. 257.
## 4 2023-08-31 33.4 105. 258.
## 5 2023-09-01 33.4 105. 245.
## 6 2023-09-05 33.2 105. 256.