0

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.

Quant
  • 27
  • 7
  • How are these portfolio weights? The rows don't sum to 1. – WaltS Jul 09 '18 at 13:59
  • @WaltS the weights here were just given as an example, using random numbers. But assuming its an unconstrained portfolio, where I don't need to invest 100% in the portfolio, but might also invest in say, a risk-free asset or something, the rows doesn't need to sum to 1. – Quant Jul 10 '18 at 09:39
  • A long-only portfolio as yours appeared to be would typically sum to 1 so the effect of rebalancing into and out of the risk-free asset would be accounted for.. A long-short portfolio might sum to something else. However, the real point is that your weights don't sum to a constant which implies that funds are either being added to or subtracted from the portfolio at each rebalancing point. Anyway, your explanation that the weights are random numbers explains the post. Thank you. – WaltS Jul 10 '18 at 11:57

2 Answers2

1

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);
}
MSW Data
  • 441
  • 3
  • 8
  • I tried running your suggestion, but for some reason I keep getting an error message " Error in try.xts(x, error = "must be either xts-coercible or timeBased") : must be either xts-coercible or timeBased " – Quant Jul 09 '18 at 14:13
  • Correct - as the error says - it should be an xts object for endpoints to work properly. – MSW Data Jul 09 '18 at 23:43
1

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
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38