**rbresearch » R**, and kindly contributed to R-bloggers)

Time really flies… it is hard to believe that it has been over a month since my last post. Work and life in general have consumed much of my time lately and left little time for research and blog posts. Anyway, on to the post!

This post will be the first in a series of to cover a momentum strategy using R.

One of my favorite strategies is a momentum or relative strength strategy. Here are just a few of the reasons why I like momentum:

- Simple to implement
- Long only or long/short portfolios
- Many ways to define the strength or momentum measure
- It just works

Also, a momentum strategy lends itself well to potential for diversification. The universe of instruments can be infinite, but the instruments traded are finite. Think about it this way… Investor A looks at 10 instruments and invests $1000 in the top 5 instruments ranked by momentum. Investor B looks at 100 instruments and invests $1000 in the top 5 instruments ranked by momentum. Investor A is limiting his potential for diversification by only having a universe of 10 instruments. Investor B has a much larger universe of instruments and can in theory be more diversified. Theoretically speaking, you can trade an infinite number of instruments with a finite amount of trading capital using a momentum or relative strength strategy.

Check out these links for further reading

In this first post of the series on momentum, I will go over some of the basic setup and functions we will be using.

The first step is to get data from yahoo.

require(quantstrat) #Load ETFs from yahoo currency("USD") symbols = c("XLY", "XLP", "XLE", "XLF") stock(symbols, currency="USD",multiplier=1) getSymbols(symbols, src='yahoo', index.class=c("POSIXt","POSIXct"), from='2000-01-01') #Convert to monthly and drop all columns except Adjusted Close for(symbol in symbols) { x <- get(symbol) x <- to.monthly(x,indexAt='lastof',drop.time=TRUE) indexFormat(x) <- '%Y-%m-%d' colnames(x) <- gsub("x",symbol,colnames(x)) x <- x[,6] #drops all columns except Adjusted Close which is 6th column assign(symbol,x) }

Note that the for loop converts the data to monthly and subsets the data so that the only column we keep is the adjusted close column. We now have four objects (XLY, XLP, XLE, XLF) that have the Adjusted Close price.

> head(XLE) XLE.Adjusted 2000-01-31 22.89 2000-02-29 21.92 2000-03-31 24.56 2000-04-30 24.19 2000-05-31 27.04 2000-06-30 25.55

The next step is to merge these four objects into a single object holding the Adjusted Close price. We can do this in a simple one-liner in R!

> head(symbols_close) XLY.Adjusted XLP.Adjusted XLE.Adjusted XLF.Adjusted 2000-01-31 24.06 18.36 22.89 18.09 2000-02-29 22.72 16.22 21.92 16.15 2000-03-31 25.89 16.77 24.56 19.04 2000-04-30 25.35 17.65 24.19 19.22 2000-05-31 23.98 18.92 27.04 19.65 2000-06-30 22.68 19.98 25.55 18.70

For the factor that will be ranked, I will use the 3 period rate of change (ROC).

#xts object of the 3 period ROC of each column in the close object #The 3 period ROC will be used as the ranking factor roc <- ROC(symbols_close, n = 3, type = "discrete")

> head(roc) XLY.Adjusted XLP.Adjusted XLE.Adjusted XLF.Adjusted 2000-01-31 NA NA NA NA 2000-02-29 NA NA NA NA 2000-03-31 NA NA NA NA 2000-04-30 0.05361596 -0.03867102 0.05679336 0.06246545 2000-05-31 0.05545775 0.16646116 0.23357664 0.21671827 2000-06-30 -0.12398610 0.19141324 0.04030945 -0.01785714

Then we apply the rank function across each row of the roc object.

> head(r) XLY.Adjusted XLP.Adjusted XLE.Adjusted XLF.Adjusted 2000-01-31 1 2 3 4 2000-02-29 1 2 3 4 2000-03-31 1 2 3 4 2000-04-30 3 4 2 1 2000-05-31 4 3 1 2 2000-06-30 4 1 2 3

That will wrap up this first post for a quick and easy way to rank assets based on 3 month simple returns. Future posts will explore other methods for ranking and using quantstrat to backtest momentum.

Here is the code in full.

require(quantstrat) #Load ETFs from yahoo currency("USD") symbols = c("XLY", "XLP", "XLE", "XLF") stock(symbols, currency="USD",multiplier=1) getSymbols(symbols, src='yahoo', index.class=c("POSIXt","POSIXct"), from='2000-01-01') #Convert to monthly and drop all columns except Adjusted Close for(symbol in symbols) { x <- get(symbol) x <- to.monthly(x,indexAt='lastof',drop.time=TRUE) indexFormat(x) <- '%Y-%m-%d' colnames(x) <- gsub("x",symbol,colnames(x)) x <- x[,6] #drops all columns except Adjusted Close which is 6th column assign(symbol,x) } #merge the symbols into a single object with just the close prices symbols_close <- do.call(merge, lapply(symbols, get)) #xts object of the 3 period ROC of each column in the close object #The 3 period ROC will be used as the ranking factor roc <- ROC(symbols_close, n = 3, type = "discrete") #xts object with ranks #symbol with a rank of 1 has the highest ROC r <- as.xts(t(apply(-roc, 1, rank)))

Created by Pretty R at inside-R.org

**leave a comment**for the author, please follow the link and comment on their blog:

**rbresearch » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...