Retail Relative Strength

[This article was first published on Timely Portfolio, 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.

So back before Thanksgiving I did a post Something to Think About Before Black Friday | rChart + dygraphs.  Well since then I have noticed that Retail relative strength has deteriorated considerably.  I thought it would be a good time to use good old bfast (see posts) to look for a structural breakpoint.  Below is the output.  And yes, I do plan to make interactive eventually with rCharts.

image

The remainder is what I think is very interesting.

image

Code:

#analyze breakpoints with the R package bfast
#please read the paper
#Verbesselt J, Hyndman R, Newnham G, Culvenor D (2010)
#Detecting Trend and Seasonal Changes in Satellite Image Time Series.
#Remote Sensing of Environment, 114(1), 106–115.
#http://dx.doi.org/10.1016/j.rse.2009.08.014
require(bfast)
require(quantmod)
getSymbols("XRT",from="2000-01-01")
getSymbols("SPY",from="2000-01-01")
#get relative strenght as weekly log price
xrt_rs <- log(to.weekly(XRT[,6] / SPY[,6])[,4])
#need ts representation so do some brute force conversion
xrt_rs.ts <- ts(
as.vector(xrt_rs),
start=c(
as.numeric(format(index(xrt_rs[1,]),"%Y")), #year
as.numeric(format(index(xrt_rs[1,]),"%m")) #month
),
frequency=52
)
#look at the stl Seasonal-Trend decomposition procedure already in R
xrt_rs.stl <- stl(xrt_rs.ts,s.window="periodic")
plot(xrt_rs.stl,main="STL Decomposition of S&P 500")
#get the results from bfast
#adjusting h lower will result in more breakpoints
xrt_rs.bfast <- bfast(xrt_rs.ts,h=0.15,max.iter=1,season="none")
plot(
xrt_rs.bfast,
type="components",
ylim=c(3,max(xrt_rs)+1),
main="Retail Rel Strength with bfast Breakpoints and Components"
)
plot(
xrt_rs.bfast,
type="trend",
ylim=c(3,max(xrt_rs)+1),
main="Retail Rel Strength with bfast Trend Breakpoints"
)
#do some additional plotting
#[[1]] is used since for speed I only did one iteration
#could plot each iteration if I did multiple
plot(xrt_rs.bfast$Yt/xrt_rs.bfast$output[[1]]$Tt-1,
main="bfast Remainder as % of Retail Rel Strength",
xlab=NA, ylab="remainder (% of rel strength)",bty="l")
#add vertical line for the breakpoints
abline(v=breakdates(xrt_rs.bfast$output[[1]]$bp.Vt),col="gray70")
#add horizontal line at 0
abline(h=0,col="black",lty=2)
text(x=breakdates(xrt_rs.bfast$output[[1]]$bp.Vt),y=par("usr")[3]+.01,
labels=breakdates(xrt_rs.bfast$output[[1]]$bp.Vt,format.times=TRUE),
srt=90,pos=4,cex=0.75)
view raw code.R hosted with ❤ by GitHub

To leave a comment for the author, please follow the link and comment on their blog: Timely Portfolio.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)