Getting data from Yahoo Finance
One of the most popular sources of free financial data is Yahoo Finance. It contains not only historical and current stock prices in different frequencies (daily, weekly, and monthly), but also calculated metrics, such as the beta (a measure of the volatility of an individual asset in comparison to the volatility of the entire market), fundamentals, earnings information/calendars, and many more.
For a long period of time, the go-to tool for downloading data from Yahoo Finance was the pandas-datareader
library. The goal of the library was to extract data from a variety of sources and store it in the form of a pandas
DataFrame. However, after some changes to the Yahoo Finance API, this functionality was deprecated. It is definitely good to be familiar with this library, as it facilitates downloading data from sources such as FRED (Federal Reserve Economic Data), the Fama/French Data Library, or the World Bank. Those might come in handy for different kinds of analyses and some of them are presented in the following chapters.
As of now, the easiest and fastest way of downloading historical stock prices is to use the yfinance
library (formerly known as fix_yahoo_finance
).
For the sake of this recipe, we are interested in downloading Apple’s stock prices from the years 2011 to 2021.
How to do it…
Execute the following steps to download data from Yahoo Finance:
- Import the libraries:
import pandas as pd import yfinance as yf
- Download the data:
df = yf.download("AAPL", start="2011-01-01", end="2021-12-31", progress=False)
- Inspect the downloaded data:
print(f"Downloaded {len(df)} rows of data.") df
Running the code generates the following preview of the DataFrame:
Figure 1.1: Preview of the DataFrame with downloaded stock prices
The result of the request is a pandas
DataFrame (2,769 rows) containing daily Open, High, Low, and Close (OHLC) prices, as well as the adjusted close price and volume.
Yahoo Finance automatically adjusts the close price for stock splits, that is, when a company divides the existing shares of its stock into multiple new shares, most frequently to boost the stock’s liquidity. The adjusted close price takes into account not only splits but also dividends.
How it works…
The download
function is very intuitive. In the most basic case, we just need to provide the ticker (symbol), and it will try to download all available data since 1950.
In the preceding example, we downloaded daily data from a specific range (2011 to 2021).
Some additional features of the download
function are:
- We can download information for multiple tickers at once by providing a list of tickers (
["AAPL", "MSFT"]
) or multiple tickers as a string ("AAPL MSFT"
). - We can set
auto_adjust=True
to download only the adjusted prices. - We can additionally download dividends and stock splits by setting
actions='inline'
. Those actions can also be used to manually adjust the prices or for other analyses. - Specifying
progress=False
disables the progress bar. - The
interval
argument can be used to download data in different frequencies. We could also download intraday data as long as the requested period is shorter than 60 days.
There’s more…
yfinance
also offers an alternative way of downloading the data—via the Ticker
class. First, we need to instantiate the object of the class:
aapl_data = yf.Ticker("AAPL")
To download the historical price data, we can use the history
method:
aapl_data.history()
By default, the method downloads the last month of data. We can use the same arguments as in the download
function to specify the range and frequency.
The main benefit of using the Ticker
class is that we can download much more information than just the prices. Some of the available methods include:
info
—outputs a JSON object containing detailed information about the stock and its company, for example, the company’s full name, a short business summary, which exchange it is listed on, as well as a selection of financial metrics such as the beta coefficientactions
—outputs corporate actions such as dividends and splitsmajor_holders
—presents the names of the major holdersinstitutional_holders
—shows the institutional holderscalendar
—shows the incoming events, such as the quarterly earningsearnings
/quarterly_earnings
—shows the earnings information from the last few years/quartersfinancials
/quarterly_financials
—contains financial information such as income before tax, net income, gross profit, EBIT, and much more
Please see the corresponding Jupyter notebook for more examples and outputs of those methods.
See also
For a complete list of downloadable data, please refer to the GitHub repo of yfinance
(https://github.com/ranaroussi/yfinance).
You can check out some alternative libraries for downloading data from Yahoo Finance:
yahoofinancials
—similarly toyfinance
, this library offers the possibility of downloading a wide range of data from Yahoo Finance. The biggest difference is that all the downloaded data is returned as JSON.yahoo_earnings_calendar
—a small library dedicated to downloading the earnings calendar.