簡體   English   中英

計算R中差價(具有正值和負值)的波動率

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

我正在嘗試使用R中的TTR包和volatility()函數來計算兩個底層證券之間差價的滾動30天波動率。

到目前為止,這是我的代碼的剝離版本(已拉/清理數據,日期匹配等):

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)

現在我得到的錯誤是:

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

非常感謝任何幫助或指導。

volatility率從時間序列的價格計算波動率:它只適用於正數量。 您可以直接使用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