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:
- Compute daily returns:
r[t] = portfolio[t] / portfolio[t-1] - 1 - Compute mean excess return:
mean_excess = mean(r) - rf / 252(whererfis the annualized risk-free rate, divided by 252 trading days) - Compute population standard deviation of returns
- 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.