I have downloaded historical data for FTSE from 1984 to now. What I would like to do is to graph volatility as a function of time. What I have written is:
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
import math
lines = [line.rstrip('\n') for line in open("Data.txt")]
a = list(range(len(lines)))
adjClose = [float(i) for i in lines]
adjClose.reverse()
dates = [line.rstrip('\n') for line in open("Date.txt")]
dates.reverse()
x = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
dailyVolatility = np.std(np.diff(np.log(adjClose))).round(4)
# Calculate returns
returns = []
for i in range(len(adjClose[1:])):
element = adjClose[i]/adjClose[i-1]
element = math.log(element)
returns.append((element))
returns = returns[1:]
mean_returns = np.mean(returns)
vol = []
for i in range(len(returns)):
element = (returns[i]-mean_returns)**2
element=math.sqrt((element))
vol.append(element)
for i in range(len(vol)):
vol[i]*=math.sqrt(252)
x = x[2:]
plt.plot(x,vol)
plt.show()
So I first load the data and then calculate the log returns and also take the average; moreover, I calculate the standard deviation for every pair of numbers in my log returns. Is my reasoning correct? In this case I haven't averaged at all for the standard deviation formula, since N-1 = 2-1=1.