Need help with Custom Universes - Python

I'm trying to understand how custom universes work.  The following backtest should buy the four symbols specified, liquidate a week later and buy the four new symbols.  Instead it is throwing an ambiguous error message after making the first set of purchases but before reading the next universe.  The error message is:  "Backtest Handled Error:   not found in portfolio. Request this data when initializing the algorithm."  What am I doing wrong?

For extra credit: I'm working toward an MACD crossover system using Minute data consolidated to 30 minute bars applied to a dynamic list of symbols.  If there is an example of such it would be nice.  Thanks for any help.

Update Backtest


have you tried to adapt the basic universe examples from the respository?


universe example:

dropbox examples:

we will try something:

github has a search above to search the LEAN respository.

copy-paste the following and press enter:

"AddUniverse" path:"Algorithm.Python"

to search for universe examples

Michael Manus, thanks for the comments.  Yes, I've seen most of that before.  In fact I started with the dropbox example and had something that would run as shown below.  Interesting though, it ends up holding the 9 unique symbols included when there were only 4 symbols in the final universe.  I had expeced the symbols to drop off after each liquidation statement, but they hang around.

Troubles began when I tried limiting the purchases to the symbols in the current universe.  Obviously I'm missing something - probably something simple.


Liquidate does not remove symbols/equities.

RemoveSecurity does with liquidating it.

ah ok so maybe it will not get removed when you have a position in this stock


also include some self.Log output lines everywhere to check what works and what doesnt.


Yes, when you think about the logic in the dropbox example, it is liquidating all open positions and then opening positions for everything in the slice, which includes the positions just closed.  Therefore, nothing ever leaves the portfolio.  That's why I started changing the code to limit the holdings to the current universe.

I had a number of self.Log and self.Debug lines before cleaning it up to share.  I'll go back and try to narrow down the location of the error.


So I put some self.Debug statments in. I used these rather than self.Log because this error does no show up in the log file and even if it did the debug works a bit better at this I think.

The code with the debug statements is below and an excerpt of the console output is here:

252 | 15:40:05: Launching analysis for ee9de87bd2c6f7839e9c6e2d1a79fe8c with LEAN Engine v2.
253 | 15:40:05: End Init
254 | 15:40:05: Begin UniverseGetter
255 | 15:40:05: End UniverseGetter
256 | 15:40:05: Begin OnSecuritiesChanged
257 | 15:40:05: End OnSecuritiesChanged
258 | 15:40:05: Begin OnData   2018-04-06 10:00:00
259 | 15:40:05: end OnData   2018-04-06 10:00:00
260 | 15:40:05:  Error:   not found in portfolio. Request this data when initializing the algorithm.
261 | 15:40:05: Begin OnData   2018-04-06 11:00:00
262 | 15:40:05: Begin OnData   2018-04-06 12:00:00
263 | 15:40:05: Begin OnData   2018-04-06 13:00:00
264 | 15:40:05: Begin OnData   2018-04-06 14:00:00
265 | 15:40:05: Begin OnData   2018-04-06 15:00:00
266 | 15:40:05: Begin OnData   2018-04-06 16:00:00
267 | 15:40:05: Begin UniverseGetter
268 | 15:40:05: End UniverseGetter

You can see from the placement that the error is occuring after the first onData runs and before the next begins.  It is not in initialization and it is not in the code related to reading the universe.  Given that the error message is missing a field value between "Error:" and "not found in portfolio" it seems to be a deep error. 

Thanks for any help.


Update Backtest


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.


This discussion is closed