Saturday, January 14, 2017

Markets | Quick Take on Presidential Inauguration

Next week's Presidential Inauguration is a much awaited phenomenon - for general public as well as for the financial markets across the globe. Dow 20K is mostly an arbitrary mark for a market index designed for pre-computer era (and some equally arbitrary Theoretical Dow has already crossed the benchmark). But it appears the entire market is somewhat directionless at present. Since the election, it has made certain assumptions on the policies of the upcoming government and has shown some very strong move across asset classes (see here, here and here). However, we still have very little in terms of concrete policy direction to rely upon. The latest press conference did not quite live up to the expectation of details on policies. A strong guidelines on future policy in the inauguration can provide a new direction to the market one way or the other. And this can kick start the next phase in the market.

The charts below show the market impact of Presidential inauguration since the post-war era (excluding first term of Barack Obama, which was in many ways an outlier). The X-axis is the number of business days from the inauguration day. The chart on the right shows normalized moves of the S&P 500 Index from 3 month before to 3 month after for each inauguration. The chart on the left shows the median line and the uncertainty around it. It appears more often than not, the markets usually rallies in to the inauguration, experiences a slight correction going in to the exact date, and tops out  around 1 or 2 weeks after the actual date before picking up its own course. (Note we have not corrected for the usually positive trends for the markets in general and hence we should not focus much on the trends here but change in the direction of the trends instead.) However we have quite an amount of uncertainties around this. Looking closely at the right hand side chart we see this pattern was more or less followed by around 10 or 11 times out of last 17 cases. (The legends on the right chart are initials of the presidents followed by a digit signifying the term, if required)

Overall positioning-wise, we have nothing extreme in either way. Post elections the leveraged funds (CTAs and hedge funds) and asset managers have increased their long (from CFTC reports). The dealers have become slightly short the markets - but all well within range. On VIX, however the dealers and asset managers remain long against the leveraged players.

This, and trend analysis of the recent intraday movement of S&P 500 suggests the street (i.e. the players who hedge) is mostly long gamma at this point. See the chart below (and see here for interpretation). This means a large sell-off is quite unlikely in the short term. On top of this, we have the asymmetric scenario on the policy clarity. If President-elect Trump does announce clear guidelines on his policies, this will likely confirm the market assumptions (very low chance of a major negative surprise) and market can have the next leg of rally. On the other hands, impact of rhetorics and vagueness will most likely be muted as there is always the next time. This suggests a long positioning for the equities. However the case of dollar is quite different. We have a very strong long dollar positioning from the leveraged players and any disappointment can be felt quite hard in the dollars.

Finally, while you can't miss the obvious market reaction to Trump's win, it is fairly easy to miss - what I think the most dramatic - real economy reaction. The NFIB small business optimism and outlook went over the top following the election, much more than the overall business outlook and optimism measures. The charts shows the standardized measure and the spread.

I think in itself, this is quite significant. Historically, we have only two similar situations when the business indicators were significantly positive and small business optimism outperformed overall measures. Once was during the recovery of early 90s and secondly during the recovery of early 2000s. While we have too few data points to draw any statistical conclusion, in both cases we had sustained economic improvement and overall positive market performance. Of course small business optimism does not necessarily mean it will be realized, nor what is good for small businesses is also necessarily good for overall markets. But perhaps we have too many people bracing for a crash now?

Wednesday, January 4, 2017

Systematic Trading: Back-testing Classical Technical Patterns

Following up from my last post on systematic pattern identification in time series, here is the part on identifying and back-testing classical technical analysis patterns. This is based on the classic paper by Lo, Mamaysky and Wang (2000). The major improvement added here lies in defining local extrema in terms of perceptually important points (as opposed to the kernel regression based slope change technique proposed in the paper). In my view, the kernel method can be too noisy and much less robust with real data.

The R package techchart has two functions for identifying classical technical patterns. The function find.tpattern will sweep through the entire time series and find all pattern matches. It takes in the time series as the first parameter (an xts object), a pattern definition to search for, and a couple of tolerance parameters. The first one is used for matching the pattern itself. The second one pip.tolerance is used for finding the highs and the lows (perceptually important points) on which the pattern matching is based. These tolerance numbers are in terms of multiple of standard deviation. Below is an example:

x <- getSymbols("^GSPC", auto.assign = F)
tpattern <- find.tpattern(x["2015"], tolerance = 0.5, pip.tolerance = 1.5)
chart_Series(x["2015"])


add_TA(tpattern$matches[[1]]$data, on=1, col = alpha("yellow",0.4), lwd=5)


Apart from returning the pattern matches, it also returns some descriptions and characteristics of the match. As below:

summary(tpattern)

## ------pattern matched on: 2015-06-23 --------
## type: complete
## move: 1.49 (percentage annualized)
## threshold: 2079.52
## duration: 57 (days)


While this is useful, you already must have spotted the catch. As this function looks at all available data at once to find a pattern, future prices influences past patterns. While this is useful for looking at a time series we need another function for rigorous back-testing. The second function available, find.pattern is to be used for this purpose. This function takes in similar arguments. It returns matched patterns. The match is based on either a completed pattern, or a forming one. A forming pattern is extracted by bumping the last closing price up or down by 1 standard deviation in the next bar and checking if it completes the pattern.

The process of identification of pattern is decoupled from the process of extracting patterns from the data - as proposed in the Lo et al (2000). The pattern defining function in the package is pattern.db.  This follows a similar implementation as here by Systematic Investor Blog, with some added features. The implementation of pattern.db in the package techchart contains some basic patterns - head and shoulder (HS), inverse head and shoulder (IHS), broadening top (BTOP) and broadening bottom (BBOT) - the default in the above functions being HS. However it is trivial to define any pattern (as long as it can be expressed in terms of local highs and lows) and customize this pattern library.

With this framework, it becomes quite straightforward to test and analyze pattern performance, run back-test on pattern based strategies and/ or combine patterns along with other indicators to devise trading strategies at any given frequency.

Here is a straightforward implementation of such a back-test, using the quantstrat package. The strategy is quite straightforward. For a given underlying, we scan data for a head-and-should (or inverse head-and-shoulder) match. Once we find a match, we enter a short (long) position if a short term moving average is below (above) a long term one. Once we enter in to a short (long) position, we hold it for at least 5 days, and exit on or after that if a short term moving average is above (below) a long term one. We apply this strategy across S&P500, DAX, Nikkei 225 and KOSPI. The chart below shows the strategy performance.

The thick transparent purple line is the average performance across these underlying indices.  The performance metrics are as below. It also has (not shown here) a strong positive skew characteristics.

 Performance metrics S&P DAX NKY KOSPI ALL Annualized Return 0.0566 0.0536 0.0678 0.0528 0.0639 Annualized Std Dev 0.1233 0.0982 0.1413 0.1205 0.0692 Annualized Sharpe (Rf=0%) 0.4591 0.546 0.4797 0.4382 0.9234

Not spectacular, but nonetheless interesting. The R code for this back-test is here. Apart from techchart, you would need to install quantmod and quantstrat (and associated packages) to run this. Please note, running this pattern finding algorithm can take considerable time depending on the length of the time series and system characteristics.