Read my Medimum article about this project here!
This is my implementation of a Pairs Trading Algorithm on the algorithmic trading research/competition platform Quantopian so I can dive deeper and learn more about Pairs Trading and implementing trading algorithms. Some tests/measures I'm currently learning about and using include:
- Kwiatkowski-Phillips-Schmidt-Shin (KPSS) stationarity test
- Augmented Dickey–Fuller (ADF) unit root test
- Hedge ratio
- Half life of mean-reversion from the Ornstein-Uhlenbeck process
- Hurst exponent
- Softmax function for calculating percent of each security on open order
Currently, my implementation will be able to run on arbitrary amount of possible pairs that a user could provide. However, for current testing purposes, I have only been testing on two possible pairs (and hardcoded some logic that assumes that such as how I record values on the graph). Those two pairs are Microsoft (MSFT) + Apple (AAPL), and McDonalds (MCD) + Yum! Brands (YUM) which owns chains like KFC, Pizza Hut, and Taco Bell. The former can be seen in the lower graph with the label "_tech" and the latter with the label "_food" with corresponding Z and hedge ratio values.
I had my algorithm run for 13 years from January 1, 2004 to January 1, 2017. However, I'm planning on seeing how extensible my algorithm is to different time periods and different combination of pairs. From the few tests I have done so far, this seems true and I haven't "overfitted" my algorithm by making it only work in a specific instance.
- Reduce the drawdown and beta and get the leveraging under control.
- Quantopian has deprecated version of Statsmodels python library which doesn't have the KPSS test available. I'll need to manually add it myself.
- Look into cleaning up how I'm currently returning a completely new pair object in
process_pair
and replacing the old pair in the for-loop inmy_handle_data
. - Haven't looked at using Kalman filters for determining hedge ratios. Not sure if I need to or if the way I did it sufficient.
- Need to look into how Quantopian's
order_target_percent
function works when I have several different pairs and not one or two (e.g. will the first opening order take up my entire portfolio?) .