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:

Parameters

get_d_ratio accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe 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_periodboolWhether 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
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
growthboolWhether to calculate the growth of the ratios. Defaults to False.True
lagint | list[int]The lag to use for the growth calculation. Defaults to 1.4
standardizeboolWhether 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_columnsstr | list[str] | NoneThe 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
Share