-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannualized-volatility.py
More file actions
31 lines (26 loc) · 1.04 KB
/
annualized-volatility.py
File metadata and controls
31 lines (26 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# --------------------------------------------------------
# Author: Daniel Xu
# Date: 07/27/2023
# Description: A program that implements annualized volatility calculations
# for tickers AMZN, GOOG, AAPL (for daily volatility)
# Annualized Volatility:
# - SD * sqrt(252), where SD is the standard deviation of the percent
# change series of the Adjusted Close
# --------------------------------------------------------
# Import libraries
import yfinance as yf
import numpy as np
# Download historical data for various stocks
tickers = ['AMZN', 'AAPL', 'GOOG']
stock_data = {}
def annualized_daily_volatility(df):
temp = df.copy()
temp['Returns'] = df['Adj Close'].pct_change()
vty = temp['Returns'].std() * np.sqrt(252)
return vty
for t in tickers:
data = yf.download(t, period='7mo', interval='1d') # 1 day candlestick
data.dropna(how='any', inplace=True)
stock_data[t] = data
for s in stock_data:
print('Annualized volatility by day for {} is {}%.'.format(s, annualized_daily_volatility(stock_data[s]) * 100))