In this post, we are going to build a Python for Finance script in order to perform a due diligence for a desired company. This is particularly useful to automate the financial analysis process performed on a company financials.
Once we have the code ready, we will specify the ticker of the company to analyse and Python will prepare all below analysis:
- Income Statement Growth for each of the last 30 quarters including graph
- Income Statement for the last 35 quarters including revenue and net income graph
- Profitability ratios for all company peers
- Analyse institutional investor transactions on the selected stocks
- Earning surprises analysis for the last 10 quarters
- Sentiment analysis on earning calls
- Price ratios peer comparison
- Overview of latest news related to the selected stock
- Stock price chart provided by TradingView
Python for Finance – Company Due Diligence
As a heads up, our Python script will have multiple functions. To make the article easy to follow, I will share the code at the end of the article and provide a short description for each of the functions during the post. To make things more clear, I will link to some of my other posts on Python for Finance whenever is applicable to share additional details.
If you run the code and you get some error, check that you have installed in your machine all required libraries. I will mention in each of the sections which Python libraries we need to install.
Please note that this post is not offering any financial advise. I am not a financial advisor and I do not intend to share financial advises or recommendations in my posts. In addition, the code provided in this post may not be free of errors. So perform your own diligence before relying on it.
Company Analysis with Python – Requirements
- Python (Python 3.6 or higher recommended)
- We will use Financialmodelingprep API. Feel free to collect the data and replicate the code with any other API. Add your apikey at the beginning of the code.
- Upon running the code, we will proved the ticker of the company to analyse. The given ticker will be the input for the rest of the analysis.
The output of the code will create a master Excel file containing all our financial analysis generated by Python. Each of the financial analysis will be included in a different tab.
Income Statement and Income Statement Growth
Analysing income statements is one of the main tasks when analysing the performance of a company. Income statement analysis is key to understand how profitable a company is and how much sales is generated.
To support our analysis, we will use a Python function, income statement growth function, that will take care of retrieving the last 35 quarterly income statements of the requested company. Then, Python will calculate the growth for each income statement line item. The outcome of the function will be below pandas DataFrame that will be included in a tab of our Excel file together with a graph.
Note that for the Excel file to be generated, the xlsxwriter package needs to be installed in your machine. Run below command in your terminal to have it installed.
In addition, we also need to install Kaleido in order to be able to insert images into the Excel file from Python. Finally, we also require some other standard packages like Pandas and Numpy.
pip install xlsxwriter pip install -U kaleido
In order to have the graphs generated, we use the free Plotly library. Therefore, we need to install Plotly 4.9 or higher otherwise the code will not work. To install Plotly, run below command in your terminal:
pip install plotly==4.14.3
Comparing Profitability Ratios across Peers
The peer function will compute profitability ratios for all peer companies of our selected ticker. The outcome will be a DataFrame containing key financial metrics for all peer companies. In addition, we will plot the metrics into a single chart by company to have a quick insight of company performance.
The outcome of the code will be included in the master Excel file. For more details on what profitability ratios are, feel free to have a look at my previous post.
Institutional Investor Transactions
Our next function will look into the latest institutional investor transactions. Based on the latest transactions, we will get the number of buys vs the number of sells performed by institutional investors over a defined number of days. Please visit my post on analysing institutional investor transactions for more details.
Below is the output that we will get in our master Excel file with a transaction summary for the selected company and peers.
Analysing Company Earning Expectations
The next analysis is key to understand whether or not a company is meeting analyst expectations. The earning surprises function will let us know how many times our selected company has beaten expectations over the last 10 quarters:
Company Earnings Transcript Sentiment Analysis
It is important to know what management thinks about their own company. In order to get a view on it, we can look into company earning calls where management gives an overview of the most recent trends and performance of the company.
This earning transcript function will analyse the whole earnings transcript of the last quarter and compute a sentiment score to let us know whether the sentiment on the company future is positive or negative. For more details on this function, have a look at my post on company earnings analysis with Python.
Note that for the earning transcript sentiment analysis to work, we need to install textblob, a library to process textual data to support our sentiment analysis with Python. We can run below code in our terminal to have it installed:
pip install textblob
Price Ratios Comparison across Peer Companies
The price ratio comparison across peers is my favourite function of the company due diligence script. It takes all peer companies of the selected company and calculates a few price ratios. Below is the outcome expected after running the script. Is it not super useful?
Latest News and Stock price chart
The last two functions will create a new Excel tab including an overview of the latest news for the selected company. Itincludes the title, link and a short overview of the news. This is super useful to have an idea on what is currently going on with the analysed company. Below is an example of the news overview if we run the code for Apple:
And of course, it is also super import to look at the price trend and volume in order to time the opening of a new position in a company. For that, we will run a function that will open in the browser the stock price of the selected company using TradingView.
Running the Code
Once we have all required libraries installed and our api key included within the api_key variable, we should be able to execute the code.
Upon script execution, we are prompted to enter the ticker of the company to analyse, the current year and the current quarter. This information is only use to retrieve the most recent company earning transcripts.
Once the code is finalised, it should generate an Excel file in the same folder where the Python script is saved.
The code has a lot of lines but it really covers a big junk of the company analysis. Hope that you find it useful!