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
<- 'GM'
my_ticker <- Sys.Date() - 30
first_date <- Sys.Date()
last_date
# fetch data
<- yf_get(tickers = my_ticker,
df_yf 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-10-19 33.8 34.2 33.3 33.6 1.03e7 33.6 NA
## 2 GM 2022-10-20 33.7 34.2 33.1 33.4 1.36e7 33.4 -0.00565
## 3 GM 2022-10-21 33.5 35.0 33.3 35 1.41e7 35 0.0467
## 4 GM 2022-10-24 34.9 35.9 34.5 35.7 1.86e7 35.7 0.0206
## 5 GM 2022-10-25 36.5 37.5 35.8 37.0 2.55e7 37.0 0.0361
## 6 GM 2022-10-26 37.4 38.3 37.2 37.9 1.96e7 37.9 0.0230
## # … 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)
<- c('TSLA', 'GM', 'MMM')
my_ticker <- Sys.Date() - 100
first_date <- Sys.Date()
last_date
<- yf_get(tickers = my_ticker,
df_yf_multiple first_date = first_date,
last_date = last_date)
<- ggplot(df_yf_multiple, aes(x = ref_date, y = price_adjusted,
p color = ticker)) +
geom_line()
p
library(yfR)
library(ggplot2)
library(dplyr)
<- 'GE'
my_ticker <- '2005-01-01'
first_date <- Sys.Date()
last_date
<- yf_get(tickers = my_ticker,
df_dailly
first_date, last_date, freq_data = 'daily') %>%
mutate(freq = 'daily')
<- yf_get(tickers = my_ticker,
df_weekly
first_date, last_date, freq_data = 'weekly') %>%
mutate(freq = 'weekly')
<- yf_get(tickers = my_ticker,
df_monthly
first_date, last_date, freq_data = 'monthly') %>%
mutate(freq = 'monthly')
<- yf_get(tickers = my_ticker,
df_yearly
first_date, last_date, freq_data = 'yearly') %>%
mutate(freq = 'yearly')
# bind it all together for plotting
<- bind_rows(
df_allfreq 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
<- ggplot(df_allfreq, aes(x = ref_date, y = price_adjusted)) +
p geom_line() +
facet_grid(freq ~ ticker) +
theme_minimal() +
labs(x = '', y = 'Adjusted Prices')
print(p)
library(yfR)
library(ggplot2)
<- c('TSLA', 'GM', 'MMM')
my_ticker <- Sys.Date() - 100
first_date <- Sys.Date()
last_date
<- yf_get(tickers = my_ticker,
df_yf_multiple first_date = first_date,
last_date = last_date)
print(df_yf_multiple)
## # A tibble: 213 × 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-08-10 37.7 38.5 37.5 38.0 1.07e7 37.9 NA
## 2 GM 2022-08-11 38.7 39.0 38.2 38.5 1.02e7 38.4 1.34e-2
## 3 GM 2022-08-12 38.8 39.5 38.5 39.5 8.91e6 39.4 2.65e-2
## 4 GM 2022-08-15 38.9 39.7 38.8 39.4 9.63e6 39.3 -2.03e-3
## 5 GM 2022-08-16 39.0 39.6 38.7 39.0 1.39e7 38.9 -1.04e-2
## 6 GM 2022-08-17 38.2 38.7 37.7 38.4 1.07e7 38.3 -1.51e-2
## 7 GM 2022-08-18 38.3 38.9 38.1 38.7 7.00e6 38.6 8.33e-3
## 8 GM 2022-08-19 39.2 40.3 39.1 39.7 2.33e7 39.6 2.53e-2
## 9 GM 2022-08-22 38.2 38.8 37.9 38.5 2.09e7 38.5 -2.90e-2
## 10 GM 2022-08-23 39.0 39.4 38.4 38.6 1.54e7 38.5 2.59e-4
## # … with 203 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
<- yf_convert_to_wide(df_yf_multiple)
l_wide
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"
<- l_wide$price_adjusted
prices_wide head(prices_wide)
## # A tibble: 6 × 4
## ref_date GM MMM TSLA
## <date> <dbl> <dbl> <dbl>
## 1 2022-08-10 37.9 146. 294.
## 2 2022-08-11 38.4 146. 287.
## 3 2022-08-12 39.4 149. 300.
## 4 2022-08-15 39.3 148. 309.
## 5 2022-08-16 38.9 147. 307.
## 6 2022-08-17 38.3 144. 304.