Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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