# [R-SIG-Finance] Excessive data needed for volatility{TTR} calculation?

Joshua Ulrich josh.m.ulrich at gmail.com
Sat May 28 05:39:58 CEST 2011

```Hi James,

On Fri, May 27, 2011 at 9:33 PM, J Toll <jctoll at gmail.com> wrote:
> Hi again,
>
> I've been trying to figure out the problem and I believe there is a
> problem with the vectorization in volatility, which results in the
> volatility calculations for the close to close method being
> inaccurate.  I believe the issue is with this part of line 14.
>
> runSum((r - rBar)^2, n - 1)
>
> The first 9 r all have to be differenced against the same rBar, not a
> running sum of rBars.  I believe a better way to accomplish this would
> be:
>
> s <- sqrt(N) * runSD(r, (n -1))
>
>> volatility
> function (OHLC, n = 10, calc = "close", N = 260, ...)
> {
>    OHLC <- try.xts(OHLC, error = as.matrix)
>    calc <- match.arg(calc, c("close", "garman.klass", "parkinson",
>        "rogers.satchell", "gk.yz", "yang.zhang"))
>    if (calc == "close") {
>        if (NCOL(OHLC) == 1) {
>            r <- ROC(OHLC[, 1], 1, ...)
>        }
>        else {
>            r <- ROC(OHLC[, 4], 1, ...)
>        }
>        rBar <- runSum(r, n - 1)/(n - 1)
>        s <- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))       # line 14
>    }
>
> Please let me know if this makes sense to anyone else, or if I'm
> mistaken.  Thanks.
>
> James
>
>
>
Thanks for digging into this.  I've recently received one or two
issue.

I think your solution will work, but using 'n' instead of 'n-1'.  The
code below shows the same results using your solution and a formula
similar to the one found here (which I mis-interpreted when I
originally wrote the function):
http://web.archive.org/web/20081224134043/http://www.sitmo.com/eq/172

set.seed(21)
N <- 260
n <- 100
r <- rnorm(n)/100
last(sqrt(N) * runSD(r, n))
sqrt(N/(n-1)*sum((r-mean(r))^2))

Thanks!
--

> On Fri, May 27, 2011 at 6:52 PM, J Toll <jctoll at gmail.com> wrote:
>> Hi,
>>
>> I have been using the volatility function from the TTR package and I
>> noticed something that I thought was a bit unusual. I expected that I
>> should be able to calculate the default 10-day volatility using the
>> close estimator starting with 10 or maybe 11 days of data.  That's not
>> what I found.  It appears that 18 days of data is necessary to
>> calculate a 10-day volatility.  For example:
>>
>>> getSymbols("SPY")
>> [1] "SPY"
>>> volatility(tail(SPY, 10), n = 10, calc = "close", N = 260)
>> Error in `[.xts`(x, beg:(n + beg - 1)) : subscript out of bounds
>>> volatility(tail(SPY, 11), n = 10, calc = "close", N = 260)
>> Error in `[.xts`(x, beg:(n + beg - 1)) : subscript out of bounds
>>> volatility(tail(SPY, 18), n = 10, calc = "close", N = 260)
>>                 [,1]
>> 2011-05-03         NA
>> 2011-05-04         NA
>> 2011-05-05         NA
>> - edited for brevity -
>> 2011-05-23         NA
>> 2011-05-24         NA
>> 2011-05-25         NA
>> 2011-05-26 0.09481466
>>
>> Stranger still (at least to me), it appears that 38 days worth of data
>> is necessary to start calculating a 20-day volatility.
>>
>>> volatility(tail(SPY, 37), n = 20, calc = "close", N = 260)
>> Error in `[.xts`(x, beg:(n + beg - 1)) : subscript out of bounds
>>> volatility(tail(SPY, 38), n = 20, calc = "close", N = 260)
>>                [,1]
>> 2011-04-04        NA
>> 2011-04-05        NA
>> 2011-04-06        NA
>>  - edited for brevity -
>> 2011-05-23        NA
>> 2011-05-24        NA
>> 2011-05-25        NA
>> 2011-05-26 0.1088309
>>
>> 58 days of data is necessary for a 30-day volatility calculation.
>> From looking at the code for the volatility function, I'm not seeing
>> why so much additional data is needed to calculate the volatility.
>> Does anybody have an idea of why so much additional data is necessary?
>>  Thanks.
>>
>> James
>>
>> R version 2.13.0 (2011-04-13)
>> Copyright (C) 2011 The R Foundation for Statistical Computing
>> ISBN 3-900051-07-0
>> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>>
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

```