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-07-18 33.3 33.6 32.5 32.6 1.27e7 32.6 NA
## 2 GM 2022-07-19 33.3 34.6 33.3 34.4 1.32e7 34.4 0.0546
## 3 GM 2022-07-20 34.2 35.0 34.0 34.8 1.01e7 34.8 0.0105
## 4 GM 2022-07-21 34.8 35.1 34.4 35.1 1.09e7 35.1 0.0106
## 5 GM 2022-07-22 35.2 35.2 34.3 34.7 1.36e7 34.7 -0.0131
## 6 GM 2022-07-25 34.9 35.0 34.1 34.5 1.48e7 34.5 -0.00433
## # … 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
## # ℹ Use `colnames()` to see all variable names
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: 201 × 11
## ticker ref_date price_open price_…¹ price…² price…³ volume price…⁴ ret_ad…⁵
## * <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA 2022-05-09 836. 846. 781. 787. 3.03e7 787. NA
## 2 TSLA 2022-05-10 819. 825. 774. 800. 2.81e7 800. 1.64e-2
## 3 TSLA 2022-05-11 795 810. 727. 734 3.24e7 734 -8.25e-2
## 4 TSLA 2022-05-12 701 760. 680 728 4.68e7 728 -8.17e-3
## 5 TSLA 2022-05-13 773. 787. 752. 770. 3.07e7 770. 5.71e-2
## 6 TSLA 2022-05-16 767. 770. 719. 724. 2.87e7 724. -5.88e-2
## 7 TSLA 2022-05-17 747. 764. 729. 762. 2.67e7 762. 5.14e-2
## 8 TSLA 2022-05-18 745. 760. 701. 710. 2.93e7 710. -6.80e-2
## 9 TSLA 2022-05-19 707 734 694. 709. 3.01e7 709. -5.49e-4
## 10 TSLA 2022-05-20 714. 722. 633 664. 4.83e7 664. -6.42e-2
## # … with 191 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
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
<- 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 TSLA GM MMM
## <date> <dbl> <dbl> <dbl>
## 1 2022-05-09 787. 38.3 151.
## 2 2022-05-10 800. 38.7 148.
## 3 2022-05-11 734 37.3 145.
## 4 2022-05-12 728 35.6 148.
## 5 2022-05-13 770. 38.2 148.
## 6 2022-05-16 724. 37.1 149.