YFinance: Dowload Financial Data from Yahoo Finance. Part I.

Yahoo Finance is a great source of free market data, including interactive charts of stocks, ETFs and traded funds and related financial information. Daily, weekly and monthly historical data can be downloaded in CSV format and used for market analysis. The CSV files contain columns for the open, high, low, close prices, adjusted close price (useful for plotting long term trends) and trading volume.

In addition to resources accessible via browser, Yahoo Finance provides free public APIs (REST) for accessing most of the market data programmatically. Luckily for Python users, there is a Python package called yfinance that provides a simple way to download historical market data from Yahoo Finance.

Installation of YFinance.

The package can be installed using pip (recommended) or conda. To install yfinance using pip, create and activate a new virtual environment or Conda environment and then run the following command:

pip install yfinance[nospam,repair]

nospam and repair are optional dependencies: nospam enables local caching of downloaded data to prevent spamming Yahoo servers, and repair is used to fix the downloaded market data in case of any errors (missing dividend adjustments, split adjustments, missing data etc.)

To install using conda, create and activate a new Conda environment and run the following command:

conda install yfinance -c conda-forge

The examples in this tutorial can be executed in IPython interactive shell, Jupyter notebook or as a standalone Python script.

Basic Usage. Ticker Module.

The Ticker module allows you to access the market data for a specific stock, ETF or traded fund:

import yfinance as yf

nvda = yf.Ticker("NVDA")

# Get all stock info
info = nvda.info

# Prints nicely formatted dictionary with information about NVDA stock
import pprint
pprint.pprint(info)

The historical data for a specific security can be downloaded using the history() method. The following example downloads the historical data for NVDA stock for the last year with daily intervals as pandas.DataFrame:

hist = nvda.history(period="1y", interval="1d")

The history method is very flexible and supports the following parameters:

  • period (str): data period to download. Valid periods are: 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max. Either use period parameter or start and end.

  • interval (str): data interval. Valid intervals are: 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo. Intraday data cannot extend past 60 days.

  • start (str): the start date (first data point) in the format YYYY-MM-DD. Do not use the parameter if period is specified. Example: "2022-01-01". Default is "1900-01-01".

  • end (str): the end date (last data point) in the format YYYY-MM-DD. Do not use the parameter if period is specified. Example: "2023-12-31". Default is now.

  • prepost (bool): include Pre and Post market data in results. Default is False.

  • auto_adjust (bool): adjust all OHLC automatically. Default is True.

  • back_adjust (bool): back-adjusted data to mimic true historical prices. Default is False.

  • repair (bool): repair the broken data. Default is False.

  • keepna (bool): keep NaN rows in the data returned by Yahoo. Default is False.

  • proxy (str): optional. Proxy server URL scheme. Default is None.

  • rounding (bool): round values to 2 decimal places. Default is False, the precision is defined by Yahoo.

  • timeout (None or float): number of seconds to wait for the response. Default is 10 seconds.

  • raise_errors (bool): raise errors if the request response is not ok. Default is True.

The downloaded data the following columns: Open, High, Low, Close, Volume, Dividends, Stock Splits. The Dividends contains dividend and Stock Splits contains stock split events. The downloaded stock data can be plotted using any charting or spreadsheet software. The following example shows how to create a candlestick chart of NVDA stock using the open-source library mplfinance. The downloaded daily data cover period of one year, but only the data starting from Oct. 1, 2023 to the end of downloaded period are selected for plotting. The plot contains 5, 13 and 19 day moving averages and volume data:

import mplfinance as mpf

mpf.plot(hist["2023-10-01":], type="candle", mav=(5,13,19), style="yahoo", volume=True, title="\n\n\n\nNVDA")
Daily stock chart of NVIDIA Corp. (NVDA) generated using ‘mplfinance’ Python package.