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
<- 'FB'
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_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)
<- c('FB', '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('FB', '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: 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>
<- 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 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.