Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created July 5, 2012 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3056379 to your computer and use it in GitHub Desktop.
Save anonymous/3056379 to your computer and use it in GitHub Desktop.
require(lawstat)
require(quantmod)
require(PerformanceAnalytics)
#set up function to use to obtain rolling p-value from bera jarque normality test
rjb.p <- function(x) {
rjb.test(x,option="RJB")$p.value
# rjb.test(x,option="RJB")$statistic
}
#set up function to do same routine on Russell 2000 and French small
#to test for robustness of result
testmultiple <- function(x,data.source="") {
if (data.source=="") data.source=colnames(x)[1]
x.roc <- ROC(to.weekly(x)[,4],type="discrete",n=1)
x.roc[1,]<-0
signal.rjb <- apply.rolling(x.roc,FUN=rjb.p,width=25)
roc.long <- ROC(to.weekly(x)[,4],type="discrete",n=8)
#plot.zoo(signal.rjb*roc.long)#,ylim=c(0,20))
perf <- merge(lag(ifelse((signal.rjb>0.05 & roc.long > 0.015) | (signal.rjb < 0.75 & signal.rjb > 0.4) ,1,0)) * x.roc,x.roc)
colnames(perf) <- c("system","buyhold")
charts.PerformanceSummary(perf,ylog=TRUE,main=paste("Performance of Systems",data.source,sep=" "))
return("done")
}
#do this to get from Yahoo! Finance but history only goes back to 1987
#getSymbols("^RUT",from="1900-01-01")
#I have local file from Bloomberg that goes back to 1980
RUT <- read.csv("rut.csv",stringsAsFactors=FALSE)
RUT <- as.xts(RUT[,2],order.by=as.Date(RUT[,1]))
testmultiple(RUT,data.source="Russell 2000 Index")
#do again but this time using French data
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_12_2_Daily.zip"
my.tempfile<-paste(tempdir(),"\\frenchmomentum.zip",sep="")
my.usefile<-paste(tempdir(),"\\6_Portfolios_ME_Prior_12_2_Daily.txt",sep="")
download.file(my.url, my.tempfile, method="auto",
quiet = FALSE, mode = "wb",cacheOK = TRUE)
unzip(my.tempfile,exdir=tempdir(),junkpath=TRUE)
#read space delimited text file extracted from zip
french_momentum <- read.table(file=my.usefile,
header = TRUE, sep = "",
as.is = TRUE,
skip = 12, nrows=12316)
colnames(french_momentum) <- c(paste("Small",
colnames(french_momentum)[1:3],sep="."),
paste("Large",colnames(french_momentum)[1:3],sep="."))
#get dates ready for xts index
datestoformat <- rownames(french_momentum)
datestoformat <- paste(substr(datestoformat,1,4),
substr(datestoformat,5,6),substr(datestoformat,7,8),sep="-")
#get xts for analysis
french_momentum_xts <- as.xts(french_momentum[,1:6],
order.by=as.Date(datestoformat))
french_momentum_xts <- french_momentum_xts/100
#get average of small to test all
french_momentum_small <- as.xts(apply(french_momentum_xts[,1:3],MARGIN=1,FUN=mean),
order.by=index(french_momentum_xts))
french_momentum_price <- as.xts(apply(french_momentum_small+1,MARGIN=2,FUN=cumprod),
order.by=index(french_momentum_xts))
#to get cumulative return(price) for each by size x momentum
#french_momentum_price <- as.xts(apply(french_momentum_xts+1,MARGIN=2,FUN=cumprod),
# order.by=index(french_momentum_xts))
#to test each by momentum
#apply(french_momentum_price[,1:3],MARGIN=2,FUN=testmultiple)
testmultiple(french_momentum_price,data.source="French Small")
#now let's do on the S&P 500
getSymbols("^GSPC",from="1900-01-01")
testmultiple(GSPC,data.source="S&P 500")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment