Gross Margin
Calculate the gross margin, a profitability ratio that measures the percentage of revenue that exceeds the cost of goods sold.
The gross margin ratio is calculated by subtracting the cost of goods sold (COGS) from the total revenue and then dividing the result by the total revenue. It represents the portion of revenue that contributes to covering other expenses and generating profit.
The formula is as follows:
\[\text{Gross Margin Ratio} = (\text{Revenue} - \text{Cost of Goods Sold}) / \text{Revenue}\]Also known as: gross profit margin, gross profit percentage.
No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Gross Margin for you. Just ask in plain English.
Calculate the Gross Margin in Python
The Gross Margin is available in the Ratios module of the open-source Finance Toolkit. Install it with:
pip install financetoolkit -U
Then call get_gross_margin as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(["AAPL", "TSLA"], api_key="FINANCIAL_MODELING_PREP_KEY")
gross_margin_ratios = toolkit.ratios.get_gross_margin()
Which returns:
| 2021 | 2022 | 2023 | 2024 | 2025 | |
|---|---|---|---|---|---|
| AAPL | 0.4178 | 0.4331 | 0.4413 | 0.4621 | 0.4691 |
| TSLA | 0.2528 | 0.256 | 0.1825 | 0.1786 | 0.1803 |
Parameters
get_gross_margin accepts the following parameters:
- rounding (int, optional): The number of decimals to round the results to. Defaults to 4.
- growth (bool, optional): Whether to calculate the growth of the ratios. Defaults to False.
- lag (int | str, optional): The lag to use for the growth calculation. Defaults to 1.
- standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.
- trailing (int): Defines whether to select a trailing period. E.g. when selecting 4 with quarterly data, the TTM is calculated.
Related Profitability Ratios
The Ratios module page introduces the module, and the sidebar lists all of its functions.