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_high price_low price_close volume
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2024-11-21 55.2 55.8 54.7 55.7 11326700
## 2 GM 2024-11-22 55.7 58.9 55.7 58.5 14591700
## 3 GM 2024-11-25 59.2 61.2 58.9 60.2 25347300
## 4 GM 2024-11-26 57.7 58.3 54.7 54.8 25817300
## 5 GM 2024-11-27 55.2 56.1 55.2 55.5 11386800
## 6 GM 2024-11-29 56.4 57.0 55.6 55.6 8064700
## # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
## # ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>
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
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)
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-09-12 44.7 46.6 44.6 46.1 14599900
## 2 GM 2024-09-13 46.3 47.0 46.2 46.3 8986300
## 3 GM 2024-09-16 46.5 47.7 46.5 46.9 9928000
## 4 GM 2024-09-17 47.4 48.2 47.1 47.5 8941600
## 5 GM 2024-09-18 47.7 49.5 47.7 48.7 14533800
## 6 GM 2024-09-19 49.5 49.9 48.2 48.6 13490200
## 7 GM 2024-09-20 48.2 49 47.9 48.9 18091600
## 8 GM 2024-09-23 47.6 48.3 47.0 48.0 14181200
## 9 GM 2024-09-24 48.5 48.8 47.6 48.1 9501000
## 10 GM 2024-09-25 46.0 46.4 45.0 45.7 22910600
## # ℹ 203 more rows
## # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
## # ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>
## [1] "price_open" "price_high" "price_low"
## [4] "price_close" "volume" "price_adjusted"
## [7] "ret_adjusted_prices" "ret_closing_prices" "cumret_adjusted_prices"
## # A tibble: 6 × 4
## ref_date GM MMM TSLA
## <date> <dbl> <dbl> <dbl>
## 1 2024-09-12 46.0 132. 230.
## 2 2024-09-13 46.2 133. 230.
## 3 2024-09-16 46.8 135. 227.
## 4 2024-09-17 47.4 134. 228.
## 5 2024-09-18 48.6 133. 227.
## 6 2024-09-19 48.5 134. 244.