Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Last active March 5, 2024 11:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save quantra-go-algo/2057ee9eace3a0ab41e951bddf784ed9 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/2057ee9eace3a0ab41e951bddf784ed9 to your computer and use it in GitHub Desktop.
Sharpe Ratio - Sharpe Ratio in Python
# Load the required modules and packages
import numpy as np
import pandas as pd
import yfinance as yf
# Pull NIFTY data from Yahoo finance
NIFTY = yf.download('^NSEI','2012-01-01', '2016-01-01')
# Compute the logarithmic returns using the closing price
returns = np.log(NIFTY['Close'] / NIFTY['Close'].shift(1))
volatility = returns.std() * np.sqrt(252)
sharpe_ratio = ((returns.mean()*252) - 0.05) / volatility
sharpe_ratio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment