Pull and Analyze Financial Data Using a Simple Python Package

We demonstrate a simple Python script/package to help you pull financial data (all the important metrics and ratios that you can think of) and plot them.



Stock market analysis and good investing (for long-term growth) require careful examination of the financial data. Various metrics and ratios are often used in such analysis i.e. to assess the inherent quality of a stock. You may have heard about some of them in the talk from financial and investment experts.

For example, the Price-over-Earning ratio or PE ratio. It is the ratio of the share price over the annual earnings/share.

Or, the Book value per share. It takes the ratio of a firm’s common equity divided by its number of shares outstanding. When a stock is undervalued, it will have a higher book value per share in relation to its current stock price in the market.

Assessing a Stock's Future With the Price-to-Earnings Ratio and PEG
The price-to-earnings ratio (P/E) is one of the most widely used metrics for investors and analysts to determine stock…

Often such data are available from websites like Yahoo Finance. However, unless, you are using some kind of paid, registered service, you cannot download or scrape the data programmatically.

Yahoo Finance - Stock Market Live, Quotes, Business & Finance News
At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market…

However, many microservices exist which provide such data over a simple API call. To take advantage of that, we show, in this article, how to write a simple Python class script for interfacing with a financial data microservice.


Image source: Screen capture by the author (Website)


With this Python class, you can pull data and build a Pandas DataFrame with almost all important financial metrics and ratios by invoking a series of simple methods.

We also provide simple charting methods (bar chart and scatter plots) for analyzing the data graphically.

Note, that, you need to obtain your own secret API key (free) from the website and register it after instantiating the class object.

With that being said, let us examine the Python package/class and the various methods that come with it.


The Python class and various built-in methods

The core Python class is available here on my Github repo. Feel free to star and fork the repo and improve upon it. You can simply clone the repository, and start using the script in your own Notebook.

> mkdir My_project
> cd My_Project
> git clone https://github.com/tirthajyoti/Finance-with-Python.git
> cd financeAPI  

To keep the code clean, in this article, we show the use of the class in a test Jupyter notebook.

We start by importing regular libraries and the class object.

Image for post


Read the secret API key from a file and register it

Note that you need to have a file called Secret_Key.txt in the same directory as the code files. Without it, you cannot progress.

Register here: https://financialmodelingprep.com/login

Image for post


Create a class instance

Image for post


It has a description

Image for post


We cannot access the data without registering the key

We definitely want to start pulling the data now. Let’s say we want to build a data dictionary for the company Apple (with the ticker symbol ‘AAPL’). We can try it but we won’t be successful because we have not registered the secret key with the class object yet.

Image for post


So, we register the secret key

Image for post


Let us build a data dictionary now

For all the methods in this class, we have to pass on the ticker symbol of the company (on the US financial market). For Apple Inc, it is ‘AAPL’.

Image for post

If we examine this dictionary, we will note that a huge amount of data has been pulled from the API endpoint. A partial screenshot is provided below.

Image for post


Build a DataFrame with multiple companies’ data

Working with Python dictionaries is fine but for large-scale data analytics, we should think of building a Pandas DataFrame. We provide a built-in method to do just that. Building a DataFrame is as easy as passing on a list of ticker symbols and the code does all the data scraping and structuring job for you.

Let’s say we want to download all the financial data for the following companies,

  • Twitter
  • Facebook
  • Microsoft
  • Nvidia
  • Apple
  • Salesforce

Image for post

A nicely formatted DataFrame is ready for your use!

Image for post


What kind of data is available anyway?

We can easily examine the kind of data that has been pulled from the API service. Note, we pass on an argument ‘profile’, ‘metrics’ or ‘ration’, and get back the list of corresponding data items.

Image for post

Image for post

Image for post


On to plotting — visual analytics

In the package, we have included code for simple visual analytics with the data.

It is often helpful to examine various metrics and financial ratios in simple bar charts. To do that, just pass on the name of the variable you want to be plotted. You can also include usual Matplotlib keyword arguments such as color and transparency (alpha).

Image for post

Image for post

You can also plot simple scatter plots to visually analyze inter-relationships between financial metrics.

Image for post

You can also pass on a third variable to be used for scaling the size of the markers in the scatter plot. This helps, in an indirect way, visualize more than two variables in a 2-dimensional plot. For example, we pass on the share price parameter as the third variable in the code below.

Image for post


Custom analysis with the underlying DataFrame

Often, investors may want to create their own filters and investment logic with the available data.

For example, we may want to consider only those companies with market capitalization > 200 billion USD and then look at the metric of Enterprise value over EBIDTA in a bar chart.

We can access the underlying DataFrame, create a custom DataFrame, and then assign this custom DataFrame to a new financeAPI() object to take advantage of the ready-made charting methods.

In this way, we won't need to request data from the API again. We should avoid the data read as much as possible because of the limitation of the number of data read with the free API key.

Image for post

And then we plot a bar chart based on this custom DataFrame (embedded in the custom class object).

Image for post



We show the implementation and usage of a simple Python class/package that can be used to pull a broad range of financial metrics and ratios from a microservice.

Registration with the service is required to obtain a Free API key for this. The usage is, of course, limited in terms of the number of data read for the free account. The website also has paid plans for unlimited usage.

Note that this work focuses on static financial data, as available in the annual financial statements, and not on the dynamic time-series share market pricing data. It should be used for gauging the long-term financial strength of a firm for growth-oriented investing.

As a data scientist, often you may need to write custom class/packages to pull and analyze data from microservice APIs, and hopefully, this article (and the associated codebase) can give you a simple yet effective introduction toward that knowledge.


Also, you can check the author’s GitHub repositories for code, ideas, and resources in machine learning and data science. If you are, like me, passionate about AI/machine learning/data science, please feel free to add me on LinkedIn or follow me on Twitter.

Original. Reposted with permission.