[This article was first published on Timely Portfolio, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 |
require(PerformanceAnalytics) require(quantmod) #set this up to get either FRED or Yahoo!Finance #getSymbols("GSPC",src="FRED") 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,order.by=index(GSPC[(width+1):NROW(GSPC)])) signal2 <- as.xts(signal2,order.by=index(GSPC[(width+1):NROW(GSPC)])) signal3 <- as.xts(signal3,order.by=index(GSPC[(width+1):NROW(GSPC)])) price_ret_signal <- merge(GSPC,lag(signal,k=1), lag(signal2,k=1),lag(signal3,k=1), ROC(GSPC,type="discrete",n=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") charts.PerformanceSummary(retCompare,ylog=TRUE,cex.legend=1.2, 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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.