Back to Community
Update: Pairs Trading Notebook (now with cloning)

This notebook was originally shared in the community along with algorithms implementing pairs trading strategies. I am re-sharing it now that notebook cloning has been added to Quantopian.

For those of you with research, if you want to try it out, click the "Clone Notebook" button below.

You can view the previous post and the algorithms here.

Loading notebook preview...
Notebook previews are currently unavailable.

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

3 responses

Thanks for the updated notebook Delaney. This is very informative!

I'm wondering if you can add color on the moving average portion towards the end of the notebook. Specifically, why is the z-score of the moving average of the spread generating the signal as opposed to the z-score of the price spread?

Hello Victor, there is no 'right' answer here, this is just an easy and somewhat robust way to do it. I use a moving average of the spread because the spread is the signal we are interested in, but like any signal it is subject to spurious jumps. We don't want our strategy trading when the spread momentarily jumps for a weird reason, we want it trading when the spread is definitely higher than normal. A moving average will smooth out the signal a little bit and make us less twitchy. Now that we have a moving average as the proxy for the 'current' spread, we can compare the current spread to the distribution of spreads using a z-score.

To see why this might help, consider the following case: One stock suddenly falls by a huge amount due to a strange market movement, your algorithm places an order to trade on the next minute bar. By this point in time, the price has recovered as it was only a momentary dip. Your order fills at the new price, and your algorithm makes no money but pays trading fees. A computer located right next to the trading floor operating on nanoseconds might be able to take advantage of these fluctuations, but you can't really otherwise.

Another option is to use Kalman filters instead of moving averages, as described here.

Let me know if this makes sense, I'd be happy to discuss this further.

Thanks Delaney, this makes perfect sense now.