How To Import Stock Market Price Data into R Programming

The yfR library can import stock data from Yahoo! Finance

Pierre DeBois
5 min readMar 12


Image created on MidJourney by Pierre DeBois

Stock performance is a great way of examining time series data, as well as understanding the mechanics of creating forecasts. The volatility of stock can teach how to examine patterns in a time series analysis as well as build some speculation for those who are trading in stock, casual or otherwise. ;-)

Within R programming, one library offers a simple way to apply these concepts. It is called yfR. yfR is a R programming library designed to extract stock market data from the Yahoo! Finance site.

This library is very unique in that there is no current Google finance counterpart. Google decommissioned the access to its Google Finance API, leaving Yahoo! finance as the main API for downloading stock market data into software applications. Fortunately for financial analysts and savvy stock pickers, this library is very straightforward to use, with seven functions featured in the supporting CRAN documentation.

When using the library, two functions are really the most important to know — — yf_get() and yf_collection_get(). Both are the main functions for downloading stock market data.

yf_get() allows you to enter the ticker symbol, the first date and last date of your stock performance period, as well as the frequency of data. The data frequency can be daily, weekly, monthly, or yearly.

The above image shows the open and closing price of auto manufacturer Ford Motor Company, ticker symbol F. The column ref_date shows the date of the trading day. The dates are linked to the current day via the Sys.Date() function. So the month_prior object that is being created reflects 30 days before the current day ( Sys.Date() — 30 ).

The above image shows the weekly open price for the retailer Target, ticker symbol TGT. You can set the frequency in the freq_data…



Pierre DeBois

#analytics |#datascience |#JS |#rstats |#marketing services for #smallbiz | #retail | #nonprofits Contrib @CMSWire @smallbiztrends #blackbusiness #BLM