1

I am trying to using the TTR package and volatility() function in R to calculate the rolling 30 day volatility of a spread between two underlyings.

Here is a stripped version of my code so far (already pulled/cleaned data, date matched, etc.):

asset1 <-c(rnorm(100, mean=50))
asset2 <-c(rnorm(100, mean=50))
spread <-c(asset1-asset2)
vClose.spread <-volatility(spread, n=30, calc="close", N=252)

Now the error I get here is:

Error in runCov(x, x, n, use = "all.obs", sample = sample, cumulative) : 
  Series contain non-leading NAs
In addition: Warning message:
In log(x) : NaNs produced

Any assistance or direction is greatly appreciated.

Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
Evan Scott
  • 11
  • 1
  • 2
  • shot in the dark here, but try `vClose.spread <-volatility(spread, n=30, calc="close", N=252, type="continuous")` – qwwqwwq May 22 '13 at 04:54

1 Answers1

5

volatility computes the volatility from a time series of prices: it will only work for positive quantities. You can use runSD directly.

# Standard deviation of the spread
sqrt(252) * runSD( spread, 30 )
# Standard deviation of the change in spread
sqrt(252) * runSD( diff(spread), 30 )
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
  • Thank you, @Vincent . I knew volatility computed from a time series, I had not previously realized it wouldn't handle 0 or negative prices, however. To get the annualized volatility, I changed your second equation slightly to this: `x2<-(sqrt(1/252))*runSD(diff(spreadb12,30))*100` which seems to give results consistent with what I have seen published in the market. Do you have any thoughts on that? Thanks again for your help. – Evan Scott May 22 '13 at 20:07
  • Dividing by `sqrt(252)` does not look right: the daily volatility (computed by `runSD`) should be lower than the annualized volatility. It may be a typo in your comment, but `30` should not be inside `diff`: it is the argument of `runSD`. – Vincent Zoonekynd May 22 '13 at 21:02
  • You are right. What is appearing strange in my dataset though is the following: When looking at my dataset and examining daily vol, 30 day vol, and 90 day realized vol, I have the following results: 90d realized has the highest mean & median vol followed by 30d and then daily. I wouldn't expect this from a normal distribution. SD and range are far greater for the daily vs the 30d and 90d (which makes sense intuitively). Am I correct in my interpretation that the data likely has extreme kurtosis that becomes muted over the longer time frames? That's the only way I can imagine such a result. – Evan Scott May 23 '13 at 21:00