Quantitative research, trading strategy ideas, and backtesting for the FX and equity markets

Main menu

Monthly Archives: February 2013

The past few posts on momentum with R focused on a relatively simple way to backtest momentum strategies. In part 4, I use the quantstrat framework to backtest a momentum strategy. Using quantstrat opens the door to several features and options as well as an order book to check the trades at the completion of the backtest.

I introduce a few new functions that are used to prep the data and compute the ranks. I won’t go through them in detail, these functions are available in my github repo in the rank-functions folder.

This first chunk of code just loads the necessary libraries, data, and applies the ave3ROC function to rank the assets based on averaging the 2, 4, and 6 month returns. Note that you will need to load the functions in Rank.R and monthly-fun.R.

The next chunk of code is a critical step in preparing the data to be used in quantstrat. With the ranks computed, the next step is to bind the ranks to the actual market data to be used with quantstrat. It is also important to change the column names to e.g. XLY.Rank because that will be used as the trade signal column when quantstrat is used.

# this is an important step in naming the columns, e.g. XLY.Rank# the "Rank" column is used as the trade signal (similar to an indicator)# in the qstratRank functioncolnames(sym.rank) <- gsub(".Adjusted",".Rank",colnames(sym.rank))# ensure the order of order symbols is equal to the order of columns # in symbols.closestopifnot(all.equal(gsub(".Adjusted","",colnames(symbols.close)),symbols))# bind the rank column to the appropriate symbol market data# loop through symbols, convert the data to monthly and cbind the data# to the rankfor(i in1:length(symbols)){
x <- get(symbols[i])
x <- to.monthly(x,indexAt='lastof',drop.time=TRUE)
indexFormat(x) <- '%Y-%m-%d'colnames(x) <- gsub("x",symbols[i],colnames(x))
x <- cbind(x, sym.rank[,i])assign(symbols[i],x)}

Changing the argument to max.levels=2 gives the flexibility of “scaling” in a trade. In this example, say asset ABC is ranked 1 in the first month — I buy 500 units. In month 2, asset ABC is still ranked 1 — I buy another 500 units.