The following is a script file containing all R code of all sections in this chapter.

Defining the Prediction Tasks

Which Predictors?

Evaluation Criteria

The Prediction Models

How Will the Training Data Be Used?

The Modeling Tools

From Predictions into Actions

How Will the Predictions Be Used?

Putting Everything Together: A Simulated Trader

Model Evaluation and Selection

Monte Carlo Estimates

Experimental Comparisons

tradingWF <- function(form, train, test, 
                      quotes, pred.target="signals",
                      learner, learner.pars=NULL,
                      predictor.pars=NULL,
                      learn.test.type='fixed', relearn.step=30,
                      b.t, s.t,
                      policy, policy.pars,
                      trans.cost=5, init.cap=1e+06)
{
    ## obtain the model(s) and respective predictions for the test set
    if (learn.test.type == 'fixed') {  # a single fixed model
        m <- do.call(learner,c(list(form,train),learner.pars))
        preds <- do.call("predict",c(list(m,test),predictor.pars))
    } else {  # either slide or growing window strategies
        data <- rbind(train,test)
        n <- NROW(data)
        train.size <- NROW(train)
        sts <- seq(train.size+1,n,by=relearn.step)
        preds <- vector()
        for(s in sts) {  # loop over each relearn step
            tr <- if (learn.test.type=='slide') data[(s-train.size):(s-1),] 
                  else data[1:(s-1),]
            ts <- data[s:min((s+relearn.step-1),n),]
            
            m <- do.call(learner,c(list(form,tr),learner.pars))
            preds <- c(preds,do.call("predict",c(list(m,ts),predictor.pars)))
        }    
    } 
    
    ## Getting the trading signals
    if (pred.target != "signals") {  # the model predicts the T indicator
        predSigs <- trading.signals(preds,b.t,s.t)
        tgtName <- all.vars(form)[1]
        trueSigs <- trading.signals(test[[tgtName]],b.t,s.t)
    } else {  # the model predicts the signals directly
        tgtName <- all.vars(form)[1]
        if (is.factor(preds))
            predSigs <- preds
        else {
            if (preds[1] %in% levels(train[[tgtName]]))
                predSigs <- factor(preds,labels=levels(train[[tgtName]]),
                                   levels=levels(train[[tgtName]]))
            else 
                predSigs <- factor(preds,labels=levels(train[[tgtName]]),
                                   levels=1:3)
        }
        trueSigs <- test[[tgtName]]
    }

    ## obtaining the trading record from trading with the signals
    date <- rownames(test)[1]
    market <- get(quotes)[paste(date,"/",sep='')][1:length(preds),]
    tradeRec <- trading.simulator(market,predSigs,
                                  policy.func=policy,policy.pars=policy.pars,
                                  trans.cost=trans.cost,init.cap=init.cap)
    
    return(list(trueSigs=trueSigs,predSigs=predSigs,tradeRec=tradeRec))
}
library(performanceEstimation)
library(e1071)
library(earth)
library(nnet)
LEARNERS <- c('svm','earth','nnet')
EST.TASK <- EstimationTask(method=MonteCarlo(nReps=20,
                                             szTrain=2540,szTest=1270,
                                             seed=1234),
                           evaluator="tradingEval")
VARS <- list()

VARS$svm <- list(learner.pars=list(cost=c(10,50,150),
                                   gamma=c(0.01,0.05)))
VARS$earth <- list(learner.pars=list(nk=c(10,17),
                                     degree=c(1,2),
                                     thresh=c(0.01,0.001)))
VARS$nnet <-  list(learner.pars=list(linout=TRUE, trace=FALSE,
                                     maxit=750,
                                     size=c(5,10),
                                     decay=c(0.001,0.01,0.1)))

VARS$learning <- list(learn.test.type=c("fixed","slide","grow"), relearn.step=120)
VARS$trading  <- list(policy=c("policy.1","policy.2"),
                     policy.pars=list(bet=c(0.2,0.5),exp.prof=0.05,max.loss=0.05),
                     b.t=c(0.01,0.05),s.t=c(-0.01,-0.05))

## Regression (forecast T indicator) Workflows
for(lrn in LEARNERS) {
    objName <- paste(lrn,"res","regr",sep="_")
    assign(objName,
           performanceEstimation(PredTask(Tform,Tdata.train,"SP500"),
                                 do.call("workflowVariants",
                                         c(list("tradingWF",
                                                varsRootName=paste0(lrn,"Regr"),
                                                quotes="GSPC",
                                                learner=lrn,
                                                pred.target="indicator"),
                                           VARS[[lrn]],
                                           VARS$learning,
                                           VARS$trading)
                                         ),
                                 EST.TASK,
                                 cluster=TRUE) # for parallel computation
           )
    save(list=objName,file=paste(objName,'Rdata',sep='.'))
}

## Specific settings to make nnet work as a classifier
VARS$nnet$learner.pars$linout <-  FALSE
VARS$nnet$predictor.pars <-  list(type="class")

## Classification (forecast signal) workflows
for(lrn in c("svm","nnet")) { # only these because MARS is only for regression
    objName <- paste(lrn,"res","class",sep="_")
    assign(objName,
           performanceEstimation(PredTask(TformC,Tdata.trainC,"SP500"),
                                 do.call("workflowVariants",
                                         c(list("tradingWF",
                                                varsRootName=paste0(lrn,"Class"),
                                                quotes="GSPC",
                                                learner=lrn,
                                                pred.target="signals"),
                                           VARS[[lrn]],
                                           VARS$learning,
                                           VARS$trading)
                                         ),
                                 EST.TASK,
                                 cluster=TRUE) # for parallel computation
           )
    save(list=objName,file=paste(objName,'Rdata',sep='.'))
}

Results Analysis

Note: The Rdata files mentioned below can be obtained at the section Other Information on the top menus of this web page.