Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is just a quick note to wish you and your family a very healthy and happy holidays and wonderful New Year! I hope you enjoyed reading my blog and thank you for your comments and emails.

Here is a short R code that implements an interesting idea from the Charting the Santa Claus Rally post by Woodshedder. I will plot and compare the SPY performance this December versus average performance in previous Decembers.

# Load Systematic Investor Toolbox (SIT)
setInternet2(TRUE)
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
source(con)
close(con)

#*****************************************************************
#******************************************************************
tickers = spl('SPY')

data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
bt.prep(data, align='remove.na', dates='1970::2011')

#*****************************************************************
# Prepare Data for the plot
#******************************************************************
prices = data\$prices
n = len(tickers)
ret = prices / mlag(prices) - 1

# find prices in December
dates = index(prices)
years = date.year(dates)
index = which(date.month(dates) == 12)

# rearrange data in trading days
trading.days = sapply(tapply(ret[index,], years[index], function(x) coredata(x)), function(x) x[1:22])

# average return each trading days, excluding current year

# cumulative
current.year = 100 * ( cumprod(1 + current.year) - 1 )

#*****************************************************************
# Create Plot
#******************************************************************
par(mar=c(4,4,1,1))
xlab = 'Number of Trading Days in December',
ylab = 'Avg % Profit/Loss'
)
lines(current.year, type='b', col=2)
grid()
plota.legend('Avg SPY,SPY Dec 2011', 1:2)


Hope this year will not disappoint and we will see the rally towards the year end.

If you want to find average performance in the other months, I recommend reading Trading Calendar article by CXO Advisory.

To view the complete source code for this example, please have a look at the bt.december.trading.test() function in bt.test.r at github.