In Recession Forecasting Part II, I compared the accuracy of Hussman's recession forecasts to the accuracy of a naive forecast that assumed the current state of the recession variable would continue next month.
An anonymous commentator pointed out a great error in the naive forecast, which is that the NBER BACKDATES their recession indicator. In other words, a recession may start, but you would not know about it for up to 6 months. Therefore, assuming the naive forecast would know of the recession in the second month is akin to predicting the future.
This creates the need for a naive forecast that only has access to the information available at a given point in time. Fortunarly, our anonymous commentator kindly provided this data, which he constructed using the recession announcement dates from the NBER. Previous to 1980, he assumed a 6-month lag in identifying turning points. This data is available as an excel spreadsheet (with commentary) and a csv file (suitable for import into R).
Practical tools for predictive modeling, data science, machine learning and web scraping
Tuesday, September 20, 2011
Friday, September 16, 2011
Backtesting Part 2: Splits, Dividends, Trading Costs and Log Plots
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.
UPDATE 9/21/2011: Costas correctly points out that I should lag my strategy vector by one day, as today's returns are determined the position we chose yesterday. I updated the code, results, and charts to reflect this. Please alert me to any similar errors in the future.
In my last post, I demonstrated how to backtest a simple momentum-based stock trading strategy in R. However, there were a few issues with my implementation: I ignored splits, dividends and transaction costs, all of which can have a large impact on a strategy. I also came up with a better plot to help show how a given strategy compares to a benchmark, and wrapped everything together into one function.
UPDATE 9/21/2011: Costas correctly points out that I should lag my strategy vector by one day, as today's returns are determined the position we chose yesterday. I updated the code, results, and charts to reflect this. Please alert me to any similar errors in the future.
In my last post, I demonstrated how to backtest a simple momentum-based stock trading strategy in R. However, there were a few issues with my implementation: I ignored splits, dividends and transaction costs, all of which can have a large impact on a strategy. I also came up with a better plot to help show how a given strategy compares to a benchmark, and wrapped everything together into one function.
Labels:
backtesting,
finance,
R,
Stocks
Thursday, September 15, 2011
Correlations among US Stocks: Is it really time to fire your adviser?
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.
The Financial Times says it's time to "Fire your Adviser" because correlations among US stocks are at their highest levels since the financial crisis. Unfortunately, they only provide data going back 3 months and it's in a boring table, rather than an awesome chart. After reading this article, I immediately pulled out quantmod and PerfomanceAnalytics in R. I used quantmod to download the daily data (from yahoo finance) for each index listed in the article, and then used PerformanceAnalytics to analyze and graph it. After and hour or so of fiddling around, I ended up with this chart:
The Financial Times says it's time to "Fire your Adviser" because correlations among US stocks are at their highest levels since the financial crisis. Unfortunately, they only provide data going back 3 months and it's in a boring table, rather than an awesome chart. After reading this article, I immediately pulled out quantmod and PerfomanceAnalytics in R. I used quantmod to download the daily data (from yahoo finance) for each index listed in the article, and then used PerformanceAnalytics to analyze and graph it. After and hour or so of fiddling around, I ended up with this chart:
Tuesday, September 13, 2011
Backtesting a Simple Stock Trading Strategy
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.
UPDATE 9/21/2011: Costas correctly points out that I should lag my strategy vector by one day, as today's returns are determined the position we chose yesterday. I updated the code, results, and charts to reflect this. Please alert me to any similar errors in the future.
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 the high 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).
UPDATE 9/21/2011: Costas correctly points out that I should lag my strategy vector by one day, as today's returns are determined the position we chose yesterday. I updated the code, results, and charts to reflect this. Please alert me to any similar errors in the future.
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 the high 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).
Labels:
backtesting,
finance,
R,
Stocks
Subscribe to:
Posts (Atom)
