Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created October 1, 2012 15:04
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 timelyportfolio/3812323 to your computer and use it in GitHub Desktop.
Save timelyportfolio/3812323 to your computer and use it in GitHub Desktop.
russell 2000 when low vol
require(quantmod)
require(PerformanceAnalytics)
getSymbols("^RUT",from = "1900-01-01")
getSymbols("^GSPC",from = "1900-01-01")
#get 1 day change for the Russell 2000 and S&P 500
roc <- na.omit(merge(ROC(RUT[,4],type="discrete",n=1),ROC(GSPC[,4],type="discrete",n=1)))
stdev <- rollapplyr(roc,FUN=sd,width=50)
#get relative strength of Russell 2000 versus S&P 500
rs <- RUT[,4]/GSPC[,4]
#do some trial graphs to see the interaction
plot.zoo(merge(stdev[,1]-stdev[,2],rs))
plot.zoo(merge(stdev[,1]-stdev[,2]/rs,rs))
plot.zoo(merge(stdev[,1]/stdev[,2],rs,RUT[,4]))
#create a PerformanceAnalytics rolling summary of Russell 2000 versus the S&P 500
charts.RollingRegression(roc[,1],roc[,2],width=250,main="")
title(main="Russell 2000 compared to the S&P 500 (Rolling 1 Year)",outer=TRUE, line=-1.5, adj=0.05)
#use colors provided in xblocks documentation
rgb <- hcl(c(0, 0, 260), c = c(100, 0, 100), l = c(50, 90, 50), alpha = 0.2)
plot.zoo(RUT[,4], #plot closing price of Russell 2000
bty="n", #no box; will fill in later with abline
las=1, #no rotation on y axis labels
xlab = NA,
ylab = NA)
xblocks(index(RUT[,4]), as.vector(stdev[,1]-stdev[,2]/rs > - 0.0125),col = rgb[3])
#connect the axes
abline(h=par("usr")[3]) #extend y axis
abline(v=par("usr")[1]) #extend x axis
abline(h=pretty(par("yaxp")),lty=1,lwd=2,col="white") #try something new for gridlines
title(main="Russell 2000 (source: Yahoo! Finance)",outer=TRUE, line=-2, adj=0.05)
mtext("blocks denote periods where Russell 2000 50 day sd low compared to S&P 500 sd",side=3,adj=0.05,cex=0.7,font=3, line=-1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment