Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 16, 2020 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/c86ffb808c791e8278257bd62a21e987 to your computer and use it in GitHub Desktop.
Save yuyasugano/c86ffb808c791e8278257bd62a21e987 to your computer and use it in GitHub Desktop.
Pandas sharpe ratio calculation for Apple Inc
# compute sharpe ratio using Pandas rolling and std methods, the trading days is set to 252 days
TRADING_DAYS = 252
returns = np.log(df['Close']/df['Close'].shift(1))
returns.fillna(0, inplace=True)
volatility = returns.rolling(window=TRADING_DAYS).std()*np.sqrt(TRADING_DAYS)
sharpe_ratio = returns.mean()/volatility
sharpe_ratio.tail()
fig = plt.figure(figsize=(15, 7))
ax3 = fig.add_subplot(1, 1, 1)
sharpe_ratio.plot(ax=ax3)
ax3.set_xlabel('Date')
ax3.set_ylabel('Sharpe ratio')
ax3.set_title('Sharpe ratio with the annualized volatility for Apple Inc')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment