Saturday 14 March 2015

FOMC Cycle Trading Strategy in Quantstrat

Another hotly anticipated FOMC meeting kicks off next week, so I thought it would be timely to highlight a less well-known working paper, “Stock Returns over the FOMC Cycle”, by Cieslak, Morse and Vissing-Jorgensen (current draft June 2014). Its main result is:

Over the last 20 years, the average excess return on stocks over Treasury bills follows a bi-weekly pattern over the Federal Open Market Committee meeting cycle. The equity premium over this 20-year period was earned entirely in weeks 0, 2, 4 and 6 in FOMC cycle time, with week 0 starting the day before a scheduled FOMC announcement day.

The paper can be downloaded from here http://faculty.haas.berkeley.edu/vissing/CieslakMorseVissing.pdf.

In this post, we’ll look to recreate their cycle pattern and then backtest a trading strategy to test the claim of economic significance. Another objective is to evaluate the R package Quantstrat “for constructing trading systems and simulation.”

Data

Although the authors used 20 years of excess return data from 1994 to 2013, instead we’ll use S&P500 ETF (SPY) data from 1994 to March 2015 and the FOMC dates (from my previous post here http://www.returnandrisk.com/2015/01/fomc-dates-full-history-web-scrape.html).

As there is not a lot of out-of-sample data since the release of the paper in 2014, we’ll use all the data to detect the pattern, and then proceed to check the impact of transaction costs on the economic significance of one possible FOMC cycle trading strategy.

+ Show R code to setup and pre-process the required data

FOMC Cycle Pattern

The chart and table below clearly show the bi-weekly pattern over the FOMC Cycle of Cieslak et al in SPY 5-day returns. This is based on calendar weekdays (i.e. day count includes holidays), with week 0 starting one day before a scheduled FOMC announcement day (i.e. on day -1). Returns in even weeks (weeks 0, 2, 4, 6) are positive, while those in odd weeks (weeks -1, 1, 3, 5) are lower and mostly slightly negative.

Table of Returns by FOMC Week, Days & Phase

Week Days Phase Average 5-day Return (%)
-1 -6 to -2 Low 0.14
0 -1 to 3 High 0.59
1 4 to 8 Low -0.05
2 9 to 13 High 0.32
3 14 to 18 Low -0.12
4 19 to 23 High 0.45
5 24 to 28 Low -0.10
6 29 to 33 High 0.69
+ Show R code for the custom indicator function for the FOMC cycle and the above chart

Economic Significance: FOMC Cycle Trading Strategy Using Quantstrat

In this section, we’ll create a trading strategy using the R Quantstrat package to test the claim of economic significance of the pattern. Note, Quantstrat is “still in heavy development” and as such is not available on CRAN but needs to be downloaded from the development web site. Nonetheless, it's been around for some time and it should be up to the backtesting task…

Based on the paper’s main result and our table above confirming the high-phase is more profitable, we’ll backtest a long only strategy that buys the SPY on even weeks (weeks 0, 2, 4, 6) and holds for 5 calendar days only, and compare it to a buy and hold strategy. In addition, we’ll look at the effect of transaction costs on overall returns.

A few things to note:

  • We’ll use a bet size of 100% of equity for all trades. This may not be optimal in developing trading systems but will allow for easy comparison with the buy and hold passive strategy, which is 100% allocated
  • Assume 5 basis points (0.05%) in execution costs (including commission and slippage), and initial equity of $100,000
  • Execution occurs on the close of the same day that the buy/sell signal happens. Unfortunately, Quantstrat does not allow this out-of-the-box, so we need to do a hack - a custom indicator function that shifts the signals forward in time (see “get.fomc.cycle” function above)

The following are the resulting performance metrics for the trading strategy, using 5 basis points for transaction costs, and comparisons with the passive buy and hold strategy (before and after transaction costs).

Summary Performance for Trading Strategy

##                           return
## Annualized Return         0.0855
## Annualized Std Dev        0.1382
## Annualized Sharpe (Rf=0%) 0.6183

Trade Statistics

##     Symbol Num.Trades Percent.Positive Net.Trading.PL Profit.Factor
## spy    spy        525         59.61905       468196.8      1.587279
##     Max.Drawdown
## spy    -107436.8

Monthly Returns

##       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug   Sep  Oct  Nov  Dec Total
## 1994   NA -2.4 -1.8  0.5  1.2  2.6  0.1 -0.9   0.2  5.7  0.3  0.4   5.8
## 1995  0.9  3.2  0.3  1.9  1.8  0.8  2.1  0.1   1.7 -0.9  1.0  0.2  13.9
## 1996  3.6 -3.3  1.5 -1.6  2.9  1.3 -0.8  3.1   0.8 -1.5  1.2 -0.3   6.9
## 1997  3.1 -0.3  0.4 -3.5  3.1  1.5  3.6  2.8   5.8 -2.6  3.1  5.3  24.1
## 1998  4.5  3.2  2.5  3.7  1.1  1.8  1.2 -5.6  -0.1  9.4  0.0  5.4  29.6
## 1999  1.9  0.9  2.2 -3.8 -0.5  8.9  0.8  3.2  -4.7  8.3  0.9  5.8  25.4
## 2000 -2.7 -0.3  6.1  6.3  1.0  2.9  0.6  0.3  -2.5  2.3 -3.7  0.6  10.8
## 2001  0.1 -6.1  0.0  2.2  2.3 -2.7  3.5  0.8 -15.7  0.5  4.2 -2.2 -13.9
## 2002 -3.1 -3.9  2.5 -0.5 -3.9 -2.1  0.6 -1.1  -2.5  5.8 -0.1 -5.9 -13.8
## 2003  2.6  1.5  3.4  6.8 -0.9  3.4  1.4  2.9   1.4  4.6  1.8  1.7  35.2
## 2004 -1.3  1.2  1.6  0.7 -1.3  0.6 -1.2  1.0  -0.9 -2.4  1.6  0.4   0.0
## 2005 -0.6  1.2  0.4  1.5  3.3  0.9  1.8 -1.4   0.0 -3.2  1.2 -0.3   4.6
## 2006 -0.8 -0.4  1.9 -0.6 -0.6  0.3 -1.5 -1.8  -0.8  1.6 -1.3  1.6  -2.6
## 2007  2.6 -0.8 -0.6  2.3 -1.0 -0.4 -0.7  2.9   1.3 -2.1  2.1 -3.2   2.2
## 2008  0.5 -1.4  2.3  6.4  5.6 -4.2 -2.5  1.9  -8.4 15.3 -2.2 -5.9   5.5
## 2009 -2.8 -6.4  5.0  3.9  7.8  4.7  3.5 -3.3   2.8  0.6  4.4 -0.5  20.5
## 2010 -1.7  3.0  2.1 -1.8  0.6  0.0  9.7 -4.4   2.4  2.0  2.6  4.0  19.5
## 2011  1.2 -0.1 -1.1  1.7 -0.5 -1.1  0.8  2.8  -5.2 13.3  0.0 -4.0   6.9
## 2012  0.5  1.0  3.1  0.1 -2.3  0.0 -2.1  1.9   1.6 -1.4 -1.4  0.8   1.8
## 2013  0.3  1.7  0.7  0.8  2.0 -3.6  2.2 -3.0   0.1  1.2  1.8  2.4   6.5
## 2014 -0.3  0.5 -0.9  1.3  0.5  1.2 -1.8  2.3   0.1  3.8  0.0  2.1   9.0
## 2015 -5.4  3.3  0.6   NA   NA   NA   NA   NA    NA   NA   NA   NA  -1.7

Summary Performance for Benchmark Buy and Hold Strategy

##                           return
## Annualized Return          0.0915
## Annualized Std Dev         0.1935
## Annualized Sharpe (Rf=0%)  0.4727

Comparison of Trading Strategy with Buy and Hold (BEFORE transaction costs)

Comparison of Trading Strategy with Buy and Hold (AFTER transaction costs)

+ Show R code for the trading strategy using Quantstrat

Conclusion

FOMC Cycle Pattern

We were able to clearly see the bi-weekly pattern over the FOMC cycle using SPY data, a la Cieslak, Morse and Vissing-Jorgensen.

Economic Significance: FOMC Cycle Trading Strategy

Before transaction costs, we were able to reproduce similar results to the paper, with the long only strategy of buying the SPY in even weeks and holding for 5 days. In our case, this strategy added about 2% p.a. to buy and hold returns, reduced volatility by 30% and increased the Sharpe ratio by 70% to 0.82 (from 0.47).

However, after allowing for a reasonable 5 basis points (0.05%) in execution costs, annualized returns fall below that of the buy and hold strategy (9.15%) to 8.55%. As volatility remains lower, this means the risk-adjusted performance is better by only 30% now (Sharpe ratio of 0.62). See table below for details.

Buy and Hold Long Only before Transaction Costs Long Only with 5bp Transaction Costs
Annualized Return 0.0915 0.1129 0.0855
Annualized Std Dev 0.1935 0.1382 0.1382
Annualized Sharpe (Rf=0%) 0.4727 0.8169 0.6183

Execution costs (brokerage and slippage) can have a material impact on trading system performance. So the key takeaway is to be explicit in accounting for them when claiming economic significance. There are a lot of backtests out there that don’t…

Quantstrat

There is a bit of a learning curve with the Quantstrat package but once you get used to it, it’s a solid backtesting platform. In addition, it has other capabilities like optimization and walk-forward testing.

The main issue I have is that it doesn’t natively allow you to execute on the daily close when you get a signal on that day’s close - you need to do a hack. This puts it at a bit of a disadvantage to other software like TradeStation, MultiCharts, NinjaTrader and Amibroker (presumably MatLab too). Hopefully the developers will reconsider this, to help drive higher adoption of their gReat package…

Click here for the R code on GitHub.

10 comments:

  1. It's a feature, not a bug that quantstrat requires a "hack" to allow you to execute on the same timestamp as a signal. It is potentially very dangerous to assume that you will be able to enter an order at the same time you generate a signal.

    You can make the argument that the assumption isn't as dangerous on lower periodicity data, but we believe the defaults should be more conservative, not less.

    ReplyDelete
  2. Thanks for the comment. I think the crux of the matter is whether you have price-based vs time-based entries/exits. Agree with you on the potential for negative surprises if you assume you can execute on the close of a close price-based signal – although this can be mitigated in production by calculating the indicators in real-time (assuming the strategy passes all the development backtests).

    However, for time-based entries/exits (of which the FOMC cycle strategy is one), it’s certainly valid to allow execution on the same bar close, cos you know it in advance e.g. sell on T+5 close. The question then is not if you will trade but at what price relative to the close. And my preferred approach for dealing with this is to set the price as the close and handle slippage/implementation shortfall in the execution cost function.

    I’m not saying it should be the default, but the option to choose would sure simplify coding.

    ReplyDelete
  3. How you generate the signal doesn't change the fact that it is impossible to execute on the same timestamp as used to generate the signal. It doesn't matter if you calculate indicators in real-time in production. There's still a non-zero calculation time. Even if it only takes a few microseconds, it's still not the same timestamp.

    quantstrat was written to model signals and orders as realistically as possible. Assuming execution on the same timestamp as a signal is not realistic.

    Like I said in my prior comment, you can argue that execution on the same timestamp as the signal isn't as unrealistic for lower-periodicity data, but it's still not realistic. For example, what if you have 1-second bars instead of daily?

    And there's already an option to do what you want. It's "allowMagicalThinking=TRUE". I forget where you need to set it, because I never use it. It will probably get passed via "...", so you could try adding it to applyStrategy. I assumed this was what you were using when you said "hack".

    ReplyDelete
  4. Good to hear that the option does exist but I didn’t see it documented if it is (noted that Quantstrat is still under heavy development and that you want to discourage such use).

    Like I said, for this particular strategy, it’s reasonable to use it because these are time-based signals e.g. exit after 5 days. Indeed, in real life there exists the Market-On-Close (MOC) order type for executing on the daily close. No magical thinking needed, and yes I do use them e.g. on Monday 23 March in the SPY my MOC order was executed at the closing price of 210 and the Interactive Brokers (IB) timestamp was 4pm exactly.

    You can see a video here from IB on this order type http://ibkb.interactivebrokers.com/video/1232.

    And for more detailed info on this market microstructure topic, here’s a quick guide from NASDAQ on “The Closing Cross” https://www.nasdaqtrader.com/content/TechnicalSupport/UserGuides/TradingProducts/crosses/openclosequickguide.pdf.

    Of course any one contemplating using MOC orders should do their own homework as mileage may vary depending on the strategy, exchange, instrument, liquidity etc…

    ReplyDelete
    Replies
    1. My point still holds. You must enter your MOC order before the close of the session you want it to execute in. A realistic simulation environment should reflect that.

      While it may not be unreasonable in this very specific situation (time-based entries and exits using only market orders), it's still extremely bad process and potentially dangerous. What if someone takes this strategy (with allowMagicalThinking=TRUE) and modifies it? Maybe they add a non-time-based signal. Maybe they use an order other than market? Now they're in a bad spot and they likely don't even know it.

      Delete
  5. To be clear, for this strategy it is definitely reasonable (nor is it bad process or dangerous) as the signals are known well in advance (days) before any trading takes place. I agree that this may not be the case in general, hence my previous comment that one needs to do one’s own homework accordingly.

    ReplyDelete
    Replies
    1. This is going to be my last comment. Feel free to have the last word.

      I agreed that it may be reasonable for this specific strategy, but I still disagree that assuming execution on the same timestamp as a signal (and order entry) is not bad process nor dangerous (that is to say that it is bad process and dangerous).

      The assumption being reasonable for this specific strategy is an exception, not the rule, and exceptions should not dictate process. Process should be independent of any specific strategy.

      Delete
    2. Thanks for your comments. Much appreciated.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Just looking at the chart, I was wondering if the fomc returns could be used as a forward indicator of market health. Seems like when the fomc curve starts outperforming the regular SPY returns, the market is tanking. I just can't tell from the low res graph whether this precedes or follows market bad behavior. So I thought I'd propose it as a possibility.

    ReplyDelete