A Python Package to Easily Retrieve Financial Data: Part — 1
An easy solution to access fundamental data with less coding
Introduction
Adequate and reliable financial data is very critical for traders as it influences their investment decision to a larger extent. Thus, the process of data collection is seriously considered and undertaken by the traders to maximize their edge over the market.
Now, financial data can be extracted using python in two different ways. The first way is to utilize the financial APIs offered by various providers and make API calls to obtain the desired data. The second way is to make use of the open-source python packages/libraries to retrieve the data.
Most people prefer the second way of extracting data which is through open-source python packages. This is mainly due to the fact that getting the data using financial APIs demand greater effort than using open-source packages and this acts as a hindrance for beginner programmers.
In this article, we are going to focus on a python package named eod
that helps in the process of data collection by providing reliable and different types of financial data. Without further ado, let’s dive into the article!
eod
Before jumping to the programming part, it is essential to know about the package we are going to use in this article. eod
is an open-source python library that supports a vast amount of market data making it accessible to every trader. This package is developed by EOD Historical Data APIs (EODHD APIs) which is a major player in the financial APIs provider arena. They created this open-source project with the aim of enabling beginner programmers to interact with their APIs in a much easier way.
There are two main advantages of using the eod
package. Firstly, the data can be extracted in a very straightforward manner and requires very less coding to finish the task. Secondly, since the package is backed by EODHD’s financial APIs, users have access to a humongous variety of market data ranging from basic to unique and critical information.
Python Implementation
In this section, we will get into to so do some coding in python to extract various market data namely fundamental data, historical or end-of-day prices data, intraday data, and real-time data. Here is a basic outline of this section for an easy follow-up:
1. Importing Packages
2. API Key Activation
3. Fundamental Data
4. Historical Data
5. Intraday Data
6. Real-time Data
The first two topics are mostly concerned with setting up the python programming environment, and the rest of the topics are related to the data extraction process.
From the third topic, additional processes such as data analysis on the extracted data are carried out for providing a gist of the practical use of the obtained data. It is not mandatory to keep up with the additional steps but highly recommended to do so.
Since each of the topics contains in-depth explanations and extensive coding processes, the article is divided into different parts. This article is obviously the first part of the series where we are going to focus only on the extraction of the Fundamental data and the rest of the topics will be covered in the upcoming parts. With that being said, let’s do some coding!
Importing Packages
The first and foremost step of setting up the coding environment is to import the required packages. In this article, we are going to use seven different packages which are eod
for financial data extraction, pandas
for data manipulation, matplotlib
for data visualization, datetime
for working with dates and times, time
for specific time-related functions, termcolor
for customizing the printed output, and finally mplfinance
for financial visualizations. The following code will import all the mentioned packages into our python environment:
from eod import EodHistoricalData
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import time
from termcolor import colored as cl
import mplfinance as mf
plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = [20, 10]
From the above-represented code, it is worth noting that apart from importing the essential packages, we are also making some changes to the default matplotlib
package settings to customize the theme and size of the visualizations. This is an optional step undertaken for beautifying the plots to be generated later in this article.
Also, if you haven’t installed any of the imported packages, make sure to do so using the pip
command in your terminal.
API Key Activation
It is essential to register the EODHD API key with the package in order to use its functions. If you don’t have an EODHD API key, firstly, head over to their website, then, finish the registration process to create an EODHD account, and finally, navigate to the ‘Settings’ page where you could find your secret EODHD API key. It is important to ensure that this secret API key is not revealed to anyone. You can activate the API key by following this code:
api_key = '<YOUR API KEY>'
client = EodHistoricalData(api_key)
The code is pretty simple. In the first line, we are storing the secret EODHD API key into the api_key
and then in the second line, we are using the EodHistoricalData
class provided by the eod
package to activate the API key and stored the response in the client
variable.
Note that you need to replace <YOUR API KEY>
with your secret EODHD API key. Apart from directly storing the API key with text, there are other ways for better security such as utilizing environmental variables, and so on.
Extracting Fundamental Data
Fundamental data consists of general information about an equity’s financial status and position. This type of data plays a vital role in the analysis process and helps to ascertain the investment decisions of the traders. Fundamental data of any given equity can be easily extracted using the following code:
ticker = 'AAPL.US'
aapl_f_info = client.get_fundamental_equity(ticker)
aapl_f_info_keys = list(aapl_f_info.keys())
for i in range(len(aapl_f_info_keys)):
print(cl(f'{i+1}. {aapl_f_info_keys[i]}', attrs = ['bold']))
In the first line of the code, we are creating a variable to store the symbol of the equity we are interested in extracting which in our case is Apple. Then, using the get_fundamental_equity
function provided by the eod
package, we are extracting the fundamental data of Apple.
This function consists of two parameters: ticker
and filter_
. In the above code, only the ticker
field is utilized that takes the symbol of the stock as the input which in our case is Apple (AAPL.US). The filter_
field will be used later in this part of the article.
Finally, we are storing the response in the aapl_f_info
variable. Apart from these two lines of code, the remaining are optional and written to produce the following output:
1. General
2. Highlights
3. Valuation
4. SharesStats
5. Technicals
6. SplitsDividends
7. AnalystRatings
8. Holders
9. InsiderTransactions
10. ESGScores
11. outstandingShares
12. Earnings
13. Financials
The above-produced output is a list of all the details contained in the extracted fundamental data of Apple. It is evident that the fundamental data provides its users with a sufficient amount of information ranging from general details to the company’s financials and even insider transactions.
But some might feel a bit overwhelmed by the huge data as they would seek only particular details of a company. This is where the filter_
parameter which we discussed earlier comes into play.
As the name suggests, the filter_
parameter can be used to filter the fundamental data and extract only those details which are required by the users. For example, if a person is interested only in the Earnings section of the fundamental data, it can be easily extracted by following this code:
earnings = client.get_fundamental_equity(ticker, filter_ = 'Earnings::History')
earnings_dates = list(earnings.keys())
cols = list(earnings[earnings_dates[0]].keys())
earnings_df = pd.DataFrame(columns = cols)
for i in range(len(earnings_dates)):
data = earnings[earnings_dates[i]]
earnings_df.loc[i] = data
earnings_df = earnings_df.drop('reportDate', axis = 1).set_index('date').dropna()
earnings_df = earnings_df.iloc[::-1]
The line which demands the most attention is the first line. It is very similar to what we did previously but the only change is the addition of the filter_
parameter in the get_fundamental_equity
function. The filter_
parameter expects the following format:
<MainCategory>::<SubCategory 1>::<SubCategory 2>....
Here, <MainCategory>
refers to any one of the thirteen categories that was previously represented in the output and <SubCategory>
refers to the subdivisions present inside each of the main categories. In the above code, we specified up to <SubCategory 1>
but it can be further narrowed down based on the remaining subdivisions.
The remaining lines of code are mostly focused on working with the extracted data consisting of the different processes of data cleaning, data formatting, and data manipulation. The final output is a Pandas dataframe that looks like this:
Data Analysis
Now that we have well-formatted data to work with, let’s do some data analysis. Before diving into the data analysis process, it is necessary for us to get ourselves clear with the terms contained in the dataframe.
Firstly, EPS refers to Earnings Per Share. As the name suggests, it reveals how much money a company makes from one share. In the data frame, we have three columns related to EPS that are: epsActual
, epsEstimate
, epsDifference
. The names of the columns are self-explanatory. Secondly,
Surprise Percent (surprisePercent
) refers to the earnings earned over and above the actual estimates of the analysts for a specific quarter. For example, in the third row, we can see that the surprisePercent
is 10 which means the earnings of Apple during that quarter beat the analysts' estimates by 10%.
The columns we are going mainly focus on for the analysis process are the EPS-related columns. Now, we are not going to pursue an extensive data analysis but rather just to get a glimpse of the data and the best way of doing so is to run a trend analysis. The most preferred way to analyze trends in data is through data visualization. The following code will produce two different plots for a better understanding of the data:
earnings_df[['epsActual', 'epsEstimate', 'epsDifference']].plot()
plt.title('AAPL EARNINGS HISTORY')
earnings_df[['epsActual', 'epsEstimate', 'epsDifference']].cumsum().plot()
plt.title('AAPL EARNINGS HISTORY (CUMULATIVE)')
The code is very simple that it requires no in-depth explanation. Here, both the Pandas
and matplotlib
packages are used in the visualization process though many prefer going with matplotlib
alone. The code generates two line charts that look like this:
The image on the left side is a line chart showing the trend of the three different EPS-related data over the years and the image on the right side represents the cumulative earnings figures of Apple. By observing both of these charts, it’s pretty clear that the earnings of Apple are moving in a strong positive direction and it’s more like for the trend to be continued.
But one thing that needs to be seriously taken into consideration is the slow decline of the epsDifference
which shows the difference between the actual earnings earned and the estimated earnings by analysts. So when the epsDifference
hits negative figures, it implies that the actual earnings of Apple are not keeping up with its expected values. This might not be that big of an issue but still, it’s safe to say that it’s worth the consideration.
Closing Note
In this article, we have learned about a new package named eod
and how it can be used to extract fundamental data of equities. To take this a step further, we did some basic data analysis on the extracted data and minted some insights regarding the stock’s earnings trend.
This article will be continued in its next part on discovering the various functions of the eod
package and the next article is centered around the extraction of historical data of assets using this package. So watch out for the next part very soon. With that being said, you’ve reached the end of the article. Hope you learned something new today. Happy coding!