Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
How to Get Stock Earnings Data with Python

How to Get Stock Earnings Data with Python

Posted October 28, 2021 at 9:46 am
Andrew Treadway
TheAutomatic.net

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.

si.get_next_earnings_date("aapl")
 
si.get_next_earnings_date("nflx")

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).

si.get_earnings("aapl")
 
si.get_earnings("amzn")

Visit http://theautomatic.net/ for additional insight on this topic:
http://theautomatic.net/2021/02/16/how-to-get-stock-earnings-data-with-python/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

Disclosure: Displaying Symbols on Video

Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.