Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pairs Trading with Chevron and Exxon

In this algorithm I use a pairs trading strategy with Chevron and Exxon. Whenever they diverge I short sell the over-performer and go long on the under-performer. Once they converge I sell everything. I'm not getting great results at the moment. Is it that I didn't choose a good pair? Did I mess up my negative signs and accidentally short sell the under-performer? All constructive feedback is welcome.

8 responses

Even assuming your algorithm is absolutely perfect, I'd be surprised if you find any significant returns with this kind of strategy. Simply because if there were significant returns with such a simple strategy, people would capitalize on this correlation and it would disappear. I think that's what you are seeing here-- on these timescales (days/minutes) this pair is weakly correlated.

i think your ratio calculation is a bit strange, for example during the week of nov 1, 2010 your ratio is lower than your lowerbound. and the week of dec 13th 2010 it's higher than your highbound, yet the spread is aproximatly the same.

it seems like you should be doing your calculation based on the spread instead. for example, entering the position when the spread is in a high percentile and exiting when the spread is in a low percentile.

a rough test strat of that here.... but i might have my math wrong, so double-check.

EDIT: don't use this backtest, there's a bug in that i'm using the wrong side of the numpy array. use the example from my last post instead.

sorry my previous example is wrong because i was using the wrong side of the numpy array (i thought new items were added in front)

here's a fix of my example. i see that in the last year there seems to be a regime change thus performance degrades

Hey Jordan. You're calculating your ratios as A / B, where A and B are prices. The 'ratio' that you really want should be solved by using a simple linear regression, of the form Y = bX, where Y and X are your two price histories.

Don't regress; cointegrate.

maybe you could post an example? i'm a little weak on my math so seeing code for doing either linear regression or cointegration to execute this strategy would be appreciated.

I think he means that Sum(Y)/Sum(X) = b is more useful (and simpler) than solving for b in Y = bX with some sort of pseudoinverse best fit

I OTOH know the math, but am new to the site, and haven't even looked at the prog lang. Pretty sure you can find code samples on google. Pair (or group) trading is about finding a mean-reverting linear combination of securities. That is the essence of cointegration. Has been done since the 90s, so I wouldn't put money on blue chip stocks like those. Once you go down on cap, you'll have to be more attentive to liquidity obviously. Then the plot thickens ;)