PerformanceAnalytics沙箱

时间:2017-09-12 09:46:00

标签: r xts finance quantmod performanceanalytics

我一直在努力计算我的投资组合回报和个人股票贡献。我偶然发现了this post,这似乎来自帮助撰写PerformanceAnalytics的人。

在文章的最后,他为某些功能发布了link to r-forge with a sandbox file

所以我试图通过to.monthly.contributions()函数将我的每日回报转换为每月总回报,但我遇到了xts错误!

这是我的代码:

library(PerformanceAnalytics)
library(quantmod)

stock.weights <- c(.15, .20, .25, .225, .175)
symbols <- c("GOOG", "AMZN", "BA", "FB", "AAPL")
getSymbols(symbols, src = 'google', from = "2016")
#xts with daily closing of each stock
merged.closing <- merge(GOOG[,4], AMZN[,4], BA[,4], FB[,4], AAPL[,4])
#xts with returns
merged.return <- na.omit(Return.calculate(merged.closing))
# weighted returns rebalanced quartely
portfolio.returns = Return.portfolio(merged.return, weights = stock.weights,
                                     rebalance_on = "quarters", verbose = TRUE)
#to monthly contributions function
to.monthly.contributions(portfolio.returns$contributions)

但是当我运行最后一行时,我收到以下错误消息:

Error in inherits(x, "xts") : 
  argument "Contributions" is missing, with no default 
5. inherits(x, "xts") 
4. is.xts(x) 
3. checkData(Contributions) 
2. to.period.contributions(contributions = contributions, period = "months") 
1. to.monthly.contributions(portfolio.returns$contributions) 

我猜这个错误与portfolio.returns$contributions不是xts有关?但我不确定如何解决这个问题。

另一方面,如果有人有任何更好的想法或来源来计算投资组合回报的数月/季度/年,我很想听,他们需要考虑到重量变化,重新平衡和贡献。变化!

1 个答案:

答案 0 :(得分:1)

请注意,PerformanceAnalytics(以及该R-Forge存储库中的许多其他软件包)已移至Brian Peterson's GitHub account。大约一年前,您会看到sandbox/to.period.contributions.R的一些变化。这可能会给你带来一些问题。

另一个问题是Return.portfolio()返回的对象没有contributions元素。您想要的元素名称是contribution(单数)。

解决这两个问题后,您的to.monthly.contributions()电话有效。

R> to.monthly.contributions(portfolio.returns$contribution)
              GOOG.Close    AMZN.Close     BA.Close      FB.Close    AAPL.Close Portfolio Return
2016-01-29  0.0002244419 -0.0156956938 -0.036245552  0.0219893367 -1.330565e-02     -0.043033115
2016-02-29 -0.0095461956 -0.0113127380 -0.003625779 -0.0121676134 -1.128288e-03     -0.037780614
2016-03-31  0.0103601952  0.0140210157  0.016927654  0.0171632715  2.218899e-02      0.080661130
2016-04-29 -0.0104584200  0.0222188532  0.015479754  0.0068624014 -2.448619e-02      0.009616397
2016-05-31  0.0085179936  0.0210895602 -0.016873347  0.0024024015  9.732993e-03      0.024869602
2016-06-30 -0.0084883795 -0.0023345382  0.007080427 -0.0086331655 -6.610526e-03     -0.018986182
2016-07-29  0.0166211530  0.0120706520  0.007295757  0.0190190760  1.576098e-02      0.070767622
2016-08-31 -0.0003521895  0.0027014233 -0.007568643  0.0040084230  3.231073e-03      0.002020086
2016-09-30  0.0020684771  0.0177517727  0.004108611  0.0039452914  1.185750e-02      0.039731657
2016-10-31  0.0013990917 -0.0113434690  0.020286170  0.0047711858  7.585139e-04      0.015871492
2016-11-30 -0.0050340240 -0.0092287866  0.015187074 -0.0217047070 -4.601884e-03     -0.025382327
2016-12-30  0.0026858660 -0.0001688763  0.009813394 -0.0059705490  8.286484e-03      0.014646319
2017-01-31  0.0048528154  0.0196327363  0.012429342  0.0298631030  8.355638e-03      0.075133635
2017-02-28  0.0047757941  0.0053484794  0.025108019  0.0094951963  2.198006e-02      0.066707545
2017-03-31  0.0010760715  0.0096512663 -0.004718775  0.0111011780  8.787645e-03      0.025897386
2017-04-28  0.0138145523  0.0086741715  0.011265973  0.0129883844 -1.218154e-05      0.046730900
2017-05-31  0.0101747490  0.0150069699  0.003781232  0.0018310137  1.060194e-02      0.041395909
2017-06-30 -0.0093108126 -0.0055092033  0.013123207 -0.0006974798 -9.767034e-03     -0.012161323
2017-07-31  0.0035934766  0.0040867769  0.056523388  0.0272271162  5.723163e-03      0.097153921
2017-08-31  0.0013284632 -0.0013521084 -0.003226369  0.0036945746  1.691168e-02      0.017356239
2017-09-19 -0.0025908954 -0.0019880088  0.014497492  0.0007343197 -5.737005e-03      0.004915902