# How to use the Sharpe ratio for risk-adjusted returns

# How to use the Sharpe ratio for risk-adjusted returns

## How to use the Sharpe ratio for risk-adjusted returns

In today’s issue, I’m going to show you how to use the most popular performance metric in investing: the Sharpe ratio.

The Sharpe ratio tells you the “risk-adjusted” return of an investment. In other words, “how much return do you get for every unit of risk you take.” It’s a great way to compare strategies because it normalizes their returns by risk.

If you’re investing or trading, you might want to consider the Sharpe ratio as one of your performance metrics.

## Without considering the Sharpe ratio, you miss important information about the performance.

Here’s an example:

- Stock A returned 20%
- Stock B returned 25%

Which is the better investment?

You’re going to say Stock B because it returned more. What if I told you Stock A has 10% volatility and Stock B has 70% volatility? Then it’s not so clear. The Sharpe ratio takes this into account. A Sharpe ratio over 1 is generally considered “good.”

By the end of this newsletter, you’ll know:

- How to get stock price data
- How to compute the Sharpe ratio
- How to compare two investment options

All using Python.

Here’s how step by step.

## Step 1: Get the data

Start by importing the libraries and getting the data. I like to use yfinance for getting stock data and NumPy for vectorizing outputs.

import yfinance as yf

import numpy as np

With yfinance, you can download data for more than one stock at a time. Grab data for both SPY and AAPL. With yfinance, you can download data for more than one stock at a time. Grab data for both SPY and AAPL.

This gives you a MultiIndex DataFrame. There’s one extra line of code we need to build the returns.

```
1closes = data['Adj Close']
2spy_returns = closes.SPY.pct_change().dropna()
3aapl_returns = closes.AAPL.pct_change().dropna()
```

## Step 2: Compute the Sharpe Ratio

Creating a function for the Sharpe Ratio in Python is straightforward.

```
1def sharpe_ratio(returns, adjustment_factor=0.0):
2 """
3 Determines the Sharpe ratio of a strategy.
4
5 Parameters
6 ----------
7 returns : pd.Series or np.ndarray
8 Daily returns of the strategy, noncumulative.
9 adjustment_factor : int, float
10 Constant daily benchmark return throughout the period.
11
12 Returns
13 -------
14 sharpe_ratio : float
15
16 Note
17 -----
18 See https://en.wikipedia.org/wiki/Sharpe_ratio for more details.
19 """
20 returns_risk_adj = returns - adjustment_factor
21 return (
22 returns_risk_adj.mean() / returns_risk_adj.std()
23 ) * np.sqrt(252)
```

The function takes a series of daily returns and subtracts an adjustment factor. In the original work by William Sharpe, the adjustment factor was the risk-free rate. (Since the risk-free rate has been near 0 for almost a decade, I don’t consider it in the analysis.)

Then, the function divides the mean return by the standard deviation of return. Since the function accepts daily returns, you can annualize it by multiplying by the square root of the number of trading days in the year.

### Step 3: Compare investment options

Compute the Sharpe Ratio for AAPL and SPY.

```
1# sharpe ratio for SPY
2sharpe_ratio(spy_returns)
3
4# sharpe ratio for AAPL
5sharpe_ratio(aapl_returns)
```

The Sharpe ratio for AAPL is higher than for SPY. That means you got more return for every unit of risk and might be considered "better."

It’s always useful to look at performance metrics through time because they are never static. It’s easy with Python.

aapl_returns.rolling(30).apply(sharpe_ratio).plot()

Use a histogram to visualize the distribution of rolling Sharpe ratios. In the case of AAPL, there is a long tail of positive Sharpe ratios.

`1aapl_returns.rolling(30).apply(sharpe_ratio).hist(bins=50)`

Subtract the SPY Sharpe ratio from the AAPL Sharpe ratio and plot the result as a histogram.

```
1(
2 aapl_returns.rolling(30).apply(sharpe_ratio)
3 - spy_returns.rolling(30).apply(sharpe_ratio)
4).hist(bins=50)
```

AAPL does not consistently beat SPY over every 30-day period. However, by the analysis above, you're safe to conclude based on risk-adjusted returns, it was a better investment.