Industry Analysis with Python

During this post we are going to perform an industry analysis with Python. We will build, analyse and compare all stocks operating within an industry. To do so,

Industry Analysis with Python

Sep 11, 2023 5:37 PM - Rajnish Katharotiya

blog post cover photo

Image credit: Victor

During this post we are going to perform an industry analysis with Python. We will build, analyse and compare all stocks operating within an industry. To do so, we will pick an industry and get all important financial metrics for companies operating in that industry. Then, we will use a couple of filters to identify the most attractive stocks operating in that sector. As a heads-up, you can see an extract of the expected result in below screenshot. We will end up having more than 19 financial metrics for each of the companies!

Photo by Pixabay on Pexels

What is an Industry Analysis?

An industry analysis is a very effective way to put into perspective and compare different stocks operating within an industry. This is particularly useful to compare company performance. Given that companies operating in the same industry share certain similarities, it can be a good starting point to benchmark different companies together.

In addition to comparing companies, performing an industry analysis will help us understand opportunities and threads that are characteristics of that industry. For example, we could apply the 5 forces of Porte to get to know the strengths and weaknesses of an industry. If an industry is very competitive and with low barriers of entry, it may be difficult for a company to thrive.

In this post, we will focus more on comparing companies with each other by building a screener for a selected industry.

How to Build a Stock Industry Analysis Tool

To perform the industry analysis, we will perform below steps:

  • Create an industry Python screener to retrieve all tickers from companies operating in the selected industry.

  • Calculate all relevant financial ratios.

  • Add all companies into a Pandas DataFrame. This will easily allow us to compare company financials within the industry.

  • Create filters to find attractive stocks within the selected industries. E.g. keep only companies with Price to Earnings ratio below 10 and revenue growth of at least 10% in the last 4 quarters.

  • Plot the selected stocks for easy visualisation

Python Stock Screener for Industry Analysis

First of all, lets start by getting tickers for all stocks within the Technology sector.

To begin, you need to define our screener criteria. Technology stocks trading in NASDAQ and with a market capitalization higher than $100,000,000. In the example the limit of the number of companies is 100. Feel free to increase or remove the limit to include more companies in the analysis.

import requests

import numpy as np

import pandas as pd

demo = 'your api key'

sector = 'Technology'

exchange = 'NASDAQ'

marketcapmorethan = '1000000000'

number_of_companies = 100

symbols = []

screener = requests.get(f'{marketcapmorethan}=1000000000&volumeMoreThan=10000§or={sector}&exchange={exchange}&limit={number_of_companies}&apikey={demo}').json()

for item in screener:



Calculating Companies Financial Ratios

Now that we have all Technology related tickers in a Python list, we can start retrieving company financials from the API. This is very similar to what we have been doing in previous posts.

What the code below is doing is looping through each of the companies obtained in the section before. Then, it retrieves the company Income Statement, Balance Sheet and Cash Flow financial statements. Next, we calculate each of the financial metrics such as ROE, revenue growth, dividend yield, etc.

Finally, we add all metrics into a Python Dictionary.

FinMetrics = {}

for company in symbols:


companydata = requests.get(f'{company}?apikey={demo}').json()

IS = requests.get(f'{company}?period=quarter&apikey={demo}').json()

BS = requests.get(f'{company}?period=quarter&apikey={demo}').json()

CF = requests.get(f'{company}?period=quarter&apikey={demo}').json()

#revenue last 4 years

revenue = [IS[0]['revenue'],IS[1]['revenue'],IS[2]['revenue'],IS[3]['revenue']]

revenue = np.array(revenue).sum()

net_income = [IS[0]['netIncome'],IS[1]['netIncome'],IS[2]['netIncome'],IS[3]['netIncome']]

net_income = np.array(net_income).sum()

FCF = CF[0]['freeCashFlow'] + CF[1]['freeCashFlow'] + CF[2]['freeCashFlow'] + CF[3]['freeCashFlow']

