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

J Toll jctoll at gmail.com
Sat May 28 04:33:56 CEST 2011


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



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)
>



More information about the R-SIG-Finance mailing list