Introduction
The pairs trading algorithm aims to find two stocks which have prices that moved historically together. If price series diverges, long and short positions are opened in the opposite direction. With the assumption of mean reversion, the algorithm expects to make profits from the abnormal fluctuation of prices. The crucial part of pairs trading is determining which stocks are correlated and how to define a price divergence.
Method
Pairs Formation
The first step of this algorithm is to select stock pairs from a universe of stocks. We use the history request to get the history closing price for the last one year. This is called the formation period. The matching partner for each stock is found by looking for the security that minimizes the sum of squared deviations between two normalized price series. Assume there are two stocks A and B with the price series \(X\) and \(Y\). For price normalization, the starting price during formation period is set to $1. The formula of distance measure is \[\sum_{i=1}^n{\left(\frac{x_i}{x_1}-\frac{y_i}{y_1}\right)^2}\] Top 4 pairs with the smallest historical distance measure are then traded. The trading pairs are selected every half year. We use the Scheduled Event method to fire the rebalance function.
Trading Pairs
As prices in a pair of stocks were closely cointegrated in past, there is high probability that those two securities share common sources of fundamental return correlations. A temporary shock could move one stock out of the common price band which presents statistical arbitrage opportunity. Given the trading pairs, the trading period is the next six months. We calculate the price spread series of the last one year. When pair prices have diverged by two standard deviations, which means the spread is 2 times standard deviation away from its long-term mean, the algorithm will go short the stock which price is diverging up and go long the stock which price is diverging down. The position is closed when prices revert back.
Pavel Fedorov
so this is what i dont get … in this backtest you code: “self.symbols.append(self.AddEquity(i, Resolution.Daily).Symbol)” but is it not more accurate to use the Minute resolution prices? it will also give a different backtest result
Ashutosh
Hi Pavel Fedorov
I decided to dive into the minute resolution, and oh boy, the results were not exactly rolling in the money.
Navigating the minute-by-minute intricacies is like walking on a tightrope, but alas, the code's switch to minute resolution seems to have turned into a money-eating monster.
Time for a code intervention, or maybe just a good laugh to keep the tears at bay
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Pavel Fedorov
exactly,,, using hourly resolution means often you don't get accurate strategy backtest results,,, so I don't know why I see many strategies ideas or paper implementations using hourly resolutions... very often even QC puts a message in the log that the price is an estimate when using hourly resolution. hence I don't see any reason to use anything less than minute resolution
Derek Melchin
See the attached backtest for an updated version of the algorithm with the following changes:
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jing Wu
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!