Can you show a simple strategy using machine Learning?

Hello,


Is it possible to build a strategy using machine learning? How can I do that? 


Thank you!

Hi,

Below an example of a strategy on the CAC relying on the popular XGBoost. 


The strategy is a classification problem. The objective is to forecast the 3-step ahead returns going-up or down. 

When the model forecasts a bullish trend, the strategy generates a buy signal (1). At the opposite, the signal is 0 and no position is taken.  


To build my features, I used well-known technical indicators: Exponential Moving Average, Relative Strength Index and returns at different time horizons.

The EMA is a smoothing indicator showing price trend direction. The RSI on the other hand, is an oscillator measuring the speed and change of price movements.  


I have trained my model on period ranging from 2007 to 2014 and tested it strating from 2015. I have obtained my performance results relying on Alphien’s algoEngine. Note that my model is trained within my payout function XGBoostStrat. 


Feel free to copy this payout and modify it to suit your needs. 


Thank you!


.sourceQlib()


XGBoostStrat<-function(ind=“CACa”, zoom=“2014”, forecastHorizon=3){

  

 ##Retrieving data

 data=getLyxorBBs(ind,asPrice = TRUE) 

  

 ##Building Features

 features=cbind(data, 

         EMA(x=data,n=5),EMA(x=data,n=10), ##Exponential Moving Average 

         RSI(price=data, n=5), RSI(price=data, n=60), ##Relative Strength Index 

         ROC(data,type=“discrete”,n=2,na.pad=FALSE), 

         ROC(data,type=“discrete”,n=5,na.pad=FALSE)) 

 features=na.omit(cbind(features, lag(features,2), lag(features,5), lag(features,10)))  

  

  

 ##Set-up of the classification problem: Forcasting 5-step ahead returns going up (1) or down (0)

 Y=ifelse(lag(ROC(data,type=“discrete”,n=forecastHorizon,na.pad=FALSE),-forecastHorizon)>=0, 1, 0)

 data=na.omit(cbind(features,Y))

  

 ##Split data into train/test sets

 data=list(“train”=data[paste0("/",zoom)], ‘test’=data[paste0((zoom+1),"/"),])  

 

  

 ##Scaling Model

 trainMeans=apply(data$train[,1:(ncol(data$train)-1)],2,mean) ##Estimating mean

 trainSd=apply(data$train[,1:(ncol(data$train)-1)],2,sd) ##Estimating Standard deviation 

 train= as.data.frame(t(apply(data$train[,1:(ncol(data$train)-1)],1,function(x){(x-trainMeans)/trainSd})))##Normalize Train set

 test = as.data.frame(t(apply(data$test[,1:(ncol(data$test)-1)],1,function(x){(x-trainMeans)/trainSd})))##Normalize Test set with train set metrics

  

 ##Training Model

 model=xgboost::xgb.train(params=list(“max.depth” = 20, “eta” =0.1,  

                    “min_child_weight” = 1, objective = “binary:logistic”), 

                    data=xgb.DMatrix(data = as.matrix(train), 

                    label = as.vector(data$train[,(ncol(train)+1)])),

                    nrounds=10000)

  

 ##Returning Payout: signals are the predictions

 return(xts(ifelse(predict(model, (xgb.DMatrix(data = as.matrix(test))))>=0.5,1,0), order.by=index((data$test))))

}


algoEngine(“CACa”,type = “MM”) %>%

 payout(XGBoostStrat, ind=“CACa”, zoom=2014) -> myXGBoostStrat


myXGBoostStrat=evaluate(myXGBoostStrat)

analyse(myXGBoostStrat)

backtest(myXGBoostStrat) 

Post deleted by manas