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 <- 'FB'
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 FB 2022-05-31 195. 198. 192. 194. 26131103
## 2 FB 2022-06-01 197. 200. 185 189. 36623495
## 3 FB 2022-06-02 188. 201. 188. 199. 31951582
## 4 FB 2022-06-03 196. 197. 190. 191. 19464993
## 5 FB 2022-06-06 194. 197. 188. 194. 30574242
## 6 FB 2022-06-07 192. 197. 191. 196. 18828687
## # … with 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('FB', '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()
plibrary(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('FB', '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: 204 × 11
## ticker ref_date price_open price_high price_low price_close volume
## * <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2022-03-21 214. 215. 208. 211. 30142338
## 2 FB 2022-03-22 211. 219. 210. 217. 31998799
## 3 FB 2022-03-23 213. 217. 212. 213. 23717321
## 4 FB 2022-03-24 215 221. 215. 220. 31502314
## 5 FB 2022-03-25 221. 226. 219. 222. 40039026
## 6 FB 2022-03-28 222. 224. 220. 224. 26224141
## 7 FB 2022-03-29 226. 231. 225. 230. 31417857
## 8 FB 2022-03-30 229. 231. 227. 228. 25588046
## 9 FB 2022-03-31 228. 228. 222. 222. 24192266
## 10 FB 2022-04-01 225. 227. 223. 225. 19544758
## # … with 194 more rows, and 4 more variables: price_adjusted <dbl>,
## # ret_adjusted_prices <dbl>, ret_closing_prices <dbl>,
## # cumret_adjusted_prices <dbl>
l_wide <- yf_convert_to_wide(df_yf_multiple)
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"
prices_wide <- l_wide$price_adjusted
head(prices_wide)## # A tibble: 6 × 4
## ref_date FB GM MMM
## <date> <dbl> <dbl> <dbl>
## 1 2022-03-21 211. 43.6 147.
## 2 2022-03-22 217. 44.6 148.
## 3 2022-03-23 213. 43.8 146.
## 4 2022-03-24 220. 44.3 147.
## 5 2022-03-25 222. 43.7 149.
## 6 2022-03-28 224. 44.2 148.