Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 16, 2020 11:19
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/0b43c076e9644c6e2ff79cf612c67bf3 to your computer and use it in GitHub Desktop.
Save yuyasugano/0b43c076e9644c6e2ff79cf612c67bf3 to your computer and use it in GitHub Desktop.
Pandas volatility calculation for Apple Inc
# 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