PyQuant News is on Substack

Looking for the newest weekly Python deep dives for algorithmic trading, market data analysis, and quant finance? Subscribe to the PyQuant Newsletter on Substack for free.

Subscribe for free 👉

How to download more fundamental data to power trading

February 17, 2024
Facebook logo.
Twitter logo.
LinkedIn logo.
Newsletter issue count indicatorNewsletter total issues indicator
How to download more fundamental data to power trading

How to download more fundamental data to power trading

Quants, financial analysis, and traders use fundamental data for investing and trading. These data are derived from quarterly and annual statements that companies file with the U.S. Securities Exchange Commission (SEC).

These statements are rich with data that can be used to build predictive factor models for investment portfolios.

The problem?

We can't download all these documents, parse them, and use them in a way that is useful for analysis at scale.

Until now.

How to download more fundamental data to power trading

The Edgar (Electronic Data Gathering, Analysis, and Retrieval) system is operated by the SEC. It automates the submission and retrieval of financial documents filed by companies. It also makes these data available electronically.

Edgar includes filings like annual reports (10-K) and quarterly reports (10-Q).

The data can be downloaded in various formats. These include HTML, XML, and plain text which makes it easy to use with Python.

After reading today's newsletter, you'll be able to download filing data, parse it, and use it to compute price to earnings ratio.

Let's dive in!

Imports and set up

Let's start with the libraries we need for the analysis. These libraries are standard Python libraries with the exception of OpenBB which we'll use for data.

Download and extract the filing data

We first generate a list of filing quarters to download. In our example, we'll grab 4 quarters worth of data in 2015. The Edgar filing data is large so make sure you start with only a few quarters to get things running.

This code iterates over each filing period, downloads, and extracts SEC filing documents for each period. For each year and quarter, it builds a directory path and fetches the Zip file from the SEC website. The Zip file is then extracted and each file in the Zip archive is saved to the directory.

The next step is to convert the extracted files to the on-disk, columnar format, Parquet.

The code iterates through the downloaded TSV files in the DATA_PATH directory, converting each to a Parquet file in the 'parquet' subdirectory. For each TSV file, it reads the file into a DataFrame, writes the DataFrame to a new Parquet file, and then deletes the original TSV file.

Build the fundamentals data set

Now that we've stored the data from each of the filings as Parquet files, we can begin building the data set.

This code uses pandas to filter the sub DataFrame where the company name is APPLE INC. From there, we transpose the columns in the DataFrame to the rows, drop any rows where there is no data, and convert it into a Series. This code extracts Apple's data from the quarterly and annual filing documents.

Use the fundamental data to build the PE ratio

First, extract all numerical data available from the Apple filings.

Now, we can select a field, such as earnings per diluted share (EPS), that we can combine with market data to calculate the price to earnings ratio.

This code extracts the diluted earnings per share from each of the filings and plots it in a bar chart.

How to download more fundamental data to power trading. Quants, financial analysis, and traders use fundamental data for investing and trading.
How to download more fundamental data to power trading. Quants, financial analysis, and traders use fundamental data for investing and trading.

Now use OpenBB to grab market data and align it with the EPS data to compute the price to earnings ratios.

The result is a chart depicting the closing price, diluted EPS, the the trailing price to earnings ratio.

How to download more fundamental data to power trading. Quants, financial analysis, and traders use fundamental data for investing and trading.
How to download more fundamental data to power trading. Quants, financial analysis, and traders use fundamental data for investing and trading.

Next steps

We only scratched the surface of what is available through the Edgar filings. As a next step, extract and parse the following fields:

• PaymentsOfDividendsCommonStock

• WeightedAverageNumberOfDilutedSharesOutstanding

• OperatingIncomeLoss

• NetIncomeLoss

• GrossProfit