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