Hello,
I get the error below during backtests. In my case it raises close to the end of the backtest. I tried searching the documents for the meaning of this, google searches wrt to quant connect, debugging to see why this occurs etc to no avail.
Would anyone be able to tell me the nature of this keyerror, or perhaps reasons for it ?
Any info would be helpful, thank you in advance.
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 key exist in the collection and/or that collection is not empty.
at OnSecuritiesChanged
self.activeStocks.remove(x.Symbol)
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 106 (Open Stack Trace)
Nico Xenox
Would you mind sharing the code or at least the ‘OnSecuritiesChanged’ part. It is quite difficult to help without knowing the code.
I'm guessing that you have a key in a loop that does not exist anymore. Either you delete it from the list or whatever you are looping trough or you make an if statement and check if the symbol is there.
Xeno core
Hi Nico Xenon,
Thank you for the response.
Background:
My focus was to integrate a SEL universe from Louis's video 8 to Peter Guenther's IN/OUT algorithm. The AddedSecurities code in the ONSecuritiesChanged method kept adding the market signals in the initialize method (lines 40-51) into the portfolio. So, the simplest way i found to deal with that is to add lines 112-138 which essentially remove these securities.
The algo is below:
When I run a backtest from 2019, 1, 1 to today, I get the error on September 16, 2022. If I modify some of the parameters, the error raises at earlier dates.
Yes, I see that either a key does not exist or the list is empty. I tried debug to see which key might be missing but I could not see it because the error would raise.
thank you
Nico Xenox
I used the timeframe that you mentioned but I didnt get an error. I added the if statement to make sure that it doesnt happen but if it does pls dont hesitate to write back.
Btw if you're trying to learn from different algos you can look here. It is an extremly big list of different strategies or simple algo structures.
Happy coding ;)
Xeno core
your solution works. i appreciate your github recommendation. this will be very useful. thank you very much.
Xeno core
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!