简体   繁体   English

计算R中差价(具有正值和负值)的波动率

[英]Calculating volatility of a spread (with positive and negative values) in R

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. 我正在尝试使用R中的TTR包和volatility()函数来计算两个底层证券之间差价的滚动30天波动率。

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. 非常感谢任何帮助或指导。

volatility computes the volatility from a time series of prices: it will only work for positive quantities. volatility率从时间序列的价格计算波动率:它只适用于正数量。 You can use runSD directly. 您可以直接使用runSD

# Standard deviation of the spread
sqrt(252) * runSD( spread, 30 )
# Standard deviation of the change in spread
sqrt(252) * runSD( diff(spread), 30 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM