Analyzing Financial Statements with Python

In this blog post, we’ll explore how to analyze company financials using DuPont Analysis and Python. We’ll retrieve financial data using the AlphaVantage API, parse the data, and calculate key financial ratios to evaluate a company’s performance. To get started, let’s take a look at the code and understand the python for financial analysis and different functions and classes.

					import json
import requests

def pretty_print(data: dict):
    print(json.dumps(data, indent=4))

We begin by importing the necessary libraries, json and requests. The pretty_print function will help us display the JSON data in a more readable format later on.

Retrieving Data from the API

					api_key = 'YOUR KEY HERE'

def retrieve_data(function: str, symbol: str, api_key: str) -> dict:
    Retrieves data from AlphaVantage's open API.
    Documentation located at:
    # query from API
    url = f'{function}&symbol={symbol}&apikey={api_key}'
    response = requests.get(url)
    # read output
    data = response.text
    # parse output
    parsed = json.loads(data)
    return parsed


The retrieve_data function connects to the AlphaVantage API and retrieves financial data based on the given parameters: function, symbol, and api_key. You can find more information on the API and its documentation here.

Getting Latest and Second Latest Balance Sheet and Income Statement

					def get_latest_balance_sheet(symbol: str, api_key: str) -> dict:
    Retrieves latest balance sheet of company specified in symbol.
    balance_sheets = retrieve_data('BALANCE_SHEET', symbol, api_key)['annualReports']
    latest_balance_sheet = balance_sheets[0]
    return latest_balance_sheet

def get_second_latest_balance_sheet(symbol: str, api_key: str) -> dict:
    Retrieves second latest balance sheet of company specified in symbol.
    balance_sheets = retrieve_data('BALANCE_SHEET', symbol, api_key)['annualReports']
    second_latest_balance_sheet = balance_sheets[1]
    return second_latest_balance_sheet

def get_latest_income_statement(symbol: str, api_key: str) -> dict:
    Retrieves latest income statement of company specified in symbol.
    income_statements = retrieve_data('INCOME_STATEMENT', symbol, api_key)['annualReports']
    latest_income_statement = income_statements[0]
    return latest_income_statement


We then create three functions, get_latest_balance_sheet, get_second_latest_balance_sheet, and get_latest_income_statement, to retrieve the latest and second latest balance sheet and income statement for a given company.

Financial Statement Analyzer Class

					class FinancialStatementAnalyzer:
    def __init__(self, balance_sheet: dict, prior_balance_sheet: dict, income_statement: dict):
        self.balance_sheet = balance_sheet
        self.prior_balance_sheet = prior_balance_sheet
        self.income_statement = income_statement

    # ... (methods for calculating net_profit_margin, asset_turnover, equity_multiplier, and roe)


We create a FinancialStatementAnalyzer class to calculate the key financial ratios needed for DuPont Analysis: net profit margin, asset turnover, equity multiplier, and return on equity (ROE).

Performing DuPont Analysis

					import time

stocks = ['ZM', 'FB', 'GOOGL', 'BABA', 'XOM', 'GE', 'LMT', 'LLY', 'PG',]

# simple DuPont analysis
print('DuPont Analysis')
for stock in stocks:
    latest_bs = get_latest_balance_sheet(stock, api_key)
    prior_bs = get_second_latest_balance_sheet(stock, api_key)
    latest_is = get_latest_income_statement(stock, api_key)
    analyzer = FinancialStatementAnalyzer(latest_bs, prior_bs, latest_is)
    # print ROE and DuPont analysis ratios
    print(f'{stock}\tProfit Margin: {analyzer.net_profit_margin()}\tAsset Turnover: {analyzer.asset_turnover()}\t\
    Equity Multiplier: {analyzer.equity_multiplier()}\tROE: {analyzer.roe()}')


Now that we have our FinancialStatementAnalyzer class set up, we can perform DuPont Analysis on a list of stocks. In the code above, we create a list of stock symbols, loop through them, and retrieve the necessary financial data using our previously defined functions. Then, we instantiate a FinancialStatementAnalyzer object for each stock, calculate the financial ratios, and print the results.

Since AlphaVantage API has limitations on the number of requests per minute, we use time.sleep(60) to wait for one minute between requests, ensuring that we stay within the allowed limit.


In this blog post, we demonstrated how to analyze company financials using DuPont Analysis and Python. We used the AlphaVantage API to retrieve financial data, parsed the data, and calculated key financial ratios to evaluate a company’s performance. This approach allows us to quickly and efficiently analyze multiple companies and gain valuable insights into their financial health.

Hire me for your next Project

I can assist you with your financial modeling and quantitative finance projects, leveraging my expertise and experience in the field.

Contact Me

Feel free to reach out to discuss your freelance project needs, and let’s collaborate on bringing your vision to life!