Hi all,
I'm currently working on a project that tests whether momentum ( as defined by Jegadeesh and Titman, 1993) exists in India. 

I'm new to R and ran into problems. I'm hoping someone here can help me with an example snippet of the code where I'm stuck... Thanks in advance!

The trading idea is to rank stocks into deciles based on the past (3,6,12 month) returns, go long the highest decile (thus a portfolio) and short the lowest decile (another portfolio) - standard momentum portfolio from any investments course. The portfolios thus formed are held for 3,6,12 months.

Note that the indicators use returns instead of price. I'm unable to get the ranking done correctly. I have attempted to write a "rank" function in order to rank the securities in any given month/date based on their past 3 (as well as 6 & 7) month returns, but so far have been unable to. 

# Load required libraries
library(quantmod)
library(quantstrat)

# Try to clean up in case the demo was run previously
suppressWarnings(rm("account.faber","portfolio.faber",pos=.blotter))
suppressWarnings(rm("ltaccount", "ltportfolio", "ClosePrice", "CurrentDate", "equity", 
            "GSPC", "stratFaber", "initDate", "initEq", "Posn", "UnitSize", "verbose"))
suppressWarnings(rm("order_book.faber",pos=.strategy))

# Set initial values
initDate='2000-01-01'
initEq=100000

currency("INR")


# Set up instruments with FinancialInstruments package
currency("INR")
symbols= c("AXISBANK.BO","BAJAJAUT.BO","BPCL.BO","BHARTIARTL.BO","CAIRN.BO","CIPLA.BO","COALINDIA.BO","DLF.BO","DRREDDY.BO")

for(symbolinsymbols){ # establish tradable instruments
    stock(symbol, currency="INR",multiplier=1)
}

# Load data with quantmod, make monthly
getSymbols(symbols, src='yahoo', index.class=c("POSIXt","POSIXct"), from=initDate)
for(symbolinsymbols) {
    x<-get(symbol)
    x<-to.monthly(x,indexAt='lastof',drop.time=TRUE)
    indexFormat(x)<-'%Y-%m-%d'
    colnames(x)<-gsub("x",symbol,colnames(x))
    assign(symbol,x)
}

# Initialize portfolio and account
port<-'moment'#use a string here for easier changing of parameters and re-trying
initPortf(port, symbols=symbols, initDate=initDate)
initAcct(port, portfolios=port, initDate=initDate, initEq=100000)
initOrders(portfolio=port, initDate=initDate)

print("setup completed")

### Here is the problem

rank<-function()

# and based on how the rank function turns out, the signals/rules must react accordingly


From what I gathered on the internet, one way to rank was as follows:


getSymbols(c("GE","XOM"), from = "2012-02-20")
x<-cbind(GE,XOM)

NewVar<-cut(x,quantile(x,(0:10)/10),include.lowest=TRUE)



I know this is not working for several reasons, one being that it is ranking by column, but I have little idea how to either correct it or to use any other technique.

How do I modify this so that it goes across columns and ranks them, for a given row.

Thanks!

Gaurav M

	[[alternative HTML version deleted]]

