(This article was first published on Timely Portfolio, and kindly contributed to R-bloggers)
DO NOT TRADE THIS SYSTEM. YOU VERY EASILY COULD LOSE LARGE AMOUNTS OF MONEY.
I am not necessarily recommending the system that I presented in Exploring the Market with Hurst, but I thought it would provide a nice platform to illustrate some backtesting with multiple indexes. Do not pass on the incredible opportunity presented by almost unlimited indexes from Yahoo! Finance and the Fed.
![]() |
| From TimelyPortfolio |
![]() |
| From TimelyPortfolio |
![]() |
| From TimelyPortfolio |
![]() |
| From TimelyPortfolio |
![]() |
| From TimelyPortfolio |
![]() |
| From TimelyPortfolio |
require(quantmod)
require(PerformanceAnalytics)
require(FGN) #sort of random mix for additional testing
indexes<-c("STI","KS11","GDAXI","DJUSAU","DJUSFI","RUT","DJUBS")
#iterate through index symbols
for (i in 1:length(indexes)) {
#get symbol
getSymbols(paste("^",indexes[i],sep=""),
from="1900-01-01",to=format(Sys.Date(),"%Y-%m-%d"))
#set monthly index to first day of the month
assign(indexes[i],to.monthly(get(indexes[i]))[,4])
#has to be a cleaner way than this to set the index
assign(indexes[i],as.xts(coredata(get(indexes[i])),
order.by=as.Date(index(get(indexes[i])))))
#get monthly changes
ret<-ROC(get(indexes[i]),n=1,type="discrete")
index(ret) <- as.Date(index(ret))
hurstKmonthly <- apply.rolling(ret, FUN="HurstK", width = 12)
colnames(hurstKmonthly) <- "HurstK.monthly"
index(hurstKmonthly) <- as.Date(index(hurstKmonthly))
serialcorr <- runCor(cbind(coredata(ret)),cbind(index(ret)),n=12)
serialcorr <- as.xts(serialcorr,order.by=index(ret))
autoreg <- runCor(ret,lag(ret,k=1),n=12)
colnames(serialcorr) <- "SerialCorrelation.monthly"
colnames(autoreg) <- "AutoRegression.monthly"
signalUpTrend <- runMean(hurstKmonthly+serialcorr+autoreg,n=6) +
(get(indexes[i])/runMean(get(indexes[i]),n=12)-1)*10
signalUpTrend <- lag(signalUpTrend,k=1)
Created by Pretty R at inside-R.org
To leave a comment for the author, please follow the link and comment on his blog: Timely Portfolio.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...







Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).