R/wrapper.R

## Wrapper function for all other RIS functions

## to call function from github.
#library(devtools)
#install_github("RISpackage2","Bjerring")
#library(RISpackage2)
#wrap(80,3)
# hosted at https://bjerring.ocpu.io/RISpackage2/R/

# Run line to download data...
# source("R/downloadData.R")

# this is a change...

## run locally:
# setwd("C:/Users/Thomas/Documents/RISpackage2")
# source("R/backtest.R")
# source("R/clusterMethods.R")
# source("R/JSON.R")
# source("R/selector.R")
# source("R/sumstat.R")
# source("R/futuretest.R")
# library(corpcor)
# library(boot)
# load("data/data.rda")
# load("data/tmp.rda")
# load("data/types.rda")
# load("data/benchmark.rda")
# score <- 110; Cno <- 3; budget <- 100000
#
wrap <- function(score=130,Cno=3,budget=100000){
  
  # first, the inputs are converted to numerical values
  tmp.score <- as.numeric(score)
  score <- tmp.score
  
  tmp.Cno <- as.numeric(Cno)
  Cno <- tmp.Cno
  
  tmp.budget <- as.numeric(budget)
  budget <- tmp.budget
  
  # prices to returns.
  returns <- diff(log(data))+1
  
  # attach correct names to description of ETFs
  names(tmp) <- colnames(data)
  
  # determine risk level
  if(score < 100){
    risk <- "Low"
  }
  if(score >= 100 && score < 150){
    risk <- "Medium"
  }
  if(score >= 150){
    risk <- "High"
  }  
  
  # ETFs to invest in now
  ETFs <- Selector(returns,risk,Cno)
  
  # info about the different ETFs
  col.info <- as.character(tmp[ETFs])
  names(col.info) <- ETFs
  
  # backtest of investment strategy with annual revision
  b.test <- budget*(backtest(returns,risk,Cno)/100000)
  
  # performance statistics
  stats <- sumstat(b.test,ETFs)
  
  # futuretest of selected portfolio
  future <- budget*(futuretest(returns[,ETFs],ETFs)/100000)
  
  # futuretest dates
  dates <- as.Date(c((Sys.Date()):(Sys.Date()+7*52*6)),origin="1970-01-01")
  dates.week <- dates[seq(1,length(dates),7)]
  
  # Benchmark (VT)
  VTret <- diff(log(benchmark[(length(benchmark)-length(b.test)):(length(benchmark))]))+1
  AGret <- returns[(dim(returns)[1]-length(b.test)+1):dim(returns)[1],"AGG"]
  
  if(risk == "Low"){
    bench <- budget*cumprod(0.3*VTret+0.7*AGret)  
    bench.name <- c("30% World stock index / 70% U.S. treasury bonds")
  }
  if(risk == "Medium"){
    bench <- budget*cumprod(0.5*VTret+0.5*AGret)  
    bench.name <- c("50% World stock index / 50% U.S. treasury bonds")
  }
  if(risk == "High"){
    bench <- budget*cumprod(0.7*VTret+0.3*AGret)  
    bench.name <- c("70% World stock index / 30% U.S. treasury bonds")
  }
  names(bench) <- names(b.test)
  
  # collect output
  output <- list()
  output[[1]] <- ETFs                             # ETFs to invest in
  output[[2]] <- round(rep(1/length(ETFs),length(ETFs)),digits=3) # weights to each ETF
  output[[3]] <- col.info                         # description of each ETF
  output[[4]] <- names(b.test)                    # dates in backtest
  output[[5]] <- round(b.test,digits=2)           # accumulated wealth in backtest
  output[[6]] <- names(stats)                     # names of summary statistics
  output[[7]] <- round(stats,digits=2)            # values of sommary statistics
  output[[8]] <- round(future[1,],digits=2)       # 5% quantile
  output[[9]] <- round(future[2,],digits=2)       # 25% quantile
  output[[10]] <- round(future[3,],digits=2)      # expected wealth
  output[[11]] <- round(future[4,],digits=2)      # 75% quantile
  output[[12]] <- round(future[5,],digits=2)      # 95% quantile
  output[[13]] <- dates.week[1:(length(future[5,]))]  # dates
  output[[14]] <- round(bench,digits=2)
  output[[15]] <- c("stock","bond","commodity")                     # ETF type, i.e. stock, bond, commodity
  output[[16]] <- round(c(length(which(types[ETFs] == "stock"))/length(ETFs),length(which(types[ETFs] == "bond"))/length(ETFs),length(which(types[ETFs] == "commodity"))/length(ETFs)),digits=2)# fraction in each catagory
  output[[17]] <- bench.name
  # write output to JSON format
  out <- JSON.OBJ(output)
  return(out)
} 
Bjerring/RISpackage2 documentation built on May 6, 2019, 7:56 a.m.