# analyze strategy performance with given matrix of weights/time and weekly returns in R

I have a matrix of 259 weekly returns, 50 assets and a portfolio composition for each of the 259 weeks. I would like to test the performance of the portfolio during 52 weeks, rebalancing every 12 weeks also taking into account fees for each rebalancing, etc. I would choose 50 dates randomly, perform the test each date, store results and finally average them. Is there any way of doing this in R?

n=50
d=52*5-1 #multiple of 7
w=16
ns=50
returns=xts(matrix(rnorm(n*d,0,0.01),d,n),Sys.Date()-seq(d*7,1,by=-7))
A=c(rep(1/w,w),rep(0,n-w))
weights=xts(t(replicate(d,sample(A,n))),Sys.Date()-seq(d*7,1,by=-7))
dates.v=as.Date(replicate(ns,sample(index(returns),1)))
for (i in 1:ns) {
while (dates.v[i]+52*7>max(index(returns))) {dates.v[i]=sample(index(returns),1)} #this is to ensure that we always use one entire year
}


I already had a look to the fPortfolio, backtest and PortfolioSim packages but haven't found a similar example so don't know whether is possible or not.

• Of course its possible - I think what you're actually asking is has someone already built a package/function for you. – jeff m Apr 28 '13 at 20:24

## 1 Answer

Have a look at fPortfolioBacktest. An example can be found here: https://r-forge.r-project.org/scm/viewvc.php/pkg/fPortfolioBacktest/man/portfolioBacktesting.Rd?view=markup&revision=4086&root=rmetrics

Edit: you may want to try backtestPlot(smoothedPortfolios) to visualise the strategy performance.