How to compute the Sharpe Ratio using Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
The Sharpe ratio is a metric used by investors to evaluate the return of an investment compared to its risk.
The higher the Sharpe the better the return is compared to its risk.
The Sharpe ratio formula is given by:
Where
So there are a few steps before we are able to compute the Sharpe Ratio.
- Compute the portfolio returns
- Get the risk-free rate
- Compute the standard deviation of the excess returns
How to compute the portfolio returns
Usually, the portfolio returns are computed from an initial portfolio value and the portfolio value at date.
Here is the formula
Let me illustrate it using an example
If you invested 1,000 USD a year ago, but today this invested amount is worth 10,000 USD you would have had a return of ~900% since inception.
How to get the risk free rate
The risk-free rate is the return rate that you would normally get if you were to invest in the least risky asset on the market. What is most often used as a reference is the T-bonds rate. But as today's rates are extremely low we usually take 0% (you could put 1% but IMO it is still a bit too optimistic).
How to compute the standard deviation of the excess returns
The excess returns are the portfolio returns minus the risk-free rate.
Since we are taking 0% as risk-free rate, our standard deviation will be the standard deviation of our portfolio's returns.
Here is the formula of the standard deviation
As you can see the standard deviation is the squared root of the variance.
Furthermore, our X_i here will be the portfolio's return at date i.
In finance the standard deviation is a proxy for volatility, so to say how much our portfolio varies over time. Remember, in investing you often aim for a portfolio with minimal variance and maximum returns.
This is because investors usually don't like when their portfolio drops 40% during the night.
So how do we code this standard deviation?
We will need the evolution of the portfolio returns over time to get its standard deviation.
Let me show you using an example
The code
For this example, we will compute the Sharpe ratio of one Apple stock bought on the 2nd of January 2020.
We use yfinance to fetch Apple stocks data from Yahoo.
We simulate the performance of the stock as if it was held onto until today.
Here you are! You now know how to compute the Sharpe Ratio using Python!
More on financial analysis
If you want to know more about Financial Analysis in Python and avoid the headaches... check out the other articles I wrote by clicking just here:
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