OperatingCF = CF[0]['operatingCashFlow'] + CF[1]['operatingCashFlow'] + CF[2]['operatingCashFlow'] + CF[3]['operatingCashFlow']

total_debt = BS[0]['totalDebt']

eps_diluted = [IS[0]['epsdiluted'],IS[1]['epsdiluted'],IS[2]['epsdiluted'],IS[3]['epsdiluted']]

eps_diluted = np.array(eps_diluted).sum()

total_shareholders_equity = BS[0]['totalStockholdersEquity']

shareholders_equity_2_quarters_Average = (BS[0]['totalStockholdersEquity'] + BS[1]['totalStockholdersEquity']) / 2

total_assets_2qts = (BS[0]['totalAssets'] + BS[1]['totalAssets']) / 2

latest_Annual_Dividend = companydata[0]['lastDiv']

price = companydata[0]['price']

market_Capitalization = companydata[0]['mktCap']

name = companydata[0]['companyName']

exchange = companydata[0]['exchange']

EBITDATTM = IS[0]['ebitda'] + IS[1]['ebitda'] + IS[2]['ebitda'] + IS[3]['ebitda']

EBITDA5YTTM = IS[20]['ebitda'] + IS[21]['ebitda'] + IS[22]['ebitda'] + IS[23]['ebitda']


grossprofitma12TTMgrowht = ((IS[0]['grossProfitRatio'] - IS[3]['grossProfitRatio']) / IS[3]['grossProfitRatio']) *100

dividend_Yield= latest_Annual_Dividend/price

FinMetrics[company] = {}

FinMetrics[company]['Dividend_Yield'] = dividend_Yield * 100

FinMetrics[company]['latest_Price'] = price

FinMetrics[company]['latest_Dividend'] = latest_Annual_Dividend

FinMetrics[company]['market_Capit_in_M'] = market_Capitalization/1000000

FinMetrics[company]['pe'] = price / eps_diluted

FinMetrics[company]['ps'] = market_Capitalization / revenue

FinMetrics[company]['pb'] = market_Capitalization / total_shareholders_equity

FinMetrics[company]['PEG'] = FinMetrics[company]['pe'] / EBITDA5YGrowht

FinMetrics[company]['GPM'] = IS[0]['grossProfitRatio']

FinMetrics[company]['latest_Financials'] = IS[0]['date']

FinMetrics[company]['GPM12TTMGrowth'] = grossprofitma12TTMgrowht

FinMetrics[company]['Revenue_last6qts_inM'] = [IS[0]['revenue'],IS[1]['revenue'],IS[2]['revenue'],IS[3]['revenue'],IS[4]['revenue'],IS[5]['revenue']]

FinMetrics[company]['Revenue_last6qts_inM'] = np.array(FinMetrics[company]['Revenue_last6qts_inM']) / 1000000

FinMetrics[company]['ptoOperatingCF'] = market_Capitalization / OperatingCF

FinMetrics[company]['ptoFCF'] = market_Capitalization / FCF

FinMetrics[company]['Debt_to_Equity'] = total_debt / total_shareholders_equity

FinMetrics[company]['ROE'] = net_income / shareholders_equity_2_quarters_Average

FinMetrics[company]['ROA'] = net_income / total_assets_2qts

FinMetrics[company]['revenue_growht_4qrts'] = ((IS[0]['revenue'] - IS[3]['revenue'])/ IS[3]['revenue']) *100

FinMetrics[company]['earnings_growht_4qrts'] = ((IS[0]['netIncome'] - IS[3]['netIncome'])/ IS[3]['netIncome']) *100



Note that we include a try and except as part of the code so that in case some of the API calls return an error, our code keeps working.

You can see that we have calculated more than 18 financial metrics for our industry analysis with Python.

Finally, we convert our FinMetrics dictionary into a Pandas DataFrame:

all_measures= pd.DataFrame.from_dict(FinMetrics,orient='index')


Let's make the tool better by having some filters and plotting capabilities.

Filtering Attractive Stocks within the Industry

