-
-
Save quantra-go-algo/7ee8b5b2a71a42e37f4d9b9173b5f7e9 to your computer and use it in GitHub Desktop.
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
## 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