backtest <- function(data,risk,Cno){
btPeriods <- round(dim(data)[1] - 4*52) # backtesting periods
wealth <- rep(100000,btPeriods) # wealth development
rebalanc <- dim(data)[1]/26 # number of weeks before rebalancing
ret <- list() # return parameter
time <- names(data[(length(data[,1]) - btPeriods):(length(data[,1])),1])
for(t in 1:btPeriods){
# Available historic returns up until t-1
data.avail <- data[t:(length(data[,1]) - btPeriods + t - 1),]
# Actual return in period t
data.act <- data[(length(data[,1]) - btPeriods + t),]
# allocate assets every 12 months
if(t == 1 || (t %% rebalanc) == 0){
allocate <- Selector(data.avail,risk,Cno)
alloc <- rep(1/Cno,Cno)
names(alloc) <- allocate
}
wealth[t+1] <- wealth[t]*sum(alloc*data.act[allocate])
}
dates <- rownames(data)[(length(rownames(data))-length(wealth)+1):length(rownames(data))]
#tmp.date <- unlist(strsplit(dates,'-',))
#day <- as.numeric(tmp.date[seq(1,length(tmp.date),3)])
#month <- match(tmp.date[seq(2,length(tmp.date),3)],month.abb)
#year <- as.numeric(tmp.date[seq(3,length(tmp.date),3)])+2000
#n.dates <- paste(year,month,day,sep="-")
#names(wealth) <- n.dates
names(wealth) <- dates
return(wealth)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.