[R-SIG-Finance] [PortfolioAnalytics] optimize.portfolio.rebalancing with changing stock universe

Joshua Knipe jo@hu@kn|pe @end|ng |rom gm@||@com
Sun Aug 4 21:56:57 CEST 2019

I am using the PortfolioAnalytics package to run a multi-period
optimisation on a stock universe of just over 100 stocks but I'm running
into some issues. I would like to exclude certain stocks at each
rebalancing date but have not been able to get it working thus far.

<a href="

This thread has a very similar issue and Ross Bennett (the creator of this
awesome package) mentions writing ones own rebalancing loop in order to
achieve an evolving universe of stocks. This sounds like the solution but I
am not sure how to implement that unfortunately.

I have calculated a measure of illiquidity for each stock (the Amihud's
ILLIQ value specifically) and have these in a separate file which I am
reading into R. The measures are weekly in order to match the data I am
using for the returns of the stocks. I have calculated a vector of the
median value of this measure for each week i.e. 160 medians for 160 weeks.

What I would like to do is exclude all the stocks which have a Amihud value
above this median from the rebalancing for that period i.e. make sure they
have 0 weighting if they are not liquid enough.

I tried it initially with a penalty function which would just sum the
weights * Amihud values and then scale that value, but this has the
undesired effect of also penalising stocks below but close to the median

My end goal is to get the returns of certain portfolio strategies (e.g.
mean-var, min-var, equal weight etc) on this data set and then compare how
the returns change when these "illiquid stocks" are excluded at each
rebalancing period.

Any advice would be seriously appreciated! :)


The Amihud values are in the following format (which is the same format as
the price data):

Date.               Stock 1.             Stock 2.                Stock 3.
          Stock 4.           etc

2009-06-05.    1.057348e-06.    5.263602e-07   2.054617e-11.   6.470526e-11

2009-06-12.    1.057346e-06.    5.123452e-07   2.311231e-11.   6.381738e-11

2009-06-19.    1.034941e-06.    5.219238e-07   2.192929e-11.   6.238939e-11


An extract of the code I have thus far:

sjPrices = as.data.frame(read_excel("ALSI_Cleaned_Weekly_Prices.xlsx",
sheet = 1, col_names = T,

                                            na = "", skip = 0))

weeklyAmihud = as.data.frame(read_excel("weeklyAmihud.xlsx", sheet = 1,
col_names = T,

                                              na = "", skip = 0))

#create vector of medians (uses function from matrixStats package)

mid <- rowMedians(as.matrix(weeklyAmihud[, -1]))

dates = as_date(sjPrices[,1])

# remove the first column (of dates)

sjPrices <- sjPrices[, -1]

weeklyAmihud <- weeklyAmihud[, -1]

#create xts objects

sjPrices <- xts(sjPrices, order.by=dates)

weeklyAmihud <- xts(weeklyAmihud, order.by=dates)

sjReturns <- ROC(sjPrices)

#remove first row (0 returns)

sjReturns <- sjReturns[-1,]

#this function does penalise the very illiquid stocks but has other
undesired side-effects

AMIHUD <- function(sjReturns, weights, wAmihud){



portf <- portfolio.spec(colnames(sjReturns))

portf <- add.constraint(portf,type = "weight_sum", min_sum = 0.99, max_sum
= 1.01)

portf <- add.constraint(portf, type="long_only")

portf <- add.objective(portf, type = "risk", name = "StdDev")

portf <- add.objective(portf, type="risk", name="AMIHUD",


opt_rebal <- optimize.portfolio.rebalancing(sjReturns,






                                            trace=TRUE, traceDE=5,

	[[alternative HTML version deleted]]

More information about the R-SIG-Finance mailing list