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:
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_h…¹ price…² price…³ volume price…⁴ ret_ad…⁵
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2022-12-07 37.7 38.3 37.6 38.0 1.16e7 38.0 NA
## 2 GM 2022-12-08 38.1 38.3 37.6 38.2 8.62e6 38.2 0.00632
## 3 GM 2022-12-09 37.8 38.6 37.8 38.3 8.87e6 38.3 0.00209
## 4 GM 2022-12-12 38.3 38.7 38.0 38.5 1.39e7 38.5 0.00549
## 5 GM 2022-12-13 39.7 40.0 38.5 38.8 1.22e7 38.8 0.00727
## 6 GM 2022-12-14 38.7 39.1 38.0 38.4 1.20e7 38.4 -0.0106
## # … with 2 more variables: ret_closing_prices <dbl>,
## # cumret_adjusted_prices <dbl>, and abbreviated variable names ¹price_high,
## # ²price_low, ³price_close, ⁴price_adjusted, ⁵ret_adjusted_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()
plibrary(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)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_…¹ price…² price…³ volume price…⁴ ret_ad…⁵
## * <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2022-09-28 34.8 35.5 34.4 35.2 1.20e7 35.2 NA
## 2 GM 2022-09-29 34.0 34.4 32.8 33.3 1.48e7 33.2 -0.0565
## 3 GM 2022-09-30 32.9 33.2 32.0 32.1 1.62e7 32.0 -0.0352
## 4 GM 2022-10-03 32.5 33.3 31.6 32.9 1.47e7 32.8 0.0243
## 5 GM 2022-10-04 34.2 35.8 33.9 35.8 1.98e7 35.7 0.0891
## 6 GM 2022-10-05 34.4 35.0 33.8 34.8 1.87e7 34.8 -0.0268
## 7 GM 2022-10-06 34.6 35.2 34.4 34.6 1.14e7 34.6 -0.00603
## 8 GM 2022-10-07 34.1 34.3 33.4 33.6 1.12e7 33.5 -0.0292
## 9 GM 2022-10-10 32.4 32.6 31.1 32.3 2.30e7 32.2 -0.0396
## 10 GM 2022-10-11 32.0 32.9 31.3 32.1 1.68e7 32.0 -0.00619
## # … with 197 more rows, 2 more variables: ret_closing_prices <dbl>,
## # cumret_adjusted_prices <dbl>, and abbreviated variable names ¹price_high,
## # ²price_low, ³price_close, ⁴price_adjusted, ⁵ret_adjusted_prices
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 2022-09-28 35.2 113. 288.
## 2 2022-09-29 33.2 111. 268.
## 3 2022-09-30 32.0 109. 265.
## 4 2022-10-03 32.8 112. 242.
## 5 2022-10-04 35.7 114. 249.
## 6 2022-10-05 34.8 114. 241.