[R-SIG-Finance] Excessive data needed for volatility{TTR} calculation?
Joshua Ulrich
josh.m.ulrich at gmail.com
Sat May 28 20:16:37 CEST 2011
Hi James,
On Sat, May 28, 2011 at 9:44 AM, J Toll <jctoll at gmail.com> wrote:
> 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))
>
<snip>
>
> 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
>
My last email wasn't very clear; I apologize.
I still agree with your suggestion and plan to use it as a patch. The
first line in my prior email was to illustrate (and convince myself)
that your solution matched the formula here:
http://web.archive.org/web/20081224134043/http://www.sitmo.com/eq/172
And it only matches when NROW(OHLC) == n because your solution
operates on a rolling window and my first line operates on everything.
Try something like this:
n <- 5
R <- cumprod(1+r)
FUN <- function(x) {
r <- ROC(x); n <- NROW(x)
sqrt(252/(n-2)*sum((r-mean(r, na.rm=TRUE))^2, na.rm=TRUE))
}
head(sqrt(N) * runSD(ROC(R), n-1),15)
head(rollapply(R, n, FUN, align="right", fill=NA),15)
n <- 10
head(sqrt(N) * runSD(ROC(R), n-1),15)
head(rollapply(R, n, FUN, align="right", fill=NA),15)
Sorry for the confusion.
Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
More information about the R-SIG-Finance
mailing list