With the Brexit upon us and the United Kingdom voting to leave the European Union, how will the domestic stock market behave? Some people believe that the market will take a hit.
If the market takes a dip, I want to be somewhat ready to take advantage of the buying opportunity.
My theory is that domestic stocks or ETFs should not get effected too much, but if they do, I’d like to know how they may behave after taking a 3% dip.
I started by getting a short list of domestic ETFs that are fairing well. I got the information from TheStreet, but I filtered out some the the symbols.
library(quantmod)
library(plyr)
library(timeSeries)
library(ggplot2)
library(tidyr)
library(TechnicalEvents)
stocks <- read.table("domesticGrowthEtf.tsv", header=FALSE, sep="\t", skip = 1)
colnames(stocks) <- c("fund", "symbol", "rating", "risk")
stocks
## fund symbol rating risk
## 1 Guggenheim S&P 500 Eq WgCon St ETF RHS A+ B+
## 2 PowerShares S&P SC Utilities PSCU A+ B
## 3 PowerShares S&P MidCap Low Vol XMLV A+ B
## 4 PowerShares S&P SC Cnsmr Staples PSCC A+ B
## 5 Consumer Staples Select Sector SPDR XLP A+ B
## 6 PowerShares S&P SC Information Tech PSCT A+ B
## 7 Powershares S&P 500 Low Vol Port SPLV A+ B+
#' Example symbols with one invalid
symbols <- as.character(stocks[, "symbol"])
#' Create a new environment to store the data
data.env <- new.env()
from.dat <- as.Date("01/01/07", format="%m/%d/%y")
to.dat <- as.Date("6/23/16", format="%m/%d/%y")
#' Get the stock quotes of all valid symbols. Skip over the invalid
#' symbols. Get a list of symbols that downloaded correctly.
sym.list <- llply(symbols, function(sym) try(getSymbols(sym, env = data.env, from = from.dat, to = to.dat)))
symbols <- symbols[symbols %in% ls(data.env)]
I created a couple of functions to extract the data after each instance of the event and plot the data.
GetAdjReturnsAfterEvent <- function(sym, env, change = -3, win = 14){
sym.data <- get(sym, env)
if(nrow(sym.data) > 50){
events.buy <- GetPercentageChangeEvents(Ad(sym.data), change)
events <- index(events.buy[events.buy[,1] == TRUE])
event.rets <- lapply(events, function(x) GetWindowCumulativeReturns(Ad(sym.data), x, win))
if(length(event.rets) > 0){
event.rets
}
}
}
plotReturns <- function(x, title, win){
keycol <- "period"
valuecol <- "returns"
gathercols <- paste("V", seq(1:win), sep="")
test <- gather_(x, keycol, valuecol, gathercols)
test$period <- gsub("V", "", test$period)
test$period <- as.numeric(test$period)
ggplot(test, aes(period, returns)) +
geom_point(alpha=0.1) +
geom_smooth() +
ggtitle(title) +
geom_hline(yintercept=0, colour="red")
}
We will take a look at the 14 sessions following each event (3% drop) and see how the stock faired by looking at the cumulative returns.
For all stocks under review:
win = 14
change = -3
adjsraw <- lapply(symbols, function(x){ GetAdjReturnsAfterEvent(x, data.env, change, win) })
adjsfull <- unlist(adjsraw, recursive = FALSE)
adjs <- as.data.frame(matrix(unlist(adjsfull), ncol = win, byrow=TRUE))
plotReturns(adjs, "All Stocks", win)
The results as a whole are not that impressive, so let’s look at the individual stocks.
for(symbol in symbols){
adj <- GetAdjReturnsAfterEvent(symbol, data.env, change, win)
adjs <- as.data.frame(matrix(unlist(adj), ncol = win, byrow=TRUE))
print(plotReturns(adjs, symbol, win))
}
Without doing more research and evaluation, but I thought this would be interesting.
Justin Nafe June 24th, 2016
Posted In: Exploratory Analysis
Tags: technical events