1
$\begingroup$

I ran this code in R

vGK <- volatility(ohlc, n = 10, calc="garman", N = 260, mean0 = FALSE)

and the ten first values appeared like this:

[1]         NA         NA         NA         NA
[5]         NA         NA         NA         NA
[9]         NA

Is it because the TTR package estimates the average over last 10 days (hence the n=10)? And that's why the values are smoothed?

$\endgroup$
2
  • $\begingroup$ You're actually showing the first 9 values, I guess the 10th value is not NA? $\endgroup$
    – Bob Jansen
    Mar 13 '17 at 18:37
  • $\begingroup$ Ah, yes, that is correct. $\endgroup$
    – Neri Kim
    Mar 13 '17 at 18:40
1
$\begingroup$

Use the source Luke:

> library(TTR)
> volatiliy

function (OHLC, n = 10, calc = "close", N = 260, mean0 = FALSE, ...) 
{
    OHLC <- try.xts(OHLC, error = as.matrix)
    calc <- match.arg(calc, c("close", "garman.klass", "parkinson", 
        "rogers.satchell", "gk.yz", "yang.zhang"))

    <snip>

    if (calc == "garman.klass") {
        s <- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2 - 
            (2 * log(2) - 1) * log(OHLC[, 4]/OHLC[, 1])^2, n))
    }

    <snip>

    reclass(s, OHLC)
}

clearly the TTR package is using runSum which seems to be rather complicated on it own. However, we can simply do

> runSum(1:11, n = 10)
NA NA NA NA NA NA NA NA NA 55 65

to see that indeed the first 9 values of runSum called with n = 10 generate 9 NA values. In R those NA's will propagate.

This intuitively makes sense for the reason you suggest in your question.

$\endgroup$
1
  • $\begingroup$ Please don't forget to accept this answer if it answered this question for you. $\endgroup$
    – Bob Jansen
    Mar 14 '17 at 19:27

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.