I’ve ordered Time Series Analysis and Its Applications: With R Examples (Springer Texts in Statistics) to help me up the time series in R learning curve. So far what I have seen it looks good. The author has a good page with the issues in R and time series. The book should arrive by the end of the week.
In the meantime, I came across a trading strategy while reading an article provide on John Mauldin’s “Over My Shoulder” service (which I highly recommend). The crux of it was that in the bear market that started with the tech bubble crash, a strategy of betting on mean reversion of the S&P500 generated significant returns. Naturally I wanted to test.
Please note, I am not recommending anything that follows. Do your homework and speak with an investment professional if you have questions.
The strategy is to go long the S&P500 when the market closes at a maximum over the previous 3 days. Reverse the trade and go long when the market closes at the minimum over the previous 3 days. ETFs make this strategy relatively easy to trade. SPY will be our vehicle for being long the S&P500 and SH will be our vehicle for going short.
The SH began trading on 06/21/2006. We focus our backtesting from that point until now.
Using the importSeries() function we previously created, get all the values for SPY and SH.
We need to create some additional timeSeries to hold
 Long/Short Flag — lets us know the current status of our holdings.
 Trade Flag — signals that we instituted a trade on this date.
 Strat.Returns — nominal return for the day with the strategy.
 Dollar Amount — a gross dollar value of the portfolio assuming a $10,000 dollar value on 06/21/2006, and a $2 transaction fee when we trade.

spy.Return

sh.Return

Strat.DollarReturns

Annualized Return

0.0067

0.0903

0.3535

Annualized Std Dev

0.2529

0.2512

0.2508

Annualized Sharpe (Rf=0%)

0.0263

0.3593

1.4092


Strat.Return to spy.Return

Alpha

0.0013

Beta

0.1921

Beta+

0.6830

Beta

0.0803

Rsquared

0.0374

Annualized Alpha

0.3990

Correlation

0.1934

Correlation pvalue

0.0000

Tracking Error

0.7447

Active Premium

0.3694

Information Ratio

0.4960

Treynor Ratio

1.8885

 It should be noted that this strategy is NOT tax efficient — any gains will be taxed at the short term capital gains rate.
 There were 411 trades. A trade involves buying and selling, so 822 times would you be charged a brokerage fee. I assumed 1 dollar per buy/sell — what is charged by Interactive Brokers. Using someone like TD Ameritrade would cost FAR more.
 This also assumes that you can buy and sell at the market closing price. Something that is possible, but slippage will occur.
Rbloggers.com offers daily email updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...