d Ratio
Calculate the d ratio of an investment portfolio or asset’s returns, the size of its losses relative to its gains, each weighted by how often they occur.
The formula is as follows:
\[d \;\text{Ratio} = \frac{n_{d} \cdot \sum(\left|\text{Returns}\right| \;\text{where Returns} < 0)}{n_{u} \cdot \sum(\text{Returns where Returns} > 0)}\]Where \(n_{d}\) is the number of negative and \(n_{u}\) the number of positive daily returns within the period. It is the inverse of the gain-loss (Bernardo-Ledoit) ratio weighted by the frequencies of the losses and gains. Lower is better: 0 means there was no loss at all, and a value above 1 means the losses weigh more than the gains.
Example · KO, PEP, MDLZ · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_d_ratio:
from financetoolkit import Toolkit
toolkit = Toolkit(
["KO", "PEP", "MDLZ"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.performance.get_d_ratio()
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
What was the d ratio, the size of losses against gains, of Coca-Cola, PepsiCo and Mondelez in each year from 2021 to 2025?
Which returns:
| Date | KO | PEP | MDLZ |
|---|---|---|---|
| 2021 | 0.6675 | 0.5798 | 0.6805 |
| 2022 | 0.7072 | 0.8231 | 0.8447 |
| 2023 | 0.9899 | 1.0095 | 0.7533 |
| 2024 | 0.7724 | 1.1237 | 1.3704 |
| 2025 | 0.8631 | 1.0868 | 1.0380 |
5 rows
Parameters
get_d_ratio accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The period to use for the calculation. Options are: “daily”, “weekly”, “monthly”, “quarterly”, “yearly”. Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”. | "weekly" |
within_period | bool | Whether to calculate the ratio from the daily returns within each period or from the period returns of the entirety of all periods, one value per ticker. Defaults to True. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 2 |
growth | bool | Whether to calculate the growth of the ratios. Defaults to False. | True |
lag | int | list[int] | The lag to use for the growth calculation. Defaults to 1. | 4 |
standardize | bool | Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False. | True |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
References
- Paper Bacon, C.R. (2008). “Practical Portfolio Performance Measurement and Attribution.” 2nd edition, John Wiley & Sons, p. 95.
- Library Referenced against PerformanceAnalytics (R)’s DRatio