Created
September 16, 2020 11:19
-
-
Save yuyasugano/0b43c076e9644c6e2ff79cf612c67bf3 to your computer and use it in GitHub Desktop.
Pandas volatility calculation for Apple Inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# compute volatility 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) | |
fig = plt.figure(figsize=(15, 7)) | |
ax1 = fig.add_subplot(1, 1, 1) | |
volatility.plot(ax=ax1) | |
ax1.set_xlabel('Date') | |
ax1.set_ylabel('Volatility') | |
ax1.set_title('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