【问题标题】:Calculate the rolling drawdown of returns in R计算 R 中收益的滚动回撤
【发布时间】:2021-01-24 09:35:28
【问题描述】:

XTS 被称为“x”,具有七种货币的每日收益。

x=structure(c(0, 0, -0.00237747604278071, -4.10901316858503e-05, 
-0.00292075554430804, -0.00327746821541597, 0, 0, -0.0024832242245989, 
-7.13774881431206e-05, -0.00288771987280823, -0.00239072530655426, 
0, 0, -0.00238446679144366, -0.00169293890925615, -0.0036441365731773, 
-0.00370471543592221, 0, 0, -0.00244664834224606, 0, -0.00285354593582876, 
-0.00288034192721653, 0, 0, -0.00310375411764707, -0.00146643546387215, 
-0.00427725501167975, -0.00376793265740194, 0, 0, -0.00183281566844917, 
0, -0.00294652695187159, -0.0021703202366018, 0, 0, -0.00243030417112311, 
-0.000372071536924534, -0.00330455837816801, -0.00245257154873846
), class = c("xts", "zoo"), index = structure(c(1297296000, 1297382400, 
1297641600, 1297728000, 1297814400, 1297900800), tzone = "UTC", tclass = "Date"), .Dim = 6:7, .Dimnames = list(
    NULL, c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")))

我使用了 PerformanceAnalytics 函数 Drawdowns 来计算和绘制累积回撤,这很好。但是,我也想要滚动 60 天的提款,并尝试了 rollapply 和 apply.rolling,但没有成功。

谁能推荐一个解决方案?

structure(c(0.00371570839572193, 0.00118836834224599, -0.00237747604278075, 
0.00234195385026738, -0.00287978374331551, -0.000357757593582888, 
0.00265543171122995, 0.000720424064171123, -0.00248322422459893, 
0.00241785080213904, -0.00281654342245989, 0.000498433903743315, 
0.00302572497326203, 0.00161579593582888, -0.00238446679144385, 
0.000693180748663102, -0.00195450652406417, -6.08004278074866e-05, 
0.00387943005347594, 0.00128238518716578, -0.00244664834224599, 
0.00274308647058824, -0.00285354593582888, -2.68726737967914e-05, 
0.00367270588235294, 0.00235053556149733, -0.00310375411764706, 
0.00164241631016043, -0.00281494748663102, 0.000511510213903743, 
0.00330757946524064, 0.00212843374331551, -0.0018328156684492, 
0.00260363310160428, -0.00294652695187166, 0.000778500588235294, 
0.00273476534759358, 0.000962438395721925, -0.00243030417112299, 
0.00206324695187166, -0.00293357834224599, 0.000854811604278075
), class = c("xts", "zoo"), index = structure(c(1297296000, 1297382400, 
1297641600, 1297728000, 1297814400, 1297900800), tzone = "UTC", tclass = "Date"), .Dim = 6:7, .Dimnames = list(
    NULL, c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")))

rollapply(x, width = 60, FUN = Drawdowns)

数组错误(ans, c(len.a%/%d2, d.ans), if (!is.null(names(dn.ans)) || : 'dimnames' [1] 的长度不等于数组范围

apply.rolling(x, width = 60, FUN = SharpeRatio)

rbind(calcs, calc) 中的错误: 矩阵的列数必须匹配(参见参数 2)

【问题讨论】:

  • 请直接发布输出并使用dput(x)而不是发布图片。
  • 您好,感谢您的建议。希望会更好吗?
  • 如果您在 Rstudio 中输入 dput(head(x)) 然后将输出发布到您的问题中,它将帮助人们重现您的问题并为您提供更好的答案。
  • 谢谢 不知道你能做到。

标签: r finance r-portfolioanalytics


【解决方案1】:

以下是使用 xts 和 PerformanceAnalytics 包的建议: 我创建了一个带有模拟返回的 xts 对象,并应用了 zoo 包(加载了 xts)中的 rollapply 函数,宽度为 25 天。

测试者

library(xts)
library(PerformanceAnalytics)

set.seed(123)
returns <- matrix(rnorm(n = 365*7, mean = 0.001, sd = 0.002), ncol = 7)
timeindex <- seq.Date(as.Date('2019-01-01'), to = as.Date('2019-12-31'), by = 'days')
test_xts <- xts(x = returns,
                order.by = timeindex)
colnames(test_xts) <- c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")

rolling_maxDD <- rollapply(test_xts, width = 25, FUN = maxDrawdown)

plot.xts(-rolling_maxDD, legend.loc = 'bottomleft')

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2014-01-30
    • 2022-01-13
    • 1970-01-01
    • 2015-01-28
    • 2017-11-13
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多