# How to calculate historical volatility and sharpe ratio in Python

1. How to calculate the annualized volatility with Pandas
2. How to calculate sharpe ratio for the annualized volatility with Pandas

Volatility measures the dispersion of returns over a certain time period. Weekly, monthly, the annualized volatility whatever!! It’s said that the volatility goes higher the security gets riskier because your asset price changes dramatically and fluctuate in either direction during that time period. There are 2 major volatilities types, implied volatility and historical volatility. While implied volatility, as known as projected volatility, is derived using the market price of a traded derivative for estimating the potential of the option in the market, historical volatility is derived from past price data of the security over predetermined priods of time.

It represents the degree of variability in returns of an asset. It can be obtained by calculating a standard deviation over predetermined time period. The famous pattern is the annualized volatility that is a calculation of standard deviation of logarithm annual returns.

Sharpe ratio measures the return of your asset or portfolio by its risk. The ratio is the return earned in excess of the risk-free rate per unit of volatility. We use historical volatility value that we will calculate. To make it simple, I will ignore the risk-free rate in the calculation but you can put that back just by adding a substraction of the risk-free rate for the return of portfolio.

In this blog I’d like to explain how to calculate the annualized volatility (Weekly, monthly, etc are easy enough) with Pandas and calculate its Sharpe ratio with the calculated annualized volatility.

Please retrieve OHLCV data or your asset’s close data at least as a Pandas Series or DataFrame object. Here’s the article I wrote and you can check how to use Pandas Datareader if you’re keen on using stock prices. Datareader supports a lot of other sources also.

How to get cumulative return for your asset and portfolio in Python

I got the variable `df` for Apple Inc OHLCV data. At least you need to have Close price in Pandas Series or DataFrame.

## How to calculate the annualized volatility with Pandas

This is the calculation formula of volatility. In the annualized volatility we use the trading days 252. It seems it’s the custom people are using 252 for the annual trading days.

``````return = logarithm(current closing price / previous closing price)
volatility = std(sum(return)) * sqrt(trading days)``````

Here’s the sample code I ran for Apple Inc.

Here’s the different example to plot every security’s volatility.

## How to calculate sharpe ratio for the annualized volatility with Pandas

We’ve got the annualized volatility so let’s use this volatility value for computing the sharpe ratio next. This is the calculation formula of sharpe ratio.

``````return = logarithm(current closing price / previous closing price)
returns = sum(return)
volatility = std(returns) * sqrt(trading days)
sharpe_ratio = (mean(returns) - risk-free rate) / volatility
``````

Here’s the sample code I ran for Apple Inc.

Here’s the different example to plot every security’s sharpe ratio except ‘JWN’ that is NordStrom.

Here’s the Jupyter notebook of all samples in github for your reference.

How to calculate historical volatility and sharpe ratio in Python