[R-SIG-Finance] Excessive data needed for volatility{TTR} calculation?
J Toll
jctoll at gmail.com
Sat May 28 16:44:56 CEST 2011
Joshua,
On Sat, May 28, 2011 at 7:13 AM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> Actually, because the first return in the moving window would always
> be NA, it should be:
> sqrt(N/(n-2)*sum((r[-1]-mean(r[-1]))^2))
>
> which yields the same result as:
> last(sqrt(N) * runSD(r, n-1))
I've been trying both lines of code and unfortunately I'm not getting
the same results. The first line seems to only work properly for me
in those instances when NCOL(OHLC) = n. For the more common situation
where NCOL(OHLC) > n, you would want a rolling window of vol
calculations. I'm still thinking that the code should be:
s <- sqrt(N) * runSD(r, (n - 1))
As a frame of reference, I believe the output should be:
> getSymbols("SPY")
> tail(volatility(SPY))
[,1]
2011-05-20 0.1206382
2011-05-23 0.1181380
2011-05-24 0.1095445
2011-05-25 0.1069024
2011-05-26 0.1068434
2011-05-27 0.1038008
I've manually calculated the value for 2011-05-27 using a spreadsheet
to confirm the value. I believe the other values to be correct also.
> After getting some sleep, it's clear that your initial solution (n-1)
> is correct.
>
> Your patch will be on R-forge shortly. Many thanks again!
>
> Best,
> --
> Joshua Ulrich | FOSS Trading: www.fosstrading.com
You may want to hold off on a patch in the short term. I still think
there might be an error in there. I'm sorry to be such a nuisance
about this, but thanks so much for your help.
James
More information about the R-SIG-Finance
mailing list