Building an Economic Calendar using Python and APIs | by Nikhil Adithyan | CodeX | Nov, 2021

A short and sweet way of acknowledging the latest economic events

Nikhil Adithyan
Photo by Nick Hillier on Unsplash

Financial news is not the only major factor that influences the stock market but also another prominent aspect which is the economic events. To define it in plain english, economic events are events related to a company’s finance that have economic significance and influences the stock market as well as the company’s financial condition.

Like how stock screeners are built to easily get the latest information on the price changes of the desired stocks, economic calendars are used for updating themselves with the latest and upcoming economic events and making investments accordingly.

Since I’m not really fond of using external economic calendars tools and building things myself is my forte, my go-to for this task is always Python and with the help of EOD Historical Data’s (EODhd) APIs, the process is made easier. In this article, we will be building two different types of economic calendars using EOD Historical Data’s API for economic calendars in Python. Without further ado, let’s dive into the article!

Let’s first start by importing the required packages into our python environment as it is the foremost process in any programming project. The only packages we need in this article are Requests to make API calls and Pandas for working with the data. In case you have not yet installed these two packages, enter the following lines of code in your command line:

pip install requests
pip install pandas

After installing the packages, we can now import them into our python environment using the code below:

import requests
import pandas as pd
api_key = 'YOUR API KEY'

We have now successfully imported the essential packages into our environment along with storing the API key into the api_key variable.

The first type of calendar we are going to build in this article is an earnings calendar. For those who don’t what an earnings calendar is, it is a calendar that reveals the reporting as well as the releasing date of the earnings statement of a particular company. Investors use earnings statements to assess the performance of the stocks and start tuning their portfolios accordingly. Hence it can be said that the earnings statement has a direct influence on the investors and the market, and has economic significance.

In this article, we are going to build two different types of earnings calendars: An upcoming earnings calendar and a historical earnings calendar of a specific stock. Let’s start off with the first type of calendar.

Upcoming Earnings Calendar

The basic idea of this section is to define a function that builds a basic upcoming earnings calendar representing the reporting date of the earnings statement of a particular along with its influence on the market. The python code to build the calendar looks like this:

def get_upcoming_earnings(start_report_date, end_report_date, currency, api_key, n_limit):

url = f'https://eodhistoricaldata.com/api/calendar/earnings?api_token={api_key}&fmt=json&from={start_report_date}&to={end_report_date}'
u_earnings = requests.get(url).json()

u_earnings_df = pd.DataFrame(u_earnings['earnings']).drop('before_after_market', axis = 1).fillna(0)
u_earnings_df = u_earnings_df[u_earnings_df.currency == f'{currency}']
u_earnings_df = u_earnings_df[u_earnings_df.actual != 0]
u_earnings_df = u_earnings_df.rename(columns = {'code':'stock'})
u_earnings_df = u_earnings_df.iloc[-n_limit:]
u_earnings_df.index = range(len(u_earnings_df))

return u_earnings_df

us_stocks_u_earnings = get_upcoming_earnings('2021-11-23', '2021-11-26', 'USD', api_key, 10)
us_stocks_u_earnings

Let’s now break down the above-represented code. Firstly, we are defining a function named get_upcoming_earnings that takes the following as parameters: The starting date of the earnings calendar (start_report_date), the currency of the earnings data (currency), the ending date of the earnings calendar (end_report_date), API Key (api_key), and finally the length of the output (n_limit).

Inside the function, we are first storing the API URL into the url variable, followed by that, we are making an API call using the get function provided by the Requests package to extract the data in JSON format. Let’s now take a minute to have a look at the structure of the API URL. This is the URL used inside the above-defined function:

https://eodhistoricaldata.com/api/calendar/earnings?api_token={api_key}&fmt=json&from={start_report_date}&to={end_report_date}

The first parameter in the URL is the api_token parameter where we have to specify the API key. The second parameter is the fmt parameter, which is an optional one, where we have to mention the type of output we want, either JSON or CSV. The third and fourth parameters are the from and to parameters in which the starting and ending date of the earnings calendar needs to be specified.

Coming back to the code, after storing the API response into the data variable, we are converting it into a Pandas Dataframe along with some data manipulations and stored it into another variable u_earnings_df . The following lines of code are further data manipulations carried out based on the given inputs. Lastly, we are returning the final dataframe and calling the created function to build an upcoming earnings calendar that looks like this: