yfR facilitates importing stock prices from Yahoofinance, organizing the data in thetidy format andspeeding up the process using a cache system and parallel computing.yfR is the second and backwards-incompatible version ofBatchGetSymbols,released in 2016 (see vignetteyfRand BatchGetSymbols for details).
In a nutshell,Yahoo Finance(YF) provides a vast repository of stock price data around theglobe. It covers a significant number of markets and assets, being usedextensively in academic research and teaching. In order to import thefinancial data from YF, all you need is a ticker (id of a stock,e.g. “GM” forGeneralMotors) and a time period – first and last date.
The main function of the package,yfR::yf_get, returns adataframe with the financial data. All price data is measured at theunit of the financial exchange. For example, price data for GM(NASDAQ/US) is measured in dollars, while price data for PETR3.SA(B3/BR) is measured in Reais (Brazilian currency).
The returned data contains the following columns:
ticker: The requested tickers (ids of stocks);
ref_date: The reference day (this can also beyear/month/week when using argument freq_data);
price_open: The opening price of the day/period;
price_high: The highest price of the day/period;
price_close: The close/last price of theday/period;
volume: The financial volume of the day/period, inthe unit of the exchange;
price_adjusted: The stock price adjusted forcorporate events such as splits, dividends and others – this is usuallywhat you want/need for studying stocks as it represents the realfinancial performance of stockholders;
ret_adjusted_prices: The arithmetic or log return(see input type_return) for the adjusted stock prices;
ret_adjusted_prices: The arithmetic or log return(see input type_return) for the closing stock prices;
cumret_adjusted_prices: The accumulatedarithmetic/log return for the period (starts at 100%).
The easiest way to find the tickers of a company stock is to searchfor it inYahoo Finance’swebsite. At the top page you’ll find a search bar:

A company can have many different stocks traded at different markets(see picture above). As the example shows, Petrobras is traded at NYQ(New York Exchange), SAO (Sao Paulo/Brazil - B3 exchange) and BUE(Buenos Aires/Argentina Exchange), all with different symbols (tickers).For market indices, a list of tickers is availablehere.
yfRFetches daily/weekly/monthly/annual stock prices/returns fromyahoo finance and outputs a dataframe (tibble) in the long format(stacked data);
A new feature calledcollections facilitatesdownload of multiple tickers from a particular market/index. You can,for example, download data for all stocks in the SP500 index with asimple call toyf_collection_get("SP500");
A session-persistent smart cache system is available by default.This means that the data is saved locally and only missing portions aredownloaded, if needed.
All dates are compared to a benchmark ticker such as SP500 and,whenever an individual asset does not have a sufficient number of dates,the software drops it from the output. This means you can choose toignore tickers with a high proportion of missing dates.
A customized function calledyf_convert_to_wide()can transform the long dataframe into a wide format (tickers ascolumns), much used in portfolio optimization. The output is a listwhere each element is a different target variable (prices, returns,volumes).
Parallel computing with packagefurrr is available,speeding up the data importation process.
# CRAN (stable)install.packages('yfR')# Github (dev version)devtools::install_github('ropensci/yfR')# ropensciinstall.packages("yfR", repos = "https://ropensci.r-universe.dev")library(yfR)# set options for algorithmmy_ticker<-'META'first_date<-Sys.Date()-30last_date<-Sys.Date()# fetch datadf_yf<-yf_get(tickers = my_ticker,first_date = first_date,last_date = last_date)#>#> ── Running yfR for 1 stocks | 2025-04-19 --> 2025-05-19 (30 days) ──#>#> ℹ Downloading data for benchmark ticker ^GSPC#> ℹ (1/1) Fetching data for META#> ! - not cached#> ✔ - cache saved successfully#> ✔ - got 20 valid rows (2025-04-21 --> 2025-05-16)#> ✔ - got 100% of valid prices -- Time for some tea?#> ℹ Binding price data#>#> ── Diagnostics ─────────────────────────────────────────────────────────────────#> ✔ Returned dataframe with 20 rows -- All OK!#> ℹ Using 5.9 kB at /tmp/Rtmpcyyijl/yf_cache for 2 cache files#> ℹ Out of 1 requested tickers, you got 1 (100%)# output is a tibble with datahead(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 META 2025-04-21 491. 494. 480. 485. 16166000#> 2 META 2025-04-22 492. 507. 486. 500. 17399400#> 3 META 2025-04-23 529. 535. 517. 520. 18173900#> 4 META 2025-04-24 519. 534. 518. 533. 13910000#> 5 META 2025-04-25 547. 550. 536. 547. 17098900#> 6 META 2025-04-28 556. 558. 541. 550. 15179000#> # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,#> # ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>PackageyfR is based onquantmod (quantmod::getSymbols) for fetching raw data fromYahoo Finance. As with any API, there is significant work in maintainingthe code. Joshua was always fast and openminded in implemented requiredchanges, and I’m very grateful for it.