[This article was first published on Milk Trader, 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.
I bought a lottery ticket yesterday. I hardly ever buy them. The last time I did I lost a dollar. Actually, every time I’ve bought a ticket I’ve lost. Yesterday I was at the local gas station in line behind a bloke who had a comprehensive folder of tickets. Kinda like those extreme coupon people, but he had lotto tickets instead. I thought to myself “why not?” This morning I checked the numbers. I lost my dollar. I hope whoever won it is happy to have it. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
To leave a comment for the author, please follow the link and comment on their blog: Milk Trader.
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.