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-04-26 32.9 33.1 32.1 32.2 18780900
## 2 GM 2023-04-27 32.4 32.8 31.8 32.7 19730100
## 3 GM 2023-04-28 32.4 33.3 32.4 33.0 15510700
## 4 GM 2023-05-01 34 34.3 33.4 33.5 16892300
## 5 GM 2023-05-02 33.2 33.4 32.5 33.1 14527600
## 6 GM 2023-05-03 32.8 33.4 32.4 32.5 13494400
## # ℹ 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 2023-02-15 42.1 43.0 42.0 43.0 11660100
## 2 GM 2023-02-16 42.4 43.6 42.2 43.1 14549600
## 3 GM 2023-02-17 43 43.2 42.2 43.2 13034100
## 4 GM 2023-02-21 42.4 43.1 41.1 41.1 12320100
## 5 GM 2023-02-22 41.2 41.5 40.6 40.9 9870000
## 6 GM 2023-02-23 41.0 41.3 38.9 39.2 19930700
## 7 GM 2023-02-24 38.9 39.3 38.5 39.2 12929600
## 8 GM 2023-02-27 39.5 40.0 39.1 39.3 17310600
## 9 GM 2023-02-28 39.2 39.2 38.5 38.7 17166700
## 10 GM 2023-03-01 38.7 39.3 38.6 38.7 14164800
## # ℹ 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 2023-02-15 42.9 112. 214.
## 2 2023-02-16 43.0 110. 202.
## 3 2023-02-17 43.1 111. 208.
## 4 2023-02-21 41.0 108. 197.
## 5 2023-02-22 40.8 107. 201.
## 6 2023-02-23 39.2 107. 202.