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_high price…¹ price…² volume price…³ ret_a…⁴
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2022-07-28 35.0 35.8 34.6 35.7 1.18e7 35.7 NA
## 2 GM 2022-07-29 35.8 36.4 35.4 36.3 1.44e7 36.3 0.0145
## 3 GM 2022-08-01 36.1 37.0 35.6 36.8 1.22e7 36.8 0.0141
## 4 GM 2022-08-02 36.3 37.0 36.1 36.1 1.31e7 36.1 -0.0174
## 5 GM 2022-08-03 36.8 38.2 36.8 37.3 1.60e7 37.3 0.0327
## 6 GM 2022-08-04 37.0 37.2 36.1 36.2 1.69e7 36.2 -0.0289
## # … with 2 more variables: ret_closing_prices <dbl>,
## # cumret_adjusted_prices <dbl>, and abbreviated variable names ¹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: 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-05-19 35.4 36.7 35.2 36.1 1.55e7 36.1 NA
## 2 GM 2022-05-20 37.0 37.1 34.4 35.4 2.44e7 35.4 -0.0199
## 3 GM 2022-05-23 35.9 36.6 35.1 36 2.00e7 36 0.0169
## 4 GM 2022-05-24 35.5 35.6 34.3 35.2 1.67e7 35.2 -0.0219
## 5 GM 2022-05-25 34.9 36.3 34.9 36.0 1.54e7 36.0 0.0219
## 6 GM 2022-05-26 36.4 37.8 36.4 37.4 1.46e7 37.4 0.0400
## 7 GM 2022-05-27 37.8 38.6 37.4 38.6 1.57e7 38.6 0.0307
## 8 GM 2022-05-31 38.6 39.0 38.0 38.7 1.99e7 38.7 0.00285
## 9 GM 2022-06-01 39.0 39.6 37.9 38.3 1.22e7 38.3 -0.0106
## 10 GM 2022-06-02 38.4 39.2 38.4 38.9 1.07e7 38.9 0.0157
## # … 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
<- 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-05-19 36.1 145. 236.
## 2 2022-05-20 35.4 142. 221.
## 3 2022-05-23 36 143. 225.
## 4 2022-05-24 35.2 144. 209.
## 5 2022-05-25 36.0 144. 220.
## 6 2022-05-26 37.4 146. 236.