The article provides a comprehensive guide on using Python to perform fundamental analysis of a company's stock by calculating and interpreting its gross profit margin.
Abstract
The article details a methodical approach to evaluating a company's financial health through the lens of its gross profit margin, a key indicator of profitability. Utilizing Python programming language, the author demonstrates how to extract financial data from the Financial Modelling Prep API, preprocess the data, and apply the gross profit margin formula. The analysis includes visualizing revenue and gross profit margin trends over a decade, which can reveal insights into a company's cost management and pricing strategies. The author emphasizes the importance of considering gross profit margin in conjunction with other financial metrics for a holistic view of a company's performance, noting that a consistent gross profit margin above 40% may indicate a durable competitive advantage.
Opinions
The author believes that gross profit margin is a critical metric for understanding a company's efficiency in managing production costs and generating profit from core operations.
Consistent gross profit margins around 60% are seen as indicative of a company with a durable competitive advantage, as exemplified by Coca Cola.
The article suggests that while gross profit margin is valuable, it should be analyzed alongside other financial ratios and factors for a comprehensive understanding of a company's financial health.
The author provides a disclaimer that the article's purpose is educational, aiming to teach Python users how to estimate gross profit margin, and does not serve as investment advice.
The author encourages readers to support their work by subscribing to Medium or buying them a coffee via Ko-fi, indicating a preference for direct reader support over traditional advertising models.
The use of affiliate links for Financial Modelling Prep and the mention of a discount code suggest the author's endorsement of these services for readers interested in financial analysis.
Fundamental Analysis of Stock using Python — Gross Profit Margin
Gross profit margin is a financial metric that measures the profitability of a company’s core business operations by analyzing the proportion of revenue that exceeds the cost of goods sold (COGS) or cost of revenue. It is a significant metric in stock analysis as it provides insights into a company’s ability to generate profit from its core business operations. It helps investors and analysts understand how efficiently a company manages its production costs and pricing strategies.
In this article, we are going to use Python to perform fundamental analysis of a target stock by evaluating its gross profit margin.
Disclaimer: The writing of this article is only aimed at demonstrating the steps to estimate gross profit margin of a stock in Python. It doesn’t serve any purpose of promoting any stock or giving any specific investment advice.
The original full source code presented in this article is available on my GitHub Repo. Feel free to download it (GrossProfitMargin.ipynb) if you wish to use it to follow my article.
Estimating Gross Profit Margin in Python
1. Stock Data Acquisition
The financial data used in this article is acquired from Financial Modelling Prep. The Financial Modelling Prep not only offers current stock pricing data but also a wide range of fundamental research data that include company income statement, balance sheet statement, cash flow statement, etc.
We need an API Key before we can acquire the stock data from Financial Modelling Prep. We can sign up an API key from this official page.
(You can start using FMP with a 50% discount for the first two months by sending an email to [email protected] with promo code BGT50)
Image Prepared by the Author
After signing up a Financial Modelling Prep API Key, we can now proceed to use Python to acquire the financial data to estimate gross profit margin.
Line 1–5: Import all the required Python libraries and suppress the non-essential module warning message.
Line 7–10: We need to establish a request URL to acquire the financial data from Financial Modelling Prep. A request URL consists of several important elements:
base_url — It is an end point offered by Financial Modelling Prep to fetch the income statement data that cover the financial info of a company such as revenue, cost of revenue, gross profit, operating expenses, interest income, etc.
ticker — Target stock symbol (e.g. KO)
limit — Maximum number of records returned by API. If we set a limit to 10, this means the API will return a maximum of 10 years of income statements of a target stock.
API_KEY — Our registered Financial Modelling Prep API key
Line 12–14: Next, we join the base_url, ticker, limit and API_KEY into a single string to form a request URL. We then use the request URL to perform the data request from Financial Modelling Prep and store the returned financial data in JSON format.
Image Prepared by the Author
2. Data Preprocessing
Prior to estimating the gross profit margin, we need to perform data preprocessing on the raw financial data acquired from Financial Modelling Prep.
Line 1: Firstly, we convert the financial data from JSON format to Pandas dataframe format.
Image Prepared by the Author
Line 2: There are a lot of income statement info which are irrelevant to gross profit margin calculation. Here, we just extract several target info (e.g. “date”, “revenue”, “costOfRevenue” and “grossProfit”) which are required to estimate gross profit margin and store them in a new dataframe (df_target).
Image Prepared by the Author
3. Applying formula to calculate Gross Profit Margin
Line 1: We apply the formula to calculate the gross profit margin of the target stock over the past 10 years and assign the results to a new column “grossProfitMargin” in the dataframe.
Image Prepared by the Author
4. Fundamental Analysis
Firstly, let us try to plot a bar chart to observe the revenue generated by a target company over the past 10 years.
Line 1–2: Use Plotlybar() method to create a stacked bar chart to show the revenue of a target company.
Image Prepared by the Author
The stacked bar chart shows that the revenue of each year which is composed of gross profit and cost of revenue. The examination of gross profit and cost of revenue of a company at the same time enables us to grasp some ideas about how much a company spend to earn the gross profit every year. The stacked bar chart above shows that Coca Cola has consistently maintain the higher gross profit than the cost of revenue over the past ten years.
To enable a better analysis of the company, we can plot another line chart to show the gross profit margin of the company over the past ten years.
Line 1–2: Use the Plotly line() method to create a line chart to display the gross profit margin of the target company over the past ten years.
Image Prepared by the Author
As shown in the line chart above, the gross profit margin of Coca Cola has consistently been maintained at around 60 percent since 2014 till to date. As a general rule, company with gross profit margins of 40% or better tend to be companies with some sort of durable competitive advantage. Coca Cola has obviously met the expectation from this aspect.
Conclusions
Gross profit margin indicates how efficiently a company can produce and sell its products or services. A higher margin suggests that the company is effectively controlling its production costs and generating more profit from its core operations. We can reuse the same Python script above to test with several other stocks and perform the similar fundamental analysis.
While the gross profit margin is a valuable metric, it is essential to consider it in conjunction with other financial ratios and factors to gain a comprehensive understanding of a company’s overall financial health and performance. Additionally, the interpretation of gross profit margins can vary significantly across industries, so context is vital when making comparisons.
I wish you enjoy reading this article.
Subscribe to Medium
If you like my article and would like to read more similar articles from me or other authors, feel free to subscribe to Medium. Your subscription fee will partially go to me.