Introduction
Paired switching is a strategy where in its simplest form, investors pick two assets that are negatively correlated and periodically switch position based on their relative performance. The idea behind this strategy is that if the assets are negatively correlated, then a traditional mixed portfolio might lead to a lower return than the return for the individual assets. If the negative correlation exists, switching positions could improve the performance of a portfolio where two assets are statically weighted.
Method
After two negatively correlated assets are determined, we will analyze them on a quarterly basis. The analysis will consist of retrieving historical prices for the two assets and calculating their performances over the prior quarter. We will buy the asset that yields a higher return during the period. The position is held for one quarter, and then the analysis is repeated.
+ Expand
def rebalance(self):
self.months +=1
if(self.months%3==0):
history_call = self.history(self.securities.keys,timedelta(days=90))
if not history_call.empty:
first_bars = history_call.loc[self.first.symbol.value]
last_p1 = first_bars["close"].iloc[0]
second_bars = history_call.loc[self.second.symbol.value]
last_p2 = second_bars["close"].iloc[0]
first_performance = (float(self.securities[self.first.symbol].price) - float(last_p1))/(float(self.securities[self.first.symbol].price))
second_performance = (float(self.securities[self.second.symbol].price) - float(last_p2))/(float(self.securities[self.second.symbol].price))
if(first_performance > second_performance):
if(self.securities[self.second.symbol].invested==True):
self.liquidate(self.second.symbol)
self.set_holdings(self.first.symbol,1)
else:
if(self.securities[self.first.symbol].invested==True):
self.liquidate(self.first.symbol)
self.set_holdings(self.second.symbol,1)
Derek Melchin
See the attached backtest for an updated version of the algorithm in PEP8 style.
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.
Shile Wen
Hi Zach,
The algorithm has many technical and stylistic issues, and since it seemed to be a popular algorithm, we've rewrote it to use the SymbolData pattern, replaced the History calls with RollingWindows, and fixed many stylistic issues. Please view the updated algorithm in the attached backtest.
Best,
Shile Wen
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.
Zach Oakes
Thanks ! It's very cool. It's like an MR take on Trend -- brilliant interpretation, and MUCH nicer than my translation.
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.
Mohamed Ajmal
How can we implement stoploss / reduce drawdown in this algorithm?
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.
Varad Kabade
Hi Mohamed Ajmal,
In the last backtest attached by Shile he has implemented the stop-loss which is triggered every day after 10 minutes of market open:
Refer to the following code snippet.
Best,
Varad Kabade
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.
Andres Arizpe
This appears to be a very interesting algo.
Is there any documentation I might use to get a basic understanding of it?
Cheers,
Andres
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.
Varad Kabade
Hi Andres Arizpe,
The above algorithm consists of components like the scheduled events, rolling window, and standard python libraries like numpy and pandas. We recommend going through the following docs[1, 2], and regarding the libraries, please look for their homepage/documentation. Please feel free to ask any specific doubts about the above algorithm.
Best,
Varad Kabade
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!