Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Mean Reverting of Two Stocks

Hi,

A little help on coding the following algo will be much appreciated. There are two stocks, A and B, whose long term ratio of daily returns is very stable, for example let's assume it is 2, i.e. stock A returns twice as stock B. The idea is that when on the intraday basis this relationship is too far away from its normal mean one puts a trade in anticipation that both stocks' returns will revert to a ratio of 2. As such the algo needs to keep track of the intraday returns of both stocks from the open. For example, at 10.00am stock A moved 1% and stock B moved 2%, making an effective ratio of 0.5, one would buy stock A and sell stock B in the right proportion. Conversely, if the ratio was to be 5, one would do the opposite.

To summarize, the entry signal is when the ratio drops below certain threshold, say 0.5 => buy A sell B
The exit signal is when the ratio goes back up to another level, say 2 => Sell A buy B

                     The opposite entry signal is when the ratio exceeds certain other threshold, say 5 => sell A buy B  
                      The exit signal is when the ratio comes back down, say to 3 => buy A and sell B

If one is left with the position overnight or over multiple days, then the subsequent tracking of the ratio will have to be based on the open of the day on which the trade was initiated. One other wrinkle is that it is probably better to use a running average of ratios, say 10 minutes, to account for unusual price prints.

Thank you

3 responses

Hi John,

What sort of help are you looking for, specifically? Are there elements algorithm that you do not know how to code? Or do you need help with the whole thing?

One thing that is not clear is how you arrive at the ratio of daily returns for the two stocks, A & B. Will this be a fixed parameter in the algorithm, or will it be derived from a trailing window of data, and updated on a walk-forward basis?

Grant

Hi Grant,

Thank you for your reply. I was looking for help for the entire thing as I am a bit at the loss on how to code the 10 minute historical average of ratios. As to your question on the various threshold levels of the strategy, they can be arrived in many different ways - I should be able to code that myself once I see how the code works with the fixed parameters. Thanks.

I gotta run, but I recommend having a look at this:

http://pandas.pydata.org/pandas-docs/stable/computation.html

The rolling_mean may be well-suited.

Unfortunately, if you want to work with minute bars, you'll need to build your own minutely data accumulator, which can be done pretty straightforwardly using the output from the history API--I may have time later today to provide an example (or maybe somebody else can provide guidance on how to generate a trailing window of minute bars). Note that the batch transform will be deprecated and is essentially no longer supported.

Grant