3
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ That's a 1 day estimate of volatility, which is fine, but is going to be very "noisy" (i.e. subject to random fluctuations). People usually average over a short period of time (such as 20 days or 120 days, etc.) to get a more stable and well behaved estimator of volatility. May I ask what the purpose of this calculation is ? $\endgroup$ – Alex C Sep 5 '15 at 21:25
  • $\begingroup$ @Alex C This is just an exercise I'm doing to learn, and also to see potential connections between volatility and actual price. If I decide to average over 20 days, does that mean I need to do this calculation every say i+20 until the end of my list (which I think is around 8500 days since I'm getting data from 1984)? $\endgroup$ – Iason Sep 5 '15 at 21:32
2
$\begingroup$

I don't have enough reputations points to comment, so I'll put this into an answer.

I am not sure what this means, "standard deviation for every pair of numbers in my log returns." but it sounds like you are taking the standard deviation of two consecutive returns. If that is the case, I would not do that.

I think you want "realized variance". This is just the sum of squared log returns. You can then take the square root of this sum to get realized volatility. If you sum over a week or month, you get the realized volatility over that week or month.

See the Wikipedia article for the nice mathematical properties of realized variance.

$\endgroup$
  • $\begingroup$ in particular the pandas package does volatility and rolling volatility with relative ease. $\endgroup$ – rhaskett Sep 9 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.