Question

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.

No correct solution

OTHER TIPS

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 )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top