How to collect Financial Statements Stock Data for Fundamental Analysis using Python

Raphael De Lio
5 min readSep 9, 2021

Twitter | LinkedIn | YouTube | Instagram

In today’s story I’m gonna teach you how to collect data to implement programmatic ways of evaluating a business at a financial level and determining whether the current price of a company’s stock reflects the future value of the company.

Fundamental analysis uses different factors such as the current economic environment and finances of the company to estimate its stock value. Different key ratios are also used to determine the financial health and understand the true value of a company’s stock.

If you’re new to Python and don’t know how to get started I recommend you first read my concise story on Getting Started With Python as an Absolute Beginner.

Finding the right API

Data is something you find everywhere, but finding organized data and the data you actually need might be something tricky. Luckily, you can find online different services that provide the data we need in the form of REST APIs.

In today’s tutorial we will be using an API provided by Financial Modeling Prep which offers a limited free plan, perfect for testing ideas.

In order to use their REST API we will need an API key to authenticate. You can create a free account on their website to get one.

Let’s start coding

Now that you have your API key, let’s get our hands dirty and start playing with some data. First of all, let’s learn how we can consume their API.

For this tutorial I will be using PyCharm, but you can use the editor of your choice.

After creating a new project, let’s type down the following code:

If you run the code above, you should see a really long String that will be difficult to read. Something like:

[{'date': '2020-09-26', 'symbol': 'AAPL', 'reportedCurrency': 'USD', 'fillingDate': '2020-10-30', 'acceptedDate': '2020-10-29 18:06:25', 'period': 'FY', 'revenue': 274515000000, 'costOfRevenue': 169559000000............................

Good! You have received back a JSON representing a list of income statements filed by Apple.

Before we continue, let’s see what variables a single income statement holds by running:

This should print:

Good job! We can see we have some meaningful data we can use for playing around now! Let’s see what we can do with it.

Getting Started With Pandas

Python is awesome for data analysis. And a library that contributes to that fame is Pandas. Pandas allows us to read, manipulate, aggregate and display data easily and with few commands. It’s open source, easy to learn and has a big and growing community around.

To install Pandas, you must run on your command line:

pip3 install pandas

Let’s get started with pandas by converting our JSON object into a data frame and then visualizing the data as a table:

By adding a breakpoint after the data_frame object was initialized and running our program in debug mode, we can see that an option to View as DataFrame is shown next to our variable:

By clicking on it, a new window will open where we can see our data displayed as a table:

Data frame displayed as a Table

Analyzing Data With Pandas

As mentioned before, Pandas offers a series of tools out of the box. Now that we have everything we need, we can start analyzing our Financial Statements data using Python and Pandas. For the examples below, we will use a few of the cases Warren Buffett mentions to use in his book “Warren Buffett and the Interpretation of the Financial Statements”

Gross Profit

According to Buffett what creates a high gross profit margin is the company’s durable competitive advantage, which allows it the freedom to price the products and services it sells well in excess of its cost of good solds. Here’s an example of how you can calculate the average of the gross profit ratio:

By running the code above, you should see something similar to this printed in the console:

General And Administrative Expenses

This is where the company reports its costs for direct and indirect selling expenses and all general and administrative expenses incurred during the accounting period. Buffett says that anything that stays under 30% should be considered fantastic. Let’s see how we can calculate the percentage change from one year to the previous and the average of the percentage change in the past 5 years:

By running the code above, you should see something similar to this printed in the console:

And that’s it!

Whether you agree or not with Buffett’s methodology for analysing financial statements, you are now ready to implement your own analysis strategies! I hope you have enjoyed this article and see you on the next one!

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Python with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--

Raphael De Lio

Software Consultant @ Xebia - Dutch Kotlin User Group Organizer: https://kotlin.nl