YAHOO_FIN
YAHOO_FIN DOCUMENTATION
What is yahoo_fin?
Yahoo_fin is a Python 3 package designed to scrape historical stock price data, as well as to provide current information on market caps, dividend yields, and which stocks comprise the major exchanges. Additional functionality includes scraping income statements, balance sheets, cash flows, holder information, and analyst data. The package includes the ability to scrape live (real-time) stock prices, capture cryptocurrency data, and get the most actively traded stocks on a current trading day. Yahoo_fin also contains a module for retrieving option prices and expiration dates.
If you like yahoo_fin and / or this blog, please click here to follow us on Twitter.
Table of Contents
For navigating yahoo_fin’s documentation, click on any of the links below.
List of package methods (with direct links)
stock_info module method descriptions
options module method descriptions
To see in-depth examples, check out the following posts:
How to get stock fundamentals data with Python
How to download real-time stock prices with Python
How to download options data with Python
Updates
Update: July 11, 2020
The latest update to yahoo_fin occurred on July 11, 2020. This newest version is 0.8.6 and includes several changes.
By popular demand, quarterly data (in addition to previously available yearly data) can now be downloaded for balance sheets, cash flow statements, and income statements!
Earnings information can be pulled with the get_earnings function.
A bug affecting ticker extraction was fixed (thanks to Alvaro Ritter!)
The code extracting fundamentals data was rewritten to have greater stability.
The get_financials function was added, which allows you to more efficiently extract balance sheets, cash flows, and income statements for the same ticker all at once
The get_dividends and get_splits functions were added to easily download historical dividends and splits information. Thank you to Daniel Catlin for this contribution!
Update: April 24, 2020
This update to yahoo_fin occurred on April 24, 2020 (version 0.8.5). This version updated the get_stats function, as well as added the get_stats_valuation function. Follow the guidance in the installation section below to upgrade yahoo_fin to the latest version.
Update: December 15, 2019
An update to this package was pushed on December 15, 2019. This update fixes the issues caused by a recent change in Yahoo Finance’s website. If you have a previously installed version of yahoo_fin, please follow the guidance below to upgrade your installation using pip.
Recommended Python Version
A few methods in yahoo_fin require a package called requests_html as a dependency. Since requests_html requires Python 3.6+, it’s recommended to use 3.6+ when installing yahoo_fin. If you’re using an older version of Python, the package is designed so that you should still be able to use the other functionality that does not depend upon requests_html, such as the get_data and get_live_price functions, among others.
The methods that depend on requests_html (and thus require Python 3.6+) are the following (broken out by the two modules in yahoo_fin):
Installation
Yahoo_fin can be installed using pip:pip install yahoo_fin
If you have a previously installed version, you can upgrade like this:pip install yahoo_fin --upgrade
Requirements
Yahoo_fin requires the following packages to be installed:
With the exception of requests_html, these dependencies come pre-installed with Anaconda. requests_html requires Python 3.6+ and is needed for several of the functions in yahoo_fin, as described above. To install requests_html, you can use pip:pip install requests_html
Methods
The yahoo_fin package has two modules. These are called stock_info and options. stock_info has the below primary methods.
The methods for options are listed below:
stock_info module
Any method from yahoo_fin’s stock_info module can be imported by running the follow line, with get_analysts_info replaced with the method of choice.
Alternatively, all methods can be imported at once like so:
get_analysts_info(ticker)
Scrapes data from the Analysts page for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/analysts?p=NFLX. This includes information on earnings estimates, EPS trends / revisions etc.
Returns a dictionary containing the tables visible on the ‘Analysts’ page.
Possible parameters
get_balance_sheet(ticker, yearly = True)
Scrapes the balance sheet for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/balance-sheet?p=NFLX.
Possible parameters
get_cash_flow(ticker, yearly = True)
Scrapes the cash flow statement for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/cash-flow?p=NFLX.
Possible parameters
get_data(ticker, start_date = None, end_date = None, index_as_date = True, interval = “1d”)
Downloads historical price data of a stock into a pandas data frame. Offers the functionality to pull daily, weekly, or monthly data.
Possible parameters
If you want to filter by a date range, you can just add a value for the start_date and / or end_date parameters, like below:
Get weekly or monthly historical price data:
get_day_gainers()
Scrapes the top 100 (at most) stocks with the largest gains (on the given trading day) from Yahoo Finance (see https://finance.yahoo.com/gainers).
get_day_losers()
Scrapes the top 100 (at most) worst performing stocks (on the given trading day) from Yahoo Finance (see https://finance.yahoo.com/losers).
get_day_most_active()
Scrapes the top 100 most active stocks (on the given trading day) from Yahoo Finance (see https://finance.yahoo.com/most-active).
get_dividends(ticker, start_date = None, end_date = None, index_as_date = True)
Downloads historical dividend data of a stock into a pandas data frame.
Possible parameters
get_earnings(ticker)
Scrapes earnings information from Yahoo Finance’s financials page for a given ticker (see https://finance.yahoo.com/quote/NFLX/financials?p=NFLX). Returns a dictionary with quarterly actual vs. estimated earnings per share, quarterly revenue / earnings data, and yearly revenue / earnings data.
Possible parameters
get_financials(ticker, yearly = True, quarterly = True)
Efficient method to scrape balance sheets, cash flow statements, and income statements in a single call from Yahoo Finance’s financials page for a given ticker (see https://finance.yahoo.com/quote/NFLX/financials?p=NFLX).
If you’re looking to get all of this information for a given ticker, or set of tickers, this function will be 3x faster than running get_balance_sheet, get_cash_flow, and get_income_statement separately. Yearly, quarterly, or both time-periods can be pulled.
Returns a dictionary with the following keys:
If yearly = True:
yearly_income_statement
yearly_balance_sheet
yearly_cash_flow
If quarterly = True:
quarterly_income_statement
quarterly_balance_sheet
quarterly_cash_flow
If yearly and quarterly are both set to be True, all six key-value pairs are returned.
Possible parameters
get_holders(ticker)
Scrapes data from the Holders tab from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/holders?p=NFLX for an input ticker.
Possible parameters
get_income_statement(ticker, yearly = True)
Scrapes the income statement for the input ticker, which includes information on Price / Sales, P/E, and moving averages (e.g. https://finance.yahoo.com/quote/NFLX/financials?p=NFLX.
Possible parameters
get_live_price(ticker)
Scrapes the live quote price for the input ticker.
Possible parameters
get_quote_table(ticker , dict_result = True)
Scrapes the primary table found on the quote page of an input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/AAPL?p=AAPL)
Possible parameters
The following fields with their corresponding values are returned:
get_splits(ticker, start_date = None, end_date = None, index_as_date = True)
Downloads historical splits data of a stock into a pandas data frame.
Possible parameters
get_stats(ticker)
Scrapes data off the statistics page for the input ticker, which includes information on moving averages, return on equity, shares outstanding, etc. (e.g. https://finance.yahoo.com/quote/NFLX/key-statistics?p=NFLX.
Possible parameters
get_stats_valuation(ticker)
Scrapes the “Valuation Measures” data off the statistics page for the input ticker, which includes information on Price / Sales, P/E, and market cap (e.g. https://finance.yahoo.com/quote/NFLX/key-statistics?p=NFLX.
Possible parameters
get_top_crypto(ticker)
Scrapes data for top 100 cryptocurrencies by market cap (see https://finance.yahoo.com/cryptocurrencies).
Possible parameters
tickers_dow()
Returns a list of tickers currently listed on the Dow Jones. No parameters need to be passed. The tickers are scraped from Yahoo Finance (see https://finance.yahoo.com/quote/%5EDJI/components?p=%5EDJI.
tickers_nasdaq()
Returns a list of tickers currently listed on the NASDAQ. No parameters need to be passed. This method, along with tickers_other, works by scraping text files from ftp://ftp.nasdaqtrader.com/SymbolDirectory/.
tickers_nasdaq scrapes the nasdaqlisted.txt file from the link above, while tickers_other scrapes the otherlisted.txt file.
tickers_other()
See above description for tickers_nasdaq.
tickers_sp500()
Returns a list of tickers currently listed in the S&P 500. The data for this is scraped from Wikipedia:
https://en.wikipedia.org/wiki/List_of_S%26P_500_companies
options module
We can import any method from options module like this:
Just replace get_options_chain with any other method. Also, we can import all methods at once like so:
get_calls(ticker, date = None)
Scrapes call options data for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/options?p=NFLX.
Returns a pandas data frame containing the call options data for the given ticker and expiration date.
Possible parameters
get_expiration_dates(ticker)
Scrapes expiration dates for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/options?p=NFLX.
Returns a list of expiration dates for the input ticker. This list is based off the drop-down selection box on the options data webpage for the input ticker.
Possible parameters
get_options_chain(ticker, date)
Scrapes calls and puts tables for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/options?p=NFLX.
Returns a dictionary with two data frames. The keys of the dictionary are labeled calls (which maps to the calls data table) and puts (which maps to the puts data table).
Possible parameters
get_puts(ticker, date = None)
Scrapes put options data for the input ticker from Yahoo Finance (e.g. https://finance.yahoo.com/quote/NFLX/options?p=NFLX.
Returns a pandas data frame containing the put options data for the given ticker and expiration date.
Possible parameters
To learn more about Python and / or open source coding, check out a new online Python course I co-created with 365 Data Science! You’ll learn all about web scraping, how to use APIs in Python, how to scrape JavaScript pages, and how to deal with other modern challenges like logging into websites! Check it out on Udemy here!
Reference : http://theautomatic.net/yahoo_fin-documentation/
Last updated