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, 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!

Industry Analysis with Python
Industry Analysis with Python

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.

Industry Analysis Python Stock Screener

Last week, I was trying to make some research on the Technology industry in order to identify attractive stocks. The approach in mind was to screen the whole industry and identify a bunch of stocks to focus my analysis on.

The problem was that while there were hundred of sites where I could look up financial information for a single stock, I could not find a free website where I could compare multiple company financials. The majority of sites required a costly monthly subscription in order to be able to compare companies.

Then is when I thought, why not building a tool to analyse industry stocks with Python and share it with all my readers? I am sure that some of you may benefit from such a Python stock analyser tool.

In our particular case, we are going to build the tool focus on comparing stocks within an industry. Of course, feel free to take the code and customise for your needs.

How to Build a Stock Industry Analysis Tool

Before moving to the code, lets define our approach. For retrieving all company financials, I will use financialmodelingprep, a financial API which offers up to 250 free monthly requests per month. Alternatively, if you need more than 250 requests per month, financialmodelingprep offers 25% extra discount to all readers of this blog for all plans. By using the following link, you can get a yearly subscription to the API for a little more than $100 a year.

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

  1. Create an industry Python screener to retrieve all tickers from companies operating in the selected industry.
  2. Calculate all relevant financial ratios.
  3. Add all companies into a Pandas DataFrame. This will easily allow us to compare company financials within the industry.
  4. 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.
  5. Plot the selected stocks for easy visualisation

1. Python Stock Screener for Industry Analysis

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

Note that you will need to open a free account or subscribe to one of the financialmodelingprep plans in order to get an api key. Once you have it, place the key within the demo variable in the code below.

Then, we define our screener criteria. Technology stocks trading in NASDAQ and with a market capitalization higher than $100,000,000. I limit the number of companies to 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'https://financialmodelingprep.com/api/v3/stock-screener?{marketcapmorethan}=1000000000&volumeMoreThan=10000&sector={sector}&exchange={exchange}&limit={number_of_companies}&apikey={demo}').json()
for item in screener:
  symbols.append(item['symbol'])

symbols
###outcome
['AAPL',
 'MSFT',
 'NVDA',
 'INTC',...

2. 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:
  try:
    companydata = requests.get(f'https://financialmodelingprep.com/api/v3/profile/{company}?apikey={demo}').json()
    IS = requests.get(f'https://financialmodelingprep.com/api/v3/income-statement/{company}?period=quarter&apikey={demo}').json()
    BS = requests.get(f'https://financialmodelingprep.com/api/v3/balance-sheet-statement/{company}?period=quarter&apikey={demo}').json()
    CF = requests.get(f'https://financialmodelingprep.com/api/v3/cash-flow-statement/{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']
    EBITDA5YGrowht = (( EBITDATTM - EBITDA5YTTM) / EBITDA5YTTM)*100

    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

  except:
    pass

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. For your reference, and in case you are not familiar with these metrics, see below a few posts where I have covered some of this ratios in detail:

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

all_measures= pd.DataFrame.from_dict(FinMetrics,orient='index')
print(all_measures.head(5))
Industry Stock Comparison with Python
Industry Stock Comparison with Python

Nice, our industry stock analyser tool is showing a very useful overview for each of the metrics by company. This way, we can compare between them.

We have just built a tool for which some websites out there are charging a monthly fee! But let’s make the tool better by having some filters and plotting capabilities.

3. 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)

print(price_earnings)
Python Stock Analyser
Python Stock Analyser

The image above shows our filtered results. It is a bit small but hopefully you are able to replicate the analysis so that you have the Pandas DataFrame in real size.

The most promising company as per the financial ratios and our defined filters seems to be Intel (INTC):

  • Price to Earnings ratio of 7 compared to an industry median of 29.
  • Price to Sales ratio of 2.7 compared to a 5.71 in the industry.

However, when performing some extra analysis, we find out that Intel may not be such a good option to invest in after all:

  • Intel revenue growth in last 4 quarters of 2.8% is significantly lower than that of the industry (4.32%).
  • Also, earnings is showing a negative growth of 14% compared to one year ago.

In addition, by reading the latest financials published last week. We find that Intel is having problems with the new generation chips and that its main competitor, Advanced Micro Devices AMD, seems to be getting some advantage over Intel.

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:

price_earnings[['pe','ps','pb','PEG','ptoFCF']].plot()
price_earnings[['Dividend_Yield','revenue_growht_4qrts']].plot(kind="bar")
Industry Analysis Visualisation
Industry Analysis Visualisation

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.

For example, check what is the value of the stock based on some valuation methods. For that, you can use the DCF method that we build in my previous post.

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. You can use GuruFocus to have a look at companies insider trade or have a look at some of my posts where I show how to get insider trading information using Python.

6 thoughts on “Industry Analysis with Python

  1. Building a Tool to Analyse Industry Stocks with Python”

    Hi
    i love what you are doing keep it up
    Please what and what do i need to install on my windows 10 laptop to get this pyhon script running?
    Thanks in anticipation

    1. Hi,

      Thanks for your comment. Actually, you can try to use google colab through google drive. That is the exact setup that I have for my videos.
      That way you can run the code without having to install Python.

      Hope that helps

  2. Excellent article! It might worth adding F-score too into the picture, and combining the selected metrics into a custom score, order it descending, it helps automatic preselection, of course manual work cannot be spared out.

    Unless we use a sentiment score included API, there are a few nowadays. Of course not the dumb twitter AI ones.

    DCF seems way to complex, I would use MA on profit/margin and extrapolate it one Q.
    That could filter out covid tainted reports.

  3. Hi,
    This is a great article and tips for quick tangible results, thanks a lot for what you’re doing and sharing. I think there’s a small mistake in the first http call with the marketcapmorethan variable name. Looking forward to the next article !
    Thanks

    1. Thanks for following! I am not sure if Nifty stocks is included in the financialmodelingprep. You may want to have a look at the documentation. Then, you can adapt the code accordingly

Comments are closed.