Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created December 22, 2022 07:36
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 quantra-go-algo/7ee8b5b2a71a42e37f4d9b9173b5f7e9 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/7ee8b5b2a71a42e37f4d9b9173b5f7e9 to your computer and use it in GitHub Desktop.
## Computing Volatility
# Load the required modules and packages
import numpy as np
import pandas as pd
import yfinance as yf
# Pull NIFTY data from Yahoo finance
NIFTY = yf.download('^NSEI',start='2018-6-1', end='2022-6-1')
# Compute the logarithmic returns using the Closing price
NIFTY['Log_Ret'] = np.log(NIFTY['Close'] / NIFTY['Close'].shift(1))
# Compute Volatility using the pandas rolling standard deviation function
NIFTY['Volatility'] = NIFTY['Log_Ret'].rolling(window=252).std() * np.sqrt(252)
print(NIFTY.tail(15))
# Plot the NIFTY Price series and the Volatility
NIFTY[['Close', 'Volatility']].plot(subplots=True, color='blue',figsize=(8, 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment