Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active January 20, 2021 03:54
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/3555983 to your computer and use it in GitHub Desktop.
Save timelyportfolio/3555983 to your computer and use it in GitHub Desktop.
exceptional case of the Russell 2000
#explore exceptional case of the Russell 2000
require(quantmod)
require(PerformanceAnalytics)
require(xtsExtra)
getSymbols("^RUT", from = "1900-01-01")
getSymbols("^GSPC", from = "1900-01-01")
#do initial exploration of distribution
chart.QQPlot(na.omit(ROC(RUT[,4],type="discrete",n=250))["1987::"])
chart.QQPlot(na.omit(ROC(GSPC[,4],type="discrete",n=250))["1987::"])
#explore barplot.xts to do a chart of annual returns for both indexes
#merge prices
prices <- merge(GSPC[,4],RUT[,4])
#use endpoints to get annual returns
returns.annual <- as.xts(apply(
ROC(prices[endpoints(prices,"years")],type="discrete",n=1),
MARGIN = 2,
FUN = na.fill, fill = 0),
order.by = index(prices[endpoints(prices,"years")]))
#name columns something a little more clear
colnames(returns.annual) <- c("S&P 500","Russell 2000")
#using barplot.xts create the plot
#I made some subtle changes to barplot.xts to experiment so plot will be cosmetically different
barplot.xts(returns.annual,
stacked=FALSE,
box="transparent", #get rid of box surrounding the plot
ylim=c(-0.5,0.5),
ylab=NA,
border=c(brewer.pal(n=11,"BrBG")[c(4,9)]),
col=c(brewer.pal(n=11,"BrBG")[c(4,9)])) #deliberately trying some new colors
title(main="Annual Returns of S&P 500 and Russell 2000",
outer = TRUE,
adj=0.05, font.main = 1, cex.main = 1.25, line = -2)
require(latticeExtra)
require(reshape2)
roc <- na.omit(merge(ROC(GSPC[,4],type="discrete",n=250),ROC(RUT[,4],type="discrete",n=250)))
#name columns something a little more clear
colnames(roc) <- c("S&P 500","Russell 2000")
roc.melt <- melt(coredata(roc))
asTheEconomist(
densityplot(~value,data=roc.melt,groups=Var2,
#par.settings=theEconomist.theme(box="transparent"),
#lattice.options=theEconomist.opts(),
auto.key=list(space="right",col=(brewer.pal(n=11,"BrBG")[c(9,4)]),lines=FALSE),
col=(brewer.pal(n=11,"BrBG")[c(9,4)]),
ylab=NA,
main="Annual Returns (Rolling 250 Day) of S&P 500 and Russell 2000")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment