The yfR Package

What is yfR?

If you are looking for data sources for your projects, I am sure that using financial data has come to your mind at some point. We are going to see how to make available to in R historical stock data.

yfR is a new package available in R that will simplify the financial data collection from Yahoo Finance.

You can find the source code at:

Previous work - BatchGetSymbols

Previously there author of this function created the package BatchGetSymbols, but now it has been improved

Using the yfR Package

How to install yfR in RStudio

You can install yfR in R with any of the following commands:

# CRAN (stable)

# Github (dev version)

# ropensci
install.packages("yfR", repos = "")

Using yfR in R together with plotly

You can use yfR to query historical stock data in R the following way:

hist_price <- yf_get(tickers = test_ticker,
                        first_date= initial_date,
                        last_date= ending_date,
                        thresh_bad_data = 0.2,
                        freq_data="monthly") %>% select(ticker, price_close, ref_date) 
hist_price$ym <- format(as.Date(hist_price$ref_date), "%Y-%m")
hist_price$Year <- as.integer(format(as.Date(hist_price$ref_date), "%Y"))

Then, just get some help from plotly to create one interactive visualization:

       plot_ly(hist_price, type = 'scatter', mode = 'lines') %>%
                          add_trace(x = ~ym, y = ~price_close, name = ~ticker)


Project Example Using yfR