Friday, October 21, 2011

Backtesting Part 4: random strategies

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.  
In part 2, we found that our 200-day high, hold 100 days strategy yielded average annual returns on the S&P 500 index of 7% in a backtest going back to 1950.  That's pretty good, but it's entirely possible that we got lucky and this was due to chance.  One way to test this is to compare our strategy to a strategy that randomly chooses long or short each day.  I tested just such a strategy on the S&P 500 index, again going back to 1950.  I also adjusted the returns series for splits and dividends, and assumed trade costs were 0.5%.  These are the exact same conditions I tested the "high-and-hold" strategy under.

Here is a histogram of average annual returns for these random strategies:

As you can see these strategies perform pretty dismally: the 0.5% trading cost quickly eats up all the available capital.

It would probably be a good idea to model a set dollar amount, and have trades costs a fixed amount, something like $7.  It might also be a good idea to make the random strategies auto-correlated, which would probably better reflect actual investor behavior. The code for this test is below the fold, if anyone wants to modify it and make these improvements.

Monday, October 17, 2011

Backtesting a Simple Stock Trading Strategy: Part 3

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.  


In a previous post, I examined a simple stock trading strategy: Find the high point over the last 200 days, and buy the stock if it's been less than 100 days since that high.  Otherwise, have no position.


What if we use different parameters than 200-day high and hold 100 days?  How will that affect our strategy?  First of all, we have to reload the data for the S&P 500 index and re-define the functions used to implement our strategy.


Next, we must decide the range of parameters we wish the test for our strategy.  I've decided to use a "grid search" to thoroughly examine the parameter space. Somewhat arbitrarily, I've decided to test the values from 5-500, by 5, for both parameters.  This gives us 100 possible values for each parameter, or 10000 total.  Good thing the "daysSinceHigh" function is pretty fast!



Sociable