Right now, we have a huge number of rows in our industry stock DataFrame. This may not be the best option in order to identify attractive stocks since it is difficult to look at more than 70 rows simultaneously. Our eyes are not built for that.

Therefore, we are going to create some custom filters. In this example, lets find stocks with low price to xxx ratios. Stocks having a low price to earnings/sales ratio are considered to be value stocks. They may be seen as a bargain (of course further research is required before putting our money into them):

First, we keep only companies with a price to earnings ratio higher than 0. This way, we get ride of companies having a negative profit.

Then, I have only activated filter number 1 in below code. Which will filter stocks with a PE ratio lower than 30, PS ratio lower than 20, PB ratio lower than 20 and price to FCF lower than 20.

Next, we sort by companies with the lowest Price to Earnings ratio. The idea is to keep only 8 companies.

Finally, lets also compute and add as a new row the median of the industry for each of the financial ratios.

#keep only companies with a PE higher than 10

price_earnings = all_measures[all_measures['pe'] > 0]

#filter option 1

price_earnings = price_earnings[(price_earnings['pe'] < 30) & (price_earnings['ps'] < 20) & (price_earnings['pb'] < 20) & (price_earnings['ptoFCF'] < 20)]

#filter option 2

#price_earnings = price_earnings[(price_earnings['pe'] < 20) & (price_earnings['revenue_growht_4qrts'] > 10)]

#Sort entities to show companies with lower PE ratio first

price_earnings = price_earnings.sort_values('pe')

#keep only 8 companies:

price_earnings = price_earnings[0:8]

#calculate industry median for each of the ratios:

price_earnings.loc[-1] = all_measures.median()

#rename the column

price_earnings.rename(index={-1: "median_industry"},inplace=True)


Plotting the Filtered Stocks

The last step of our industry analyser tool is to plot the results. That way, we can easily visualise and compare between the stocks within the selected industry or sector:



Wrapping Up

We have built an amazing tool to perform an industry analysis with Python. Some websites are charging a crazy amount of money for a similar service. Here, we have built it ourselves and we can customise it to our needs!

My advise is to use the tool as a screener to find interesting stocks in an industry. However, do not invest in any stock by simply applying this analysis. You must perform some additional research on the selected company.

Also, read the latest company financial report to see how the company sees its future outlook. Google also the company to find out if it is facing some legal challenges or any other negative surprises.

In addition, I also advise to do some due diligence on the company managers. Finally, consider how the current economy cycle is affecting the selected company.

The last piece of advise is to have a look at the company insider trading transactions of the company. If insiders are buying stocks may be a good sign.

Other Blogs

Sep 11, 2023 - Rajnish Katharotiya

P/E Ratios Using Normalized Earnings

Price to Earnings is one of the key metrics use to value companies using multiples. The P/E ratio and other multiples are relative valuation metrics and they cannot be looked at in isolation. One of the problems with the P/E metric is the fact that if we are in the peak of a business cycle, earni...

blog post title

Sep 11, 2023 - Rajnish Katharotiya

What is Price To Earnings Ratio and How to Calculate it using Python

Price-to-Earnings ratio is a relative valuation tool. It is used by investors to find great companies at low prices. In this post, we will build a Python script to calculate Price Earnings Ratio for comparable companies. Photo by Skitterphoto on Pexels Price Earnings Ratio and Comparable Compa...

blog post title

Oct 17, 2023 - Davit Kirakosyan

VMware Stock Drops 12% as China May Hold Up the Broadcom Acquisition

Shares of VMware (NYSE:VMW) witnessed a sharp drop of 12% intra-day today due to rising concerns about China's review of the company's significant sale deal to Broadcom. Consequently, Broadcom's shares also saw a dip of around 4%. Even though there aren’t any apparent problems with the proposed solu...

blog post title


Financial Modeling Prep API provides real time stock price, company financial statements, major index prices, stock historical data, forex real time rate and cryptocurrencies. Financial Modeling Prep stock price API is in real time, the company reports can be found in quarter or annual format, and goes back 30 years in history.
2017-2023 © Financial Modeling Prep