Generate random portfolios whose turnover from the existing portfolio is in a given range.
This presumes that you can do basic random portfolio generation. For example, that you have mastered “Very simple long-only”.
- Portfolio Probe
You need to have the Portfolio Probe package loaded into your R session:
If you don’t have Portfolio Probe, see “Demo or Buy”.
Doing the example
- The objects:
curPortfolthat are defined in several examples, such as in “Passive, no benchmark (minimum variance)”
We generate 1000 random portfolios with a limit on turnover (buys plus sells):
rpTurnover <- random.portfolio(1000, priceVector, long.only=TRUE, gross=grossVal, port.size=10, existing=curPortfol, turnover=c(21000, 32000))
We are forcing the turnover to be between 21,000 and 32,000 (dollars).
Remember that each time you execute this command, you get a different set of portfolios.
turnover argument takes a numeric vector of length two. The numbers are in currency units, and give the lower and upper limits on how much trading (buys plus sells) can take place.
You can give
turnover a single number, in which case that will be the upper bound — the lower bound is understood to be zero.
It is possible to get the random trades rather than the random portfolios. All you need to do is use the
ranTradeTurnover <- random.portfolio(1000, priceVector, long.only=TRUE, gross=grossVal, port.size=10, existing=curPortfol, turnover=c(21000, 32000), out.trade=TRUE)
This is precisely the same as the call that created
rpTurnover except for the addition of “
We can get the gross valuation of the random trades (that is, the turnover) with the command:
ranValTurnover <- valuation(ranTradeTurnover, priceVector, collapse=TRUE)
The minimum and maximum of the turnover are:
> range(ranValTurnover)  21000.79 31998.76
Checking your work
We can see that turnover satisfies the constraints that we set by using the
trade.distance function. First we create a vector of the value of the turnover for each random portfolio:
rpTurnTurn <- sapply(rpTurnover, function(x) trade.distance(curPortfol, x, prices=priceVector, scale=FALSE))
Now we look at the range of the turnovers:
> range(rpTurnTurn)  21000.83 31998.50