System Failure-Maybe it Will Help

August 11, 2011

(This article was first published on Timely Portfolio, and kindly contributed to R-bloggers)

I hope everyone is enjoying the market.  After a crazy week personally and 6% intraday swings, I remember why I abandoned day trading.

I often wonder if I should share ideas that do not work as well as I would like.  In this case, I know I have generated an acceptable system in a previous life in Excel, but I cannot remember the details.  So far all the testing and various trails in R have not yielded anything exceptional, but I am sure capable readers can find the secret combination.  Please let me know what you discover.

This idea uses linear models to generate slope and correlation.  Then if slope is positive and correlation high, the system enters.  THIS IS NOT INVESTMENT ADVICE.  THIS CAN LOSE LOTS OF MONEY.

From TimelyPortfolio

R code: (click to download)

require(quantmod)   #set this up to get either FRED or Yahoo!Finance
getSymbols("^GSPC",from="1896-01-01",to=Sys.Date())     GSPC <- to.weekly(GSPC)[,4]
#GSPCmean <- runMean(GSPC,n=20)
#index(GSPC) <- as.Date(index(GSPC))   width = 25
for (i in (width+1):NROW(GSPC)) {
linmod <- lm(GSPC[((i-width):i),1]~index(GSPC[((i-width):i)]))
ifelse(i==width+1,signal <- coredata(linmod$residuals[length(linmod$residuals)]),
signal <- rbind(signal,coredata(linmod$residuals[length(linmod$residuals)])))
ifelse(i==width+1,signal2 <- coredata(linmod$coefficients[2]),
signal2 <- rbind(signal2,coredata(linmod$coefficients[2])))
ifelse(i==width+1,signal3 <- cor(linmod$fitted.values,GSPC[((i-width):i),1]),
signal3 <- rbind(signal3,cor(linmod$fitted.values,GSPC[((i-width):i),1])))
signal <- as.xts(signal,[(width+1):NROW(GSPC)]))
signal2 <- as.xts(signal2,[(width+1):NROW(GSPC)]))
signal3 <- as.xts(signal3,[(width+1):NROW(GSPC)]))   price_ret_signal <- merge(GSPC,lag(signal,k=1),
price_ret_signal[,2] <- price_ret_signal[,2]/price_ret_signal[,1]
price_ret_signal[,3] <- price_ret_signal[,3]/price_ret_signal[,1]   #ret <- ifelse((runMin(price_ret_signal[,3],n=10) >= 0 &
# runSum(price_ret_signal[,2],n=30) >= 0.0) |
# (runMin(price_ret_signal[,3],30) < 0 &
# runSum(price_ret_signal[,2],n=50) >= 0.02),
# 1, 0) * price_ret_signal[,5]
#ret <- ifelse(runSum(price_ret_signal[,3],n=10) >= 0, 1, 0) * price_ret_signal[,5]   ret <- ifelse((runMean(price_ret_signal[,3],n=5) > 0 &
runMean(price_ret_signal[,4],n=5) > 0.25),
1, 0) * price_ret_signal[,5]   retCompare <- merge(ret, price_ret_signal[,5])
colnames(retCompare) <- c("Linear System", "BuyHold")
colorset=c("black","gray70"),main="GSPC System Return Comparison")

To leave a comment for the author, please follow the link and comment on their blog: Timely Portfolio. 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...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)