We are all compelled to use our experiences as factors to our judgment. There is no guarantee that history repeats itself, of course. But it's good to have some perspective on last time something happened. Sort of like the meltup we had the last four days. The S&P 500 returned over 0.85 percent for four days in a row. Wow. When was the last time that happened?
Well, good thing we have R to do some data mining for us. Here is a nifty function that returns a data series complete with my own preferences on how to view things. It defaults to using the Federal Reserve of St. Louis for its data source. That takes us back to 1957.
future <- function(sym="SP500", source="FRED"){
require("quantmod")
x <- getSymbols(sym, src=source, auto.assign=FALSE)
x$ret <- dailyReturn(x, type="log")
x$ago.1 <- lag(x$ret, k=1)
x$ago.2 <- lag(x$ret, k=2)
x$ago.3 <- lag(x$ret, k=3)
x$ago.4 <- lag(x$ret, k=4)
x$next.1 <- lag(x$ret, k=-1)
x$next.2 <- lag(x$ret, k=-2)
x$next.3 <- lag(x$ret, k=-3)
x$next.4 <- lag(x$ret, k=-4)
x$next.5 <- lag(x$ret, k=-5)
x$next.6 <- lag(x$ret, k=-6)
x$next.7 <- lag(x$ret, k=-7)
x$next.8 <- lag(x$ret, k=-8)
x$in.1 <- exp(x$next.1)-1
x$in.2 <- exp(x$next.1 + x$next.2)-1
x$in.3 <- exp(x$next.1 + x$next.2 + x$next.3)-1
x$in.4 <- exp(x$next.1 + x$next.2 + x$next.3 + x$next.4)-1
x$in.5 <- exp(x$next.1 + x$next.2 + x$next.3 + x$next.4 + x$next.5)-1
x$in.6 <- exp(x$next.1 + x$next.2 + x$next.3 + x$next.4 + x$next.5 + x$next.6)-1
x$in.7 <- exp(x$next.1 + x$next.2 + x$next.3 + x$next.4 + x$next.5 + x$next.6 + x$next.7)-1
x$in.8 <- exp(x$next.1 + x$next.2 + x$next.3 + x$next.4 + x$next.5 + x$next.6 + x$next.7 + x$next.8)-1
food <- paste(sym,"future.returns", sep=".")
assign(food, x, envir=.GlobalEnv)
}
If you run this program, you will get an unwieldy monstrosity. First, re-assign the name of the object returned by the function to S. Then, you need to do a little indexing to get the data you want. To get only those days where the last four days each had returns that exceeded 0.85%, run this little command from the R command prompt:
s <- S[(exp(S$ret)-1) > .0085 & (exp(S$ago.1)-1) > .0085 & (exp(S$ago.2)-1) > .0085 & (exp(S$ago.3)-1) > .0085 ]
It's a little verbose, but I prefer using log returns so sometimes it requires a little extra jumping around. This gives you a smaller data set, of length 9. We are interested in next.1, which are the returns the next day. Again with the dog-and-pony show with logs, but it must be done. It makes sense to plot it with a histogram, so let's do that all with one fell swoop.
hist((exp(s$next.1)-1), col="burlywood", main="Next Day Returns After Four Consecutive Up Days\n of Over 0.85% Each", xlab="1957 to Present")
And here is the resulting chart:
There you have it. Seven out of nine times the next day is a loser. Even with those odds, I would hesitate to bet a dollar on it though. Okay, maybe one dollar.

Thanks for this. "If you run this program, you will get an unwieldy monstrosity. First, re-assign the name of the object returned by the function to S" How do I do that?
ReplyDelete@Rob just copy and paste the function into R.
ReplyDeletegreat function, thanks for the post..
ReplyDelete@Rob, I think i understand where you are coming from.. what you need to do is find out what the output is being assigned to.. so for me, i had to run
S <- SP500.future.returns
SP500.future.returns is the output from the function.. thats the 'food'
You can also refactor the last line of code in the blog that reads hist((exp(s$next.1)-1)...) to be simply hist(s$in.1 ... ). This is a special case where the two are equivalent because the cumulative return only considers one day.
ReplyDeleteThere are perils in jumping in and out of logarithms, but it keeps the braincells firing.
Thank you.
ReplyDelete