Advanced Financial Analysis with Python— Part 0 —Prepping the Data

Guy
4 min readAug 3, 2021

--

Advanced Financial Analysis with Python — Part 1 — Trailing 5 Year Over Year Positive EPS Growth now available.

Welcome everybody,

In these posts, I wanted to go beyond the basics of financial analysis with Python. There are already countless discussions covering the filtering of stocks by simple metrics like market cap, volume, P/E, and many others. The thing is, you can already do all of this much more easily with any basic stock screener. Performing this research with Python is needlessly complex when the outcome can be replicated with just a few clicks on almost any screener available online.

Instead, I want to cover something new. In chapter 1 of this series, we’ll cover…

Trailing EPS Growth Year Over Year with Positive Net Income? Yikes, that’s a mouthful. Let me break it down. We’re going to filter stocks that meet the following criteria.

  • Positive EPS growth every year for the last 5 years.
  • Positive Net Income for at least the last 2 years.

What this means. The companies we’ll finish with have been growing their earnings consistently, and have been or recently become profitable.

The Symbols

Please note, all of the following code is available in a Jupyter Notebook [1]

To get started, we need data. Given the amount of data we’ll be downloading, we’ll use the Quantel API [3] so we don’t get rate limited like we would with Yahoo Finance. You can get an API key for free here.

We’ll cover the NYSE, New York Stock Exchange, but you can easily swap out the NSYE for any exchange you’d like to do research on.

First, we’ll need a list of symbols to look up. The symbols API endpoint provides a list of the symbols available on the Quantel platform. It also provides what the exchange each symbol belongs to. We can then filter our list symbols of symbols the exchange like so.

Finally, we convert the

Let’s verify our data.

Looks great.

We’ll isolate the symbols since we really don’t need any of the other information for this project.

Retrieving Income Statements

One of the invaluable features of Quantel is that you can submit multiple tickers at once separated by commas. In fact, you can submit up to 30 tickers simultaneously. This is as great as it reduces the number of requests we have to make by 30x. To achieve this, we’re going to break our list of symbols into chunks of 30 with the following function that I shamelessly stole from StackOverflow

Excellent. Now we have all our symbols in chunks of 30, we can start downloading the data we’re going to need. Usually, I’d write an asynchronous function to handle the downloading, and I will provide an alternative Jupyter Notebook that uses asyncio and aiohttp [2], however, Quantel’s Basic subscription is rate limited to 5 requests per second which our asynchronous version would definitely violate. If you’ve got a Quantel Commercial subscription feel free to use the asynchronous version of this function. For the rest of you, we’ll use the following synchronous function that takes advantage of Python’s requests library. During my testing, it took about 3 minutes to execute.

I’ve made endpoint a parameter so that this can easily be reused for downloading other data available from Quantel.

In this case, we’re going to use the income-statement API endpoint so we can get historical EPS values. You can read more about the endpoint here https://quantel.io/docs/#tag--Income-Statement

Currently, the res variable has our results in a multidimensional list. It looks something like this.

We’ll need to flatten the results so we can make it into a DataFrame.

Fantastic!

We’ve just downloaded over 44 thousand income statements from the entire NYSE in under 30 lines of Python.

The End

That completes Chapter 0. I can’t wait to get into our analysis. Follow me to stay updated on future parts.

Thanks a lot, everyone!

--

--