The Ulcer Index is a financial metric used to assess the risk and volatility of an investment portfolio or asset. Developed by Peter Martin in the 1980s, the Ulcer Index is particularly useful for evaluating the downside risk and drawdowns associated with investments.

The Ulcer Index differs from traditional volatility measures like standard deviation or variance because it focuses on the depth and duration of drawdowns rather than the dispersion of returns.

The formula is as follows:

Ulcer Index = SQRT(SUM[((Pn - Highest High) / Highest High)^2] / n)

Also known as: UI, drawdown risk.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Ulcer Index (UI) for you. Just ask in plain English.

Calculate the Ulcer Index (UI) in Python

The Ulcer Index (UI) is available in the Risk module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_ulcer_index as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(["AMZN", "TSLA"], api_key="FINANCIAL_MODELING_PREP_KEY")

toolkit.risk.get_ulcer_index()

Which returns:

  AMZN TSLA Benchmark
2012 0.0497 0.0454 0.0234
2013 0.035 0.0829 0.0142
2014 0.0659 0.0746 0.0174
2015 0.0273 0.0624 0.0238
2016 0.0519 0.0799 0.0151
2017 0.0241 0.0616 0.0067
2018 0.0619 0.0892 0.0356
2019 0.0373 0.0839 0.016
2020 0.0536 0.1205 0.0594
2021 0.0427 0.085 0.0136
2022 0.1081 0.1373 0.0492
2023 0.0475 0.0815 0.0186

Parameters

get_ulcer_index accepts the following parameters:

  • period (str, optional): The data frequency for returns (daily, weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”.
  • rolling (int | None, optional): The trailing lookback window used as the high-water mark reference for each day’s drawdown. Pass None for an expanding (since-inception) high-water mark instead – this is what the “Highest High” in the formula above literally refers to; a fixed int window is a common, distinct variant (e.g. a 14-day trailing high) rather than a substitute for “the entire period”. Note that passing rolling= the full length of your return series does NOT give you the since-inception result – pandas only starts producing a rolling value once the full window is filled, so it would silently degenerate to just the final period’s drawdown; use rolling=None instead. Defaults to 14.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
  • growth (bool, optional): Whether to calculate the growth of the UI values over time. Defaults to False.
  • lag (int | list[int], 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.

The Risk module page introduces the module, and the sidebar lists all of its functions.

Share