Tag: quant

Mixture model over S&P 500 returns

Market returns have different characteristics depending on whether they are in a “bull” phase or a “bear” phase. Daily returns can be labeled as either belonging to the “bull” camp or the “bear” camp using mixture models. This post extends Eran Raviv’s idea described here.

Rolling vs. whole period analysis

The density plot of daily returns of the distributions fit by the mixture model using the entire data-set of daily returns looked promising. There was a very visible difference in the way returns behaved under the two regimes. However, rolling period analysis hews closer to the real world. And the densities don’t look as pretty:
density plot of returns in stable and unstable regimes

Sure, “unstable” or “bear” regimes have slightly fatter tails but the densities are not as different as they appeared to be in the whole-period analysis.

Another gotcha is that when the regimes are superimposed on the S&P 500 price index, it looks like it could be good idea to use this system to trade it:
S&P 500 regimes
1 (blue) => stable

Timing signal?

It looks like the model helped escape most of the 2008 carnage and the “stable” regime looks to be long most of the up-trends. However, the overall return profile is sub-par when used in a systematic trading strategy:
S&P 500 cumulative returns

Take-away

Mixture models are an interesting tool in the quant tool-box. However, like how using skew as a timing signal appeared to be a good idea on the face of it, it turns out that using mixture models to time trades in a linear fashion is not such a good idea.

Code and charts on github.

Nifty Gaps

There is always a time alignment problem when working with global data. For example, Nifty closes a day ahead of the S&P for the same closing date. There are two ways to get around this “rolling close” problem:

  1. Shift up the the lagging data to align the times zones.
  2. Instead of using close-to-close, use open-to-close on the data that is ahead.

But how big a problem is this? If the model uses a weekly time-series, how much of a difference would trading on Friday close vs. Monday open make?

Gap Opens

Unfortunately, for the NIFTY, we can only use data from 2011 to analyze opening gaps (Why?) Be that as it may, are Monday opening gaps different from other trading-day gaps?

NIFTY gaps

NIFTY gaps table

MONDAY: gap on a regular Monday after a two-day weekend.
HOLIDAY: gap on a weekday that is not a Monday, but one that opens after a holiday.
DAY_1: gap on a regular day that is not a MONDAY and not a HOLIDAY.

What this tells us is that there is nothing special about a Monday open. On, average, it is just like any other day.

Close-to-Close vs. Open-to-Close returns

The next question is how much of a difference would it make if we traded at the close and held our position over the weekend vs. buying at the open on Monday?

Comparing Close-to-Close (c2c) vs. Open-to-Close (o2c) is tricky because the holding period of the latter is considerably shorter than the former. Nevertheless, here are cumulative returns of buying on Friday close and selling on Monday close vs. buying on Monday open and selling on Monday close:
Buying NIFTY on Friday close and selling on Monday close vs. buying on Monday open and selling on Monday close

What about holding over a holiday?
holding NIFTY over a holiday

And, lastly, holding overnight vs. over a single trading day:
holding NIFTY overnight vs. over a single trading day

The above charts indicate that there is a big difference in holding positions overnight vs. buying at the open.

Volatility at Open vs. Close

The opening and closing prices are computed prices. Actual traded prices could vary based on market conditions. The last question that needs to be answered before choosing between trading at the open vs. the close is how different is the market at the open vs. the close?

To answer this question, lets take the on-the-run NIFTY futures and plot the summary metrics of its returns over the first half-hour and the last half-hour of trading:
NIFTY first 30 minutes
NIFTY last 30 minutes

There seems to be no glaring difference. The drift between traded prices and the published open/close should be about the same whether you trade at the open or at the close.

Take-away

All things considered, there is a net benefit in not carrying over positions over the weekend. So, in theory, a global macro model using weekly time-series could be run over the weekend – positions opened on Monday at the open and closed on Friday at the close – and the “rolling close” problem can be ignored when trading the NIFTY.

Related: Trading turnover throughout the day
Code is on github.

USDINR and Dollar Indices, Part IV

Please read Part I for the introduction, Part II for a study of the spread between USDINR and the dollar indices and Part III for a spread-trading back-test on daily returns.

Weekly vs. Daily

In our previous posts, we used daily returns to setup the analysis. However, analyzing daily series on currencies and commodities is problematic. They trade 24/7 in a global marketplace and “closing” prices for commodities and currencies are hard to pin down at a granular level across markets. One way to ameliorate this issue is to use a weekly or a monthly series instead.

Here are the plots of the spreads and p-values from the adf-tests applied to weekly returns:
USDINR.DTWEXB weekly spread
USDINR.DTWEXM weekly spread
USDINR.DTWEXO weekly spread

The back-test results mirror that of the daily series, with bets on momentum carrying through on the USDINR and DTWEXM pairs:
USDINR weekly spread trading backtest

This gives us more confidence in our back-tests. We end our series with the following caveats:

  1. Trading the spread involves trading both legs (as discussed in Part III.)
  2. One can only buy a currency by selling another. i.e., buying USDINR implies going long USD and short INR.
  3. Using the above analysis, if a trade involves buying USDINR in one of the legs, it does not inform anything on relative valuation of USD or INR.

Code and charts on github.

USDINR and Dollar Indices, Part III

Please read Part I for the introduction and Part II for a study of the spread between USDINR and the dollar indices.

Trading the spread

In Part II, we defined spread = A – βB. When we say “trade the spread” we literally mean going long or short the spread as defined. To actually implement the trade, one would have to create two legs: one that is long USDINR (A) and the other that is short β times one of the dollar indices (B). Since the dollar indices are not something that can be actually traded, the following back-tests are purely a theoretical exercise.

Back-test

We consider three scenarios:

  1. C1: if the spread diverges beyond 1-sigma, bet on mean-reversion.
  2. C2: if the spread diverges beyond 1-sigma, bet on it getting bigger.
  3. D1: if the spread is between the average and 1-sigma, bet on it blowing out.

The first one is pure convergence and the last one is pure divergence. The second one is sort of like momentum – if the spread is already beyond 1-sigma, bet on it further blowing out.

USDINR spread-trading backtest

It appears the second scenario, the one that bets on momentum carrying through, is the most profitable. Also, the most profitable pair seems to be USDINR and DTWEXM (Trade Weighted U.S. Dollar Index: Major Currencies).

In the last part in this series, we will run through this analysis for a weekly time-series of these indices.

Code and charts are on github.

USDINR and Dollar Indices, Part II

Please read Part I for the introduction.

In Part I, we saw that if we force the intercept to be zero during linear regression between two series A and B, we end up with A = βB. In this post, we go one step further and define spread = A – βB

Pair trading

Readers of our posts on pair trading will immediately recognize the above relationships. The idea here is that if we assume USDINR to be dependent on DTWEXB, DTWEXM and DTWEXO indices, then we:

  1. calculate the spread between USDINR and each of the indices in turn,
  2. check if the spread is ‘stable’ by conducting an adf test on the residuals of the linear fit and checking if the p-value is less than a threshold,
  3. if the p-values confirm stability, then we can go long/short the spread whenever it deviates from the mean.

Spreads

When we plot the spreads and p-values, we see that a 50-day period is probably the most suitable time-frame over which to calculate the spread. And, we also observe considerable mean-reversion suggesting that a trading model can be built over it.
spread between USDINR.DTWEXO
spread between USDINR.DTWEXM
spread between USDINR.DTWEXB

In Part III, we will back-test a couple of trading models based on these spreads.

Code and charts are on github.