How I built a simple algorithm to analyze a company’s financial health — and how to do it
Tired of reading annual reports? Learn how you can build a simple algorithm to analyze a company’s financial health
I’ve always been told that the best way to decide whether to buy stocks in a certain company was to read annual reports. And that might still be the case.
Reading annual reports might be, for some at least, a tiresome process, and some might just skip right to the pages filled with the financial statements or balance sheets.
So, what if you could build a smarter and more efficient way to extract key information from annual reports?
I’ve previously written articles about how I built a stock recommendation algorithm and a stock price forecasting model using Python with the help of ChatGPT which you can read here:
But now, I thought it was time to try to write a simple algorithm that could help me get insights into a company’s financial health, without the need of reading annual reports.
I decided to write in Python and execute the code in PyCharm.
The first thing needed was to find out how I could extract financial data from companies without the need to e.g. downloading annual reports and uploading these to the algorithm.
So, I did a little research and fell over the page Financial Modelling Prep which holds data about companies’ financial statements. More importantly, they offer an API that can be utilized to automatically extract data from certain companies.
Important to note that the free version of Financial Modelling Prep’s API only contains data about companies listed in the US.
By obtaining the API key, I could slowly begin to write a simple algorithm in Python, getting insights into companies' financial health.
I decided to explore metrics such as liabilities, equity, assets, debt, current ratio, and debt-to-equity ratio to analyze the financial health of companies. These metrics provide a holistic view of a company’s stability, leverage, liquidity, and overall financial well-being.
In the code, the first thing needed is to define your API key and the URL in which information about companies’ financial statements exists:
def financial_health(ticker):
api_key = "Insert_Your_API_Key_Here"
url = f"https://financialmodelingprep.com/api/v3/balance-sheet-statement/{ticker}?apikey={api_key}"
response = requests.get(url)
data = response.json()[0]
As seen in the code above, the data is stored in JSON format.
The next thing I did was to define which variables and numbers I needed from the balance sheet to calculate the metrics mentioned before:
liabilities = data["totalLiabilities"]
equity = data["totalEquity"]
assets = data["totalAssets"]
debt = data["totalDebt"]
current_ratio = data["totalCurrentAssets"] / data["totalCurrentLiabilities"]
debt_to_equity = debt / equity
Then, we can do the simple calculations and add a small text to the output:
if assets > liabilities:
print(f"\n- {ticker} has a strong financial position with assets exceeding liabilities.")
elif assets == liabilities:
print(f"\n- {ticker} has a balanced financial position with assets equal to liabilities.")
else:
print(f"\n- {ticker} has a weak financial position with liabilities exceeding assets.")
if equity > debt:
print(f"- {ticker} has a strong equity position with equity exceeding debt.")
elif equity == debt:
print(f"- {ticker} has a balanced equity position with equity equal to debt.")
else:
print(f"- {ticker} has a weak equity position with debt exceeding equity.")
if current_ratio > 1:
print(f"- {ticker} has a strong current position with current assets exceeding current liabilities.")
elif current_ratio == 1:
print(f"- {ticker} has a balanced current position with current assets equal to current liabilities.")
else:
print(f"- {ticker} has a weak current position with current liabilities exceeding current assets.")
if debt_to_equity < 1:
print(f"- {ticker} has a manageable debt level with debt to equity ratio less than 1.")
else:
print(f"- {ticker} has a high debt level with debt to equity ratio greater than or equal to 1.")
Last thing needed is now to define which stock you want to analyze:
financial_health("Insert_Stock_Ticker")
By inserting “AAPL” we will get the following output:
Financial Health Analysis for AAPL as of 2022-09-24:
- AAPL has a strong financial position with assets exceeding liabilities.
- AAPL has a weak equity position with debt exceeding equity.
- AAPL has a weak current position with current liabilities exceeding current assets.
- AAPL has a high debt level with debt to equity ratio greater than or equal to 1.
And there you have it! You now have a simple algorithm that can help you enhance your decision-making when buying stocks based on a company’s financial health.
I do not think that this should be an alternative or a standalone analysis in contrast to reading annual reports, but it can however help you to quickly get an overview of a company’s financial health.
Feel free to give any feedback or suggestions to optimize the code, or simply state if you have any wishes on what I should try to build next!
You have the full code here:
import requests
def financial_health(ticker):
api_key = "Insert_API_Key_Here"
url = f"https://financialmodelingprep.com/api/v3/balance-sheet-statement/{ticker}?apikey={api_key}"
response = requests.get(url)
data = response.json()[0]
liabilities = data["totalLiabilities"]
equity = data["totalEquity"]
assets = data["totalAssets"]
debt = data["totalDebt"]
current_ratio = data["totalCurrentAssets"] / data["totalCurrentLiabilities"]
debt_to_equity = debt / equity
print(f"Financial Health Analysis for {ticker} as of {data['date']}:")
if assets > liabilities:
print(f"\n- {ticker} has a strong financial position with assets exceeding liabilities.")
elif assets == liabilities:
print(f"\n- {ticker} has a balanced financial position with assets equal to liabilities.")
else:
print(f"\n- {ticker} has a weak financial position with liabilities exceeding assets.")
if equity > debt:
print(f"- {ticker} has a strong equity position with equity exceeding debt.")
elif equity == debt:
print(f"- {ticker} has a balanced equity position with equity equal to debt.")
else:
print(f"- {ticker} has a weak equity position with debt exceeding equity.")
if current_ratio > 1:
print(f"- {ticker} has a strong current position with current assets exceeding current liabilities.")
elif current_ratio == 1:
print(f"- {ticker} has a balanced current position with current assets equal to current liabilities.")
else:
print(f"- {ticker} has a weak current position with current liabilities exceeding current assets.")
if debt_to_equity < 1:
print(f"- {ticker} has a manageable debt level with debt to equity ratio less than 1.")
else:
print(f"- {ticker} has a high debt level with debt to equity ratio greater than or equal to 1.")
financial_health("Insert_Stock_Ticker")