In this post, we’ll walk through a few examples for getting stock earnings data with Python. We will be using yahoo_fin, which was recently updated. The latest version now includes functionality to easily pull earnings calendar information for individual stocks or dates.
If you need to install yahoo_fin, you can use pip:
pip install yahoo_fin
If you already have it installed and need to upgrade, you can update your version like this:
pip install yahoo_fin --upgrade
To get started, let’s import yahoo_fin:
import yahoo_fin.stock_info as si
Getting stock earnings calendar data
The first method we’ll cover is the get_earnings_history function. get_earnings_history returns a list of dictionaries. Each dictionary contains an earnings date along with EPS actual / expected information. Let’s test it out with a few sample tickers.
# get earnings history for AAPL aapl_earnings_hist = si.get_earnings_history("aapl")
Depending on the ticker, the data returned may include future earnings dates. Any data for future earnings dates will have a None value for EPS actual. Below, we look at one of the entries returned. We can see the earnings date for this entry is July 30, 2019. The EPS actual is 0.55 and the EPS estimate is 0.53.
If you want to convert the list of earnings dictionaries to a data frame, you can use pandas like this:
import pandas as pd frame <- pd.DataFrame.from_dict(aapl_earnings_hist)
How to get the next earnings date for a stock
yahoo_fin also now has a function to find the next upcoming earnings date for an input stock ticker. If the next earnings date is known, then you can find it using the get_next_earnings_date method.
How to get stocks with earnings on a specific date
What if you want to know all the stocks that have earnings on a specific date? You can find them using the get_earnings_for_date method. This function takes a single date as input and returns a list of dictionaries. Each dictionary contains a ticker, company name, EPS estimate, and (if known) EPS actual. The EPS actual value will only have a value if you input a date in the past – otherwise, you’ll get a None type. get_earnings_for_date can handle a variety of date formats as input.
si.get_earnings_for_date("02/18/2021") si.get_earnings_for_date("2021-02-25") si.get_earnings_for_date("March 1 2021")
How to get stocks with earnings on a date range
Extending from the above section, you can also get all the stocks that have earnings within an input date range. To do that, let’s take a look at the get_earninggs_in_date_range method. This function takes a start date and end date as parameters.
earnings_in_week = si.get_earnings_in_date_range("02/16/2021", "02/23/2021")
If you’re pulling a longer range, this method may take more time as the API needs to pull each day separately.
Getting recent revenue data
The last function we’ll cover in the article is the get_earnings method. get_earnings takes an input ticker and returns a dictionary of yearly / quarterly revenue and the last four quarters of EPS actual / estimate data (see here: https://finance.yahoo.com/quote/aapl/financials?p=aapl).
Visit http://theautomatic.net/ for additional insight on this topic:
Disclosure: Interactive Brokers
Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from TheAutomatic.net and is being posted with permission from TheAutomatic.net. The views expressed in this material are solely those of the author and/or TheAutomatic.net and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.