Hi,
I’ve been trying to improve the Pairs Trading with Stocks strategy y Jin Wu published in 2018 (https://www.quantconnect.com/learning/articles/investment-strategy-library/pairs-trading-with-stocks).
However I’ve run into an unexpected issue that prevents me from having more than 32 stocks in the trading universe. If I add XLF as the 33rd stock I get the following error message:
Runtime Error: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the XLF key exist in the collection and/or that collection is not empty.
at Rebalance
if self.history_price[str(i[0])] and self.history_price[str(i[1])]:
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyObject[] args)
at QuantConnect.Scheduling.ScheduleManager.<>c__DisplayClass15_0.<On>b__0(String name in main.py: line 117 (Open Stack Trace)
Since this error has nothing to do with the issue, I have no idea if it is a system or memory limitation (which I highly doubt) but much less on how to solve it.
Hope someone has come across something similar and might help me solve this issue.
Cheers,
Andres
Louis Szeto
Hi Andres
It is not about the number of securities, but the historical dataframe’s column name. We have updated it to be using SID rather than Symbol string to avoid problem on same-symbol-different-asset-class problem on the same dateframe. You could simply change all reference of self.history from str(symbol) to str(symbol.ID).
Best
Louis
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
Hi Louis,
I really appreciate your feedback however adding the ID per your suggestion did not fix the problem (see attached backtest)..
When I limit the algo to 32 symbols it runs fine (with or without the ID field).
When I add ANY 33rd symbol, I get the error above. With the ID is just changes to “XLF RGRPZX100F39” in the error message.
Any other ideas?
Cheers,
Andres
Andres Arizpe
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!