Lesson 13 of 15

Sharpe Ratio via Backtest

Sharpe Ratio

The Sharpe ratio measures risk-adjusted return. A higher Sharpe ratio means more return per unit of risk. It is one of the most widely used metrics for evaluating trading strategies.

Formula

Given a series of daily portfolio values:

  1. Compute daily returns: r[t] = portfolio[t] / portfolio[t-1] - 1
  2. Compute mean excess return: mean_excess = mean(r) - rf / 252 (where rf is the annualized risk-free rate, divided by 252 trading days)
  3. Compute population standard deviation of returns
  4. Annualize: multiply by sqrt(252)
Sharpe = (mean_excess / std) * sqrt(252)

If std == 0, return 0.0.

Task

Implement backtest_sharpe(portfolio_values, rf=0.0) that returns the annualized Sharpe ratio of the portfolio.

Python runtime loading...
Loading...
Click "Run" to execute your code.