Risk-adjusted returns: Sharpe, Sortino, Calmar
Formulas at a glance
| Ratio | Calculation | Risk measured |
|---|---|---|
| Sharpe | Mean periodic excess return / sample standard deviation × square root of periods per year | Total return variability |
| Sortino | Mean periodic return above target / downside deviation × square root of periods per year | Shortfall below the target |
| Calmar | Compounded annual return / absolute maximum drawdown | Peak-to-trough loss |
Use equally spaced, net-of-cost simple returns as decimals: 1% is 0.01. Match the target rate to the return frequency. Here, downside deviation is the square root of the mean squared negative shortfall over all observations, not the standard deviation of only losing returns. See downside deviation and Sharpe definitions.
For example, annualised excess return of 12% divided by volatility of 20% gives Sharpe 0.60; annualised return above target of 12% divided by downside deviation of 10% gives Sortino 1.20. CAGR of 15% divided by maximum drawdown of 25% gives Calmar 0.60. These illustrative inputs must use consistent periods and conventions.
The square-root annualisation is a convention that assumes returns are sufficiently uncorrelated; it can mislead with serially correlated returns. The examples return NaN when a denominator is zero rather than presenting an undefined ratio as a good score. Use complete finite input data and a positive periods-per-year value.
Chapter 130's stats work at the trade level. This chapter works at the equity-curve level — the standard ratios for comparing strategies with different return *and* risk profiles on a single number, plus a proper max-drawdown-duration calculation (chapter 84 referenced this without building it — here it is).
import numpy as np
import pandas as pd
def sharpe_ratio(returns: pd.Series, risk_free_rate_annual: float = 0.0, periods_per_year: int = 252) -> float:
target = (1 + risk_free_rate_annual) ** (1 / periods_per_year) - 1
excess_returns = returns - target
volatility = excess_returns.std(ddof=1)
if len(returns) < 2 or not np.isfinite(volatility) or volatility <= 0:
return float("nan")
return excess_returns.mean() / volatility * np.sqrt(periods_per_year)
def sortino_ratio(returns: pd.Series, risk_free_rate_annual: float = 0.0, periods_per_year: int = 252) -> float:
# The chosen risk-free rate is the minimum acceptable return in this example.
target = (1 + risk_free_rate_annual) ** (1 / periods_per_year) - 1
excess_returns = returns - target
downside_deviation = np.sqrt(np.mean(np.minimum(excess_returns.to_numpy(), 0.0) ** 2))
if not np.isfinite(downside_deviation) or downside_deviation <= 0:
return float("nan")
return excess_returns.mean() / downside_deviation * np.sqrt(periods_per_year)
Why Sortino often tells a better story than Sharpe for options-heavy or skewed strategies
Sharpe penalizes *all* volatility equally — including large favorable moves. Sortino only penalizes *downside* volatility. Chapter 127's skew discussion applies directly: a positively-skewed strategy (small losses, occasional big wins — e.g. option buying) can show a misleadingly low Sharpe purely because its upside volatility is large, while Sortino isolates shortfall below the chosen target. Neither ratio captures every tail risk or establishes that a strategy is suitable.
Max drawdown and drawdown duration — the piece chapter 84 deferred
def drawdown_series(equity_curve: pd.Series) -> pd.Series:
running_max = equity_curve.cummax()
return (equity_curve - running_max) / running_max
def max_drawdown_and_duration(equity_curve: pd.Series) -> dict:
dd = drawdown_series(equity_curve)
max_dd = dd.min()
in_drawdown = dd < 0
drawdown_periods = []
start = None
for i, is_dd in enumerate(in_drawdown):
if is_dd and start is None:
start = i
elif not is_dd and start is not None:
drawdown_periods.append(i - start)
start = None
if start is not None:
drawdown_periods.append(len(dd) - start) # drawdown still ongoing at series end
return {
"max_drawdown_pct": max_dd * 100,
"longest_drawdown_duration_bars": max(drawdown_periods) if drawdown_periods else 0,
"avg_drawdown_duration_bars": np.mean(drawdown_periods) if drawdown_periods else 0,
"currently_in_drawdown": in_drawdown.iloc[-1] if len(in_drawdown) else False,
}
This is precisely the metric chapter 98's psychology chapter referenced — "longest recovery: 45 trading days" in that chapter's example plan is exactly longest_drawdown_duration_bars from this function, computed on your actual backtest equity curve, not an assumed number.
Calmar ratio — return relative to max drawdown, not volatility
def calmar_ratio(returns: pd.Series, equity_curve: pd.Series, periods_per_year: int = 252) -> float:
if returns.empty or (returns <= -1).any() or not np.isfinite(returns.to_numpy()).all():
return float("nan")
annualized_return = (1 + returns).prod() ** (periods_per_year / len(returns)) - 1
max_dd = abs(max_drawdown_and_duration(equity_curve)["max_drawdown_pct"] / 100)
return annualized_return / max_dd if max_dd > 0 else float("nan")
Use an equity curve that includes starting capital before the first return; otherwise a first-period loss disappears from drawdown. The curve and returns must cover the same period without external cash flows:
returns = pd.Series([-0.10, 0.05, 0.02])
equity_curve = pd.Series(np.r_[1.0, (1 + returns).cumprod().to_numpy()])
ratio = calmar_ratio(returns, equity_curve)
Calmar uses geometric annual return, not a compounded arithmetic average. See the Calmar ratio reference. Disclose the lookback period when comparing results.
Calmar is particularly relevant for anyone who cares more about "how bad does it get" than "how volatile is it day-to-day" — a meaningful distinction for a trader whose real constraint is psychological tolerance for a large drawdown (chapter 98) rather than a formal volatility budget.
Comparing strategies properly — one table, not cherry-picked single metrics
def full_risk_adjusted_comparison(strategies: dict[str, pd.Series], equity_curves: dict[str, pd.Series]) -> pd.DataFrame:
rows = []
for name, returns in strategies.items():
rows.append({
"strategy": name,
"sharpe": sharpe_ratio(returns),
"sortino": sortino_ratio(returns),
"calmar": calmar_ratio(returns, equity_curves[name]),
**max_drawdown_and_duration(equity_curves[name]),
})
return pd.DataFrame(rows)
Never select a strategy based on one favorable-looking ratio in isolation — a strategy optimized specifically to maximize Sharpe can have an ugly Calmar (a rare but severe drawdown that Sharpe's volatility-based measure underweights) — look at the full table, consistent with chapter 85's broader message about single-number strategy pitches.