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 2025-02-12 46.4 48.0 46.3 47.7 10572400
## 2 GM 2025-02-13 48.1 48.6 47.3 47.9 7748300
## 3 GM 2025-02-14 48.4 48.7 47.9 48.4 5630000
## 4 GM 2025-02-18 48.7 48.7 47.6 48.1 6967300
## 5 GM 2025-02-19 47.7 48.0 47.2 47.8 6836000
## 6 GM 2025-02-20 47.8 47.9 46.8 47.9 5801800
## # ℹ 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: 201 × 11
## ticker ref_date price_open price_high price_low price_close volume
## * <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2024-12-04 53.6 53.8 52.8 53.4 13225700
## 2 GM 2024-12-05 53.6 53.9 53.0 53.4 10017900
## 3 GM 2024-12-06 53.7 53.9 53.0 53.4 8625900
## 4 GM 2024-12-09 54 54.5 52.7 52.7 7959600
## 5 GM 2024-12-10 53.8 53.8 52.3 52.7 7722000
## 6 GM 2024-12-11 53.2 53.5 51.3 52.0 12609100
## 7 GM 2024-12-12 52.4 52.7 51.9 52.3 9454900
## 8 GM 2024-12-13 52.2 52.8 51.9 52.5 8849300
## 9 GM 2024-12-16 52.0 52.6 51.3 52.2 11688400
## 10 GM 2024-12-17 51.6 51.9 51.1 51.2 11201600
## # ℹ 191 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-12-04 53.1 130. 358.
## 2 2024-12-05 53.1 133. 369.
## 3 2024-12-06 53.3 132. 389.
## 4 2024-12-09 52.6 131. 390.
## 5 2024-12-10 52.6 129. 401.
## 6 2024-12-11 51.9 129. 425.