Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Oil Shares Pair Trade based on Kalman Filter & Mahalanobis Distance

Hi, this is my first entry on Quantopian. I hope to seek suggestions on my strategy and results.

My strategy attempts to take advantage of market psychology due to oil price movements in the recent years. Greed and fear has led to close scrutiny of oil stocks, which has also likely lead to stronger co-integration within them.

Kalman filter is applied to compute beta and beta hedging ratio between two Oil & Gas equipment & services companies (Haliburton & Schlumberger). Co-integration test verified the pair and it was one of best pair I could find. I also coded the Kalman filter for the sake of learning and it was running appreciably faster than the pykalman module in my notebook.

The transition and measurement noise is estimated based on the sum of innovation chi square test and verified with innovation sequence chart and innovation residue auto-correlation test.

Mahalanobis distance, a result that can be obtained from the mathematics of Kalman filters is used to compute leverage. It is an attempt to selectively apply leverage. The equation “y = A/x + B” is used to bind the leverage ratio within 1 to 2 to avoid excessive leverage and meet Q fund requirement. It performed better than sigmoid shaped functions.

For similar mean leverage ratio strategy, I noted that leveraging shorter distance trade generates higher return & Sharpe as it appears more frequently. However it is riskier and could lead to larger max drawdown. Longer distance trade seems safer but does not occur as frequently to beat the market.

In this backtest, the unleveraged formula is able to track the market return with 0.02 beta and a max drawdown of 9.4%. For a mean leverage of ~1.85 (similar to the backtest result below), the algorithm can be pushed to obtain 200% return at the cost of 18.5% max drawdown with the commented leverage formula. The strategy does not work well before 2011, except for 2008.

I also have a small margin of safety (1.05*std. dev) to avoid excessive trading, which erodes profit due to commission fee.

Questions:

  1. How do I further improve the quality of entering and exit a trade? I want to learn how squeeze more alpha/sharpe from a pair. Adaptive Kalman filter / OU process works?
  2. How does code performance/speed affect trade execution especially minute frequency trade?
  3. Q fund worthy? :P
3 responses

Maybe trying to see if a momentum or MA indicator would help to enter and exit trades with better timing ?
I am willing to cooperate if you want...

Hi Gabriel,

I am happy to collaborate with anyone. Momentum is a known "anomaly" according to EMH. It will be interesting if we can incorporate it in the model.

However, how are we going to measure momentum effectively? long & short MA is sufficient?
I am worried of over-fitting MA period if we used that approach.

Hey guys,
I thinking of migrating the code to the new Syntax.
Any punters on it?