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