给定权重的投资组合重新平衡

时间:2018-07-09 12:52:26

标签: r portfolio rebalancing

鉴于我每月有权重可用于投资组合,但我想按季度重新平衡我的投资组合。如何创建仅从原始每月权重中减去每个季度的权重,然后重复这些权重直到下一个季度等的函数?我尝试使用Return.portfolio包中的Return.rebalancingPerformanceAnalytics,但似乎并没有给我正确的答案。

说原始权重看起来像这样:

               [,1]       [,2]      [,3]      [,4]
January   0.5934314 0.40594301 0.1017005 0.5273729
February  0.4186024 0.43438567 0.2401071 0.1998037
March     0.4916238 0.34787895 0.5021476 0.5630176
April     0.1722450 0.03804423 0.3836163 0.3663108
May       0.4119517 0.32062497 0.1087187 0.4715353
June      0.1319934 0.09609216 0.4827495 0.2007550
July      0.1748113 0.36587410 0.2160457 0.1891824
August    0.5924169 0.26085346 0.3804973 0.4542487
September 0.3178340 0.40817036 0.1026307 0.5350073
October   0.1029935 0.51396102 0.2648184 0.3430611
November  0.3668116 0.42736210 0.2782707 0.5204025
December  0.1523560 0.06694210 0.2345268 0.1135560

股票1应该会产生以下结果:

              [,1]
January   0.5934314
February  0.5934314
March     0.5934314
April     0.1722450
May       0.1722450
June      0.1722450
July      0.1748113
August    0.1748113
September 0.1748113
October   0.1029935
November  0.1029935
December  0.1029935

与股票2、3和4相似。我想将所有结果存储在一个新元素中,例如weights.new

2 个答案:

答案 0 :(得分:1)

下面怎么样?

library(PerformanceAnalytics);

RebalanceQtr = function(Port.Wgt.xts) {

# Get Qtr endpoints
Port.Qtr.Dts = endpoints(Port.Wgt.xts,on="quarter");

# Get Qtrly Portflio
Port.Qtr = Port.Wgt.xts[Port.Qtr.Dts];

# Merge with Original Portfolio
Port.new = merge(Port.Wgt.xts,Port.Qtr);

# Change colname
colnames(Port.new) = c("Wgt","Wgt.New");

# LOCF
Port.new[,"Wgt.New"] = na.locf(Port.new$"Wgt.New");

return(Port.new);
}

答案 1 :(得分:1)

如果您的原始重量存储在weight.mat中,则可以执行以下操作

new.weights <- weight.mat[1 + (ceiling((1:12)/3) - 1)*3,]

为清楚说明正在发生的事情,1 + (ceiling((1:12)/3) - 1)*3返回了

 # [1]  1  1  1  4  4  4  7  7  7 10 10 10