Skip to content

Instantly share code, notes, and snippets.

@timotewb
Created November 9, 2020 09:56
Show Gist options
  • Save timotewb/ac534acb1634b7c7639a79982a8a555d to your computer and use it in GitHub Desktop.
Save timotewb/ac534acb1634b7c7639a79982a8a555d to your computer and use it in GitHub Desktop.
def sharpeRatio(returns, periods, rf=0):
# --- Annualised Sharpe Ratio
# returns: array, percentage change values in decimals
# periods: int, annual periods in returns (e.g. days = 252, months = 12)
# rf: decimal, Risk Free Rate generally zero but can be specified as a decimal
import numpy as np
sr = np.sqrt(periods) * ((np.nanmean(returns) - rf) / np.nanstd(returns))
# --- if value is +-inf then set to Nan
if sr == np.inf or sr == -np.inf:
sr = np.nan
return sr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment