public
anonymous / rjb test on rut.r
Created

  • Download Gist
rjb test on rut.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.