Calculate the Sharpe ratio, a measure of risk-adjusted return that evaluates the excess return of an investment portfolio or asset per unit of risk taken.

The Sharpe ratio is calculated as the difference between the expected return of the asset or portfolio and the risk-free rate of return, divided by the standard deviation of the asset or portfolio’s excess return. It quantifies the amount of return generated for each unit of risk assumed, providing insights into the investment’s performance relative to the risk taken.

The formula is as follows:

\[\text{Sharpe Ratio} = \text{Excess Return} / \text{Excess Standard Deviation}\]

By default one Sharpe ratio is reported per period, computed from the daily excess returns falling inside that period. For a given period, for example monthly, this translates into the following:

\[\text{Sharpe Ratio} = \text{Average Daily Excess Return within the Month} / \text{Standard Deviation of the Daily Excess Returns within the Month}\]

For a rolling period, period instead sets the frequency of the returns themselves and the ratio is computed over a rolling window of rolling such returns:

\[\text{Sharpe Ratio} = \text{Average Rolling Excess Return} / \text{Standard Deviation of Rolling Excess Returns}\]

Note that this is explicitly already subtracts the Risk Free Rate.

The result is not annualized: it is a per-observation Sharpe ratio, so a value computed from daily returns is roughly SQRT(252) smaller than the annualized figure usually quoted in the literature (SQRT(52), SQRT(12) and SQRT(4) for weekly, monthly and quarterly returns respectively). Multiply by that factor before comparing against published annualized Sharpe ratios.

The plain Sharpe ratio only looks at the mean and standard deviation of returns, implicitly assuming Gaussian, i.i.d. returns and ignoring how much uncertainty surrounds the estimate itself. The method parameter selects one of three corrections for that, each keeping the same excess returns and therefore the same underlying Sharpe ratio as its starting point:

  • "adjusted" - the Adjusted Sharpe Ratio (ASR, Pezier & White, 2006) penalizes (or rewards) the Sharpe ratio for negative skewness and excess kurtosis using a Cornish-Fisher-style expansion, so that two strategies with the same Sharpe ratio but different tail shapes are no longer scored identically:
  • ASR = SR * [1 + (S / 6) * SR − ((K − 3) / 24) * SR^2]
  • "probabilistic" - the Probabilistic Sharpe Ratio (PSR) is the probability that the true (population) Sharpe ratio exceeds benchmark_sharpe_ratio, folding the skewness and (non-excess) kurtosis of the underlying returns into the standard error of the Sharpe ratio so that a short, lumpy sample no longer looks more convincing than it is:
  • PSR(SR) = Φ( (SR̂ − SR) · sqrt(n − 1) / sqrt(1 − γ₃·SR̂ + ((γ₄ − 1) / 4)·SR̂²) )
  • "deflated" - the Deflated Sharpe Ratio (DSR) is the Probabilistic Sharpe Ratio corrected for the fact that a reported Sharpe ratio is often the best of many strategy variations, parameter combinations, or lookback windows tried during a backtest (multiple testing / selection bias / “backtest overfitting”). It estimates the Sharpe ratio one would expect to observe purely by chance as the maximum of n_trials independent trials under the null hypothesis of no skill, and uses that expected maximum as the benchmark SR* in the Probabilistic Sharpe Ratio formula instead of a naive benchmark such as 0:
\[\text{SR} \cdot = \sqrt{\text{Var} \left[\text{SR\_trials}\right]} \cdot \left[(1 - \text{γ}) \cdot \operatorname{Phi⁻¹}(1 - 1 / N) + \text{γ} \cdot \operatorname{Phi⁻¹}(1 - 1 / (N \cdot e))\right]\]

Where SR̂ is the observed Sharpe ratio, S (γ₃) is the skewness and K (γ₄) the non-excess (raw) kurtosis of the same returns, n is the number of return observations, N is n_trials, Var[SR_trials] is the variance of the Sharpe ratios observed across those N trials, γ ≈ 0.5772 is the Euler-Mascheroni constant and Φ is the standard normal CDF. Since DSR = PSR(SR*), it is always less than or equal to the Probabilistic Sharpe Ratio computed against a benchmark of 0.

This codebase does not track “N literal strategy trials” - there is no record of how many parameter combinations were tried before arriving at the current Toolkit configuration. As a documented approximation, Var[SR_trials] is estimated from the variance of an auxiliary rolling Sharpe ratio series (see get_rolling_sharpe_ratio) computed over a trials_window-sized window across the full return history, and n_trials defaults to the number of valid (non-NaN) values in that same rolling series. This treats each rolling window as if it were one “trial” - a reasonable proxy for how dispersed the Sharpe ratio could plausibly have been under different choices, but not a substitute for passing the actual number of variations tried (via n_trials) when that is known, since the quality of the correction depends directly on it.

See definition: https://en.wikipedia.org/wiki/Sharpe_ratio

Also known as: risk-adjusted return, reward-to-variability ratio. The variants are also known as the Pezier and White Adjusted Sharpe Ratio (ASR), the Sharpe ratio significance probability (PSR) and the backtest overfitting or selection-bias-adjusted Sharpe ratio (DSR).

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

Calculate the Sharpe Ratio in Python

The Sharpe Ratio is available in the Performance module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_sharpe_ratio as shown below.

from financetoolkit import Toolkit

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

toolkit.performance.get_sharpe_ratio()

Which returns:

Date AAPL TSLA
2021 0.1277 0.1334
2022 -0.0482 -0.0812
2023 0.1189 0.095
2024 0.07 0.0637
2025 0.0188 0.0263
2026 0.0475 -0.0604

And, asking for the probability that these Sharpe ratios are genuine instead:

toolkit.performance.get_sharpe_ratio(method="probabilistic")
Date AAPL TSLA
2021 0.8922 0.9022
2022 0.225 0.0998
2023 0.9684 0.9323
2024 0.8693 0.8496
2025 0.618 0.6618
2026 0.7167 0.2264

Parameters

get_sharpe_ratio accepts the following parameters:

  • period (str, optional): The period to use for the calculation. Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”.
  • rolling (int, optional): The rolling period to use for the calculation. If you select period = ‘monthly’ and set rolling to 12 you obtain the rolling 12-month Sharpe Ratio.
  • method (str, optional): Which Sharpe ratio to calculate, one of “standard”, “adjusted”, “probabilistic” or “deflated”, as described above. Defaults to “standard”.
  • benchmark_sharpe_ratio (float, optional): The hypothesized or benchmark Sharpe ratio (SR*) to test the observed Sharpe ratio against. Only used when method=”probabilistic”. Defaults to 0.0, i.e. testing whether the strategy has any skill at all above doing nothing.
  • trials_window (int, optional): The window size (in units of period) used for the auxiliary rolling Sharpe ratio series that approximates Var[SR_trials] and the default n_trials, see above. Only used when method=”deflated”. Defaults to None, which uses half of the available return history so that enough overlapping windows exist regardless of period or date range.
  • n_trials (int, optional): The number of independent (or effectively independent) strategy variations, parameter combinations, or lookback windows tried before arriving at the reported Sharpe ratio. Only used when method=”deflated”. Defaults to None, which falls back to the number of valid values in the auxiliary rolling Sharpe ratio series described above. Pass this explicitly whenever the actual number of trials is known.
  • 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.

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

Share