Site icon R-bloggers

Backtesting a Simple Stock Trading Strategy

[This article was first published on Modern Toolmaking, 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.
Note: This post is NOT financial advice!  This is just a fun way to explore some of the capabilities R has for importing and manipulating data.  


I recently read a post on ETF Prophet that explored an interesting stock trading strategy in Excel.  The strategy is simple: Find the high point of the stock over the last 200 days, and count the number of days that have elapsed since that high.  If its been more less than 100 days, own the stock.  If it’s been more than 100 days, don’t own it.  This strategy is very simple, but it yields some impressive results. (Note; however, that this example uses data that has not been adjusted from splits or dividends and could contain other errors.  Furthermore, we’re ignoring trading costs and execution delays, both of which affect strategy performance.)

Implementing this strategy in R is simple, and provides numerous advantages over excel, the primary of which is that pulling stock market data into R is easy, and we can test this strategy on a wide range of indexes with relatively little effort.

First of all, we download data for GSPC using quantmod. (GSPC stands for the S&P 500 index). Next, we construct a function to calculate the number of days since the n-day high in a time series, and a function to implement our trading strategy.  The latter function takes 2 parameters: the n-day high you wish to use, and the numbers of days past that high you will hold the stock.  The example is 200 and 100, but you could easily change this to the 500-day high and see what happens if you hold the stock 300 days past that before bailing out.  Since this function is parameterized, we can easily test many other versions of our strategy.  We pad the beginning of our strategy with zeros so it will be the same length as our input data. (If you wish for a more detailed explaination of the daysSinceHigh function, see the discussion on cross-validated).


We multiply our position (0,1) vector by the returns from the index to get our strategy’s returns.  Now we construct a function to return some statistics about a trading strategy, and compare our strategy to the benchmark.  Somewhat arbitrarily, I’ve decided to look at cumulative return, mean annual return, sharpe ratio, winning %, mean annual volatility, max drawdown, and max length drawdown.  Other stats would be easy to implement.



Results:


As you can see, this strategy compares favorably to the default “buy-and-hold” approach.

Finally, we test our strategy on 3 other indexes: FTSE which represents Ireland and the UK, the Dow Jones Industrial Index, which goes back to 1896, and the N225, which represents Japan.  I’ve functionalized the entire process, so you can test each new strategy with 1 line of code:


Results:

FTSE:

DJIA:

N225:

The strategy performs well on the other indexes.  It even manages a positive return on the N225!

Feel free to test this strategy with other parameters, or on other indexes.  For homework, think of possible ways that I have fooled myself in this backtest, and post them in the comments.  One example of this is that we have’t looked at transaction costs, which might be significant…


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

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.