sp500 and us 1 year since 1960

  • Download Gist
sp500 and us 1 year since 1960.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#start exploring Buffett's cash as a call option
#as described in
#http://www.theglobeandmail.com/globe-investor/investment-ideas/streetwise/for-warren-buffett-the-cash-option-is-priceless/article4565468/
 
require(latticeExtra)
require(directlabels)
require(reshape2)
require(quantmod)
require(PerformanceAnalytics)
 
 
#get sp500 for first attempt
getSymbols("SP500", src="FRED")
SP500.monthly <- to.monthly(SP500)[,4]
index(SP500.monthly) <- as.Date(index(SP500.monthly))
roc <- ROC(SP500.monthly, n = 1, type = "discrete")
 
#get 1 year t-bill for risk-free
getSymbols("GS1", src = "FRED")
 
#combine the monthly SP500 return with a monthly return of GS1 1 year treasury
returns <- na.omit( merge(roc, ((1+lag(GS1,1) / 100) ^ (1/12)) - 1) )
#do cumulative returns since 1960 so skip the 1950s
cumreturns <- cumprod(1+returns["1960::",])
 
cumreturns.df <- as.data.frame(cbind(index(cumreturns),coredata(cumreturns)))
colnames(cumreturns.df) <- c("Date","S&P500.Price","US1y")
cumreturns.df[,"Date"] <- as.Date(cumreturns.df[,"Date"])
cumreturns.melt <- melt(cumreturns.df,id.vars=1)
colnames(cumreturns.melt) <- c("Date","Index","CumulReturns")
#make the cumulative return line chart with latticeExtra
direct.label(asTheEconomist(xyplot(CumulReturns~Date, data = cumreturns.melt, groups=Index,
main = "S&P 500 (Price Only) Compared to US 1 Year Treasury Cumulative Growth")),
,method = list("last.points",hjust=1,vjust=0,cex=1.2))
#make a barplot for comparison of cumulative returns
barplot(t(table.Stats(returns["1960::",])[7,] + 1) ^ 12 - 1,
beside=TRUE,
ylim=c(0,0.07),
names.arg=colnames(cumreturns.df)[2:3],
col=theEconomist.theme()$superpose.line$col,
las=1, cex.axis=0.8,
main="")
abline(h=0)
title(main="S&P 500 (Price Only) and US 1 Year Cumulative Return Since 1960",
adj=0.05,outer=TRUE,line=-2,cex.main=0.95)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.