Calculate the Value at Risk (VaR) of an investment portfolio or asset’s returns.

Value at Risk (VaR) is a risk management metric that quantifies the maximum potential loss an investment portfolio or asset may experience over a specified time horizon and confidence level. It provides insights into the downside risk associated with an investment and helps investors make informed decisions about risk tolerance.

The VaR is calculated as the quantile of the return distribution, representing the loss threshold that is not expected to be exceeded with a given confidence level (e.g., 5% for alpha=0.05).

Also known as: VaR, maximum expected loss, portfolio loss risk.

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

Calculate the Value at Risk (VaR) in Python

The Value at Risk (VaR) is available in the Risk module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_value_at_risk as shown below.

from financetoolkit import Toolkit

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

toolkit.risk.get_value_at_risk()

Which returns:

  AMZN TSLA
2012 -0.0244 -0.0343
2013 -0.0204 -0.0537
2014 -0.0312 -0.0423
2015 -0.0208 -0.0422
2016 -0.0288 -0.0394
2017 -0.0154 -0.0345
2018 -0.0416 -0.0503
2019 -0.0232 -0.0492
2020 -0.0369 -0.0741
2021 -0.0252 -0.0499
2022 -0.0518 -0.0713
2023 -0.0271 -0.054

Parameters

get_value_at_risk 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”.
  • alpha (float, optional): The confidence level for VaR calculation (e.g., 0.05 for 95% confidence). Defaults to 0.05.
  • within_period (bool, optional): Whether to calculate VaR within the specified period or for the entire period. Thus whether to look at the VaR within a specific year (if period = ‘yearly’) or look at the entirety of all years. Defaults to True.
  • rolling (int, optional): The rolling window size to use for the calculation. If set, VaR is calculated over a rolling window of this many periods across the full return history instead of per period (e.g. a rolling 60-day VaR). Only available for distribution="historic"; see get_var_backtest for a rolling, out-of-sample VaR path under the parametric distributions. Defaults to None.
  • 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 VaR 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.
  • distribution (str): The distribution to use for the VaR calculations (historic, gaussian, cf, cornish-fisher, studentt or evt). Defaults to “historic”. Note that “cf” and “cornish-fisher” both adjust the gaussian quantile for skewness and kurtosis, but “cornish-fisher” uses the more standard Cornish-Fisher expansion (see var_model.get_var_cornish_fisher), while “cf” is kept for backwards compatibility.
  • threshold_percentile (float, optional): Only used when distribution is “evt”. The percentile of losses above which the Generalized Pareto Distribution is fitted. Defaults to 0.95.

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

Share