Introduction to Yahoo Finance in R

kcin999

April 1, 2023


What is Yahoo Finance?

Yahoo Finance is a powerful suite of information supported by Yahoo, which provides information such as:

  • Financial News
  • Stock and Market Data (Stock Quotes, Financial Reports, etc.)

In addition to providing this information within an easy to use website, there is also an extensive API framework developed for the website in order to easily facilitate information being downloaded for analysis.

Why would we want to use Yahoo Finance?

As I mentioned above, Yahoo Finance provides a powerful suite of financial information related to markets. Being able to have access to this data for detailed analysis has the potential to unlock untold gains, as well as to facilitate market predictions. While this isn’t a perfect science by any means, having access to this information can help create better informed decisions and facilitate decisions with live, up-to-date data, rather than data from days or even weeks ago.

How can we use Yahoo Finance in R?

Luckily for us, there are already packages that we can leverage in order to get this data.

In Python, there is yfinance, an extensive package for interacting with the data.

In R, there is quantmod, which also retrieves the information needed.It also provides a couple of charts and details that we can use in order to further help our analysis.

Quantmod does not require any credentials or authorization, so we are able to hop right into getting our data.

How to set up Quantmod:

  1. Install the quantmod packages with the following command:
    • install.packages("quantmod")
  2. In your R file, load quantmod using:
    • library(quantmod)

That is it! Two easy steps, and we are ready to begin collecting data.

How to use quantmod:

Suppose we want to collect information about Apple. We only need to know their ticker, which is ‘AAPL’ on the ‘NASDAQ’.

From there, we can combine that information with the getSymbols function:

apple_df <- getSymbols('AAPL', src='yahoo', auto.assign=FALSE)

A couple of notes about the parameters:

  • The auto.assign=FALSE parameter allows us to control the name of the dataframe and not have automatically put into memory
  • The src='yahoo' parameter allows us to specific the source of the stock information that we would like to use.
The data stored within our apple_df will look like this:
Date AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2023-03-27 159.94 160.77 157.87 158.28 52390300 158.28
2023-03-28 157.97 158.49 155.98 157.65 45992200 157.65
2023-03-29 159.37 161.05 159.35 160.77 51305700 160.77
2023-03-30 161.53 162.47 161.27 162.36 49501700 162.36
2023-03-31 162.44 165.00 161.91 164.90 68694700 164.90

Now that we have this data, we are able to use some other built in functions, such as chartSeries in order to generate some visualizations.

This following code generates the trend of AAPL’s stock price and volume over the last 6 months.

chartSeries(apple_df, name="AAPL", subset="last 6 months", theme=chartTheme("white"))