简体   繁体   中英

Rebalancing portfolio with given weights

Given that I have monthly weights to use in a portfolio, but I want to rebalance my portfolio say, quarterly. How may I create a function that subtracts the weights only each quarter from my original monthly weights, and then repeat those weights until the next quarter etc? I have tried using the Return.portfolio and the Return.rebalancing from the PerformanceAnalytics package, but it doesn't seem to give me the correct answer.

Say the original weights looks like this:

               [,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

Stock 1 should yield the following result:

              [,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

And similar for stock 2,3, and 4. I would like to store all the results in a new element, for instance weights.new .

how about below?

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);
}

If your original weights are stored in weight.mat , you can do the following

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

To make it clear what's happening, 1 + (ceiling((1:12)/3) - 1)*3 returns

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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