Skip to content
Prev 14787 / 15274 Next

[PortfolioAnalytics] optimize.portfolio.rebalancing with changing stock universe

Hi Brian,

Thank you for your response - I was a bit unclear as to what exactly the
rebalancing loop entailed but that helped clear things up. Thank you also
for all the work you've done in developing both PerformanceAnalytics and
PortfolioAnalytics! Both have been essential in the research project I'm
working on at the moment.

I was also not aware of the 'combine.portfolios' and
'combine.optimizations' functions but those look like they are just what I
need!

I have included the rebalancing loop section of my code below if anyone
else needs it before you guys find the time to implement it in the package.

Kind regards
Josh

*Code:*

numWeeks = floor(as.duration(startDate %m+% weeks(52) %--%
last(dates))/dweeks(13))

for (i in 1:numWeeks){
  portf <- add.constraint(portf, type="group",

    #this is checking which stocks have Amihud values above the median
Amihud value at each rebalancing date
                          groups = list(which(weeklyAmihud[paste0(startDate
%m+% weeks(52+13*i))]
                                >as.numeric(mid[paste0(startDate %m+%
weeks(52+13*i))])) ),

    #this is setting their max weight to be 0
                          group_min=c(0),
                          group_max=c(0),
                          group_labels=c("Illiquid"),
                          group_pos=c(1),
                          indexnum=3)

  #will have to increase search_size when running on full dataset
  portfolio.optimized <- optimize.portfolio(R = sjReturns[paste0(startDate
%m+% weeks(13*i),
                                                  "::",
                                                   startDate %m+%
weeks(52+13*i))],
                                            portfolio = portf,
                                            optimize_method = "random",
                                            search_size = 500)
  #get the weights at each rebalacing period and add to dataframe
  extra <-extractWeights(portfolio.optimized)
  rebal_weights <- rbind(rebal_weights,extra)

  #set rowname to equal rebalancing date
  rownames(rebal_weights)[i+1]<-paste0(startDate %m+% weeks(52+13*i))
   }

#change first rowname to first rebalancing date
rownames(rebal_weights)[1]<- paste0(startDate %m+% weeks(52))
dates2 = as_date(rownames(rebal_weights))

rebal_weights <- xts(rebal_weights, order.by=dates2)
rebal_returns <- Return.portfolio(sjReturns, weights=rebal_weights)


On Sun, Aug 4, 2019 at 11:09 PM Brian G. Peterson <brian at braverock.com>
wrote: