I am having an issue finding added securities in self.ActiveSecurities after they have been added to my list of securities through self.AddEquity. I am not adding securities “normally” through universe selection as I want to create my own “private” universe and remove and add securities when I please. Regardless of start date, the algorithm always finds every security in self.ActiveSecurities the first day, but some are missing the next days.
The backtest debug prints the following:
Universe selection time: 2022-04-11 00:00:00
Check stocks time: 2022-04-11 09:25:00 0 of 6674 not in active securities
Resetting universe: 2022-04-11 21:00:00
Universe selection time: 2022-04-12 00:00:00
Check stocks time: 2022-04-12 09:25:00 41 of 6658 not in active securities
Resetting universe: 2022-04-12 21:00:00
Universe selection time: 2022-04-13 00:00:00
Check stocks time: 2022-04-13 09:25:00 41 of 6674 not in active securities
Resetting universe: 2022-04-13 21:00:00
I get that self.AddEquity is probably asynchronous, but I am adding them at midnight and not checking self.ActiveSecurities until 09:25, so they should have time to be added? May not be a good solution anyway, but I need to check the newest price for all my securities in self.selectedSymbols at 09:25..is there a way to create a security by symbol and fetch the current price? Or maybe there is another better way?
Fred Painchaud
Hi Haakon,
Your way of doing Universe is rather unorthodox but well, I might post the more orthodox way to do custom universe later on.
For now, named parameters are not supported since Python in LEAN is only a Python.NET front-end to .NET code. So you need to change your calls to AddEquity, like so:
Fred
Alexandre Catarino
Hi,
QuantConnect's version of pythonet (like the master version) supports names arguments, so
is supported.
I would recommend avoiding mixing Universe Selection with AddEquity:
and follow the documentation on the topic
Haakon
Hello Fred and Alexandre, thanks for the replies 😊
Is your code working though Fred? I still get a key error in self.ActiveSecurities even when adding securities the way you do it.
Fred - Maybe the solution is your way of creating a custom universe? I would love to see it. My idea is to select some stocks at midnight through coarse and possibly fine selection and at a given time before market open check the most current price in order to select the final watchlist. In my example, it would be to remove stocks from self.selectedSymbols and self.ActiveSecurities (through self.RemoveSecurities) inside the “CheckStocks” function. But for that to work, all stocks from universe selection must be in self.ActiveSecurities..
Alexandre - Using simple universe selection is not enough as I need to check the price at a given time right before market open (09:25 in this case), and possibly also other attributes than price. Maybe there is a better way of doing it than what I am trying to do?
Vladimir
Haakon Flaarønning
If we follow the recommendation of Alexander Catarino
the results will be
2022-04-11 09:25:00 : Check stocks time: 2022-04-11 09:25:00
2022-04-11 09:25:00 : 0 of 6674 not in active securities
2022-04-12 00:00:00 : Resetting universe: 2022-04-11 21:00:00
2022-04-12 00:00:00 : Universe selection time: 2022-04-12 00:00:00
2022-04-12 09:25:00 : Check stocks time: 2022-04-12 09:25:00
2022-04-12 09:25:00 : 0 of 6658 not in active securities
2022-04-13 00:00:00 : Resetting universe: 2022-04-12 21:00:00
2022-04-13 00:00:00 : Universe selection time: 2022-04-13 00:00:00
2022-04-13 09:25:00 : Check stocks time: 2022-04-13 09:25:00
2022-04-13 09:25:00 : 0 of 6674 not in active securities
Haakon
Vladimir: Thanks for the reply. Yes, I know you can use a standard universe like that..the problem is though, as far as I know, you can't change that universe other than through the CoarseUniverseSeleciton function that runs at midnight. The purpose of my code is that I want to change the universe after checking the price in the try-catch statement in the CheckPrice function - when the time is 09:25 each day.
Fred Painchaud
Hi All,
Thanks for your replies. The OP asked for “[…] as I want to create my own “private” universe and remove and add securities when I please”.
Haakon, is that still something you want to do? You can do this through custom universes' “selector”… It seems you still want to do that but I want to make sure before I commit any more time to this.
Thanks,
Fred
Haakon
Hi Fred,
Yes, I still want to create my own “private” universe. I have expanded on the code in the attached backtest. In the example, I exclude all stocks with a price less than 1 at 09:25 AM each trading day. It is important that this selection happens at 09:25 and not at midnight, as filtering on price is just an example.
Please elaborate on custom universe selector :) from what I have read in the docs (https://www.quantconnect.com/docs/algorithm-reference/universes#Universes-Custom-Universe-Selection), all I can see is that it reads securities from a custom file..? I want to use coarse as it gives me all tradable securities on a given day. However, I need to control the universe selection at another time than midnight each day.
Louis Szeto
Hi Haakon
It is possible to create a dictionary with each iterated Symbol in Coarse Universe Selection as keys. Then update their values as the prices at 9:25.
Please note that you'll have to do a "pre-selection" in CoarseUniverseSelection first as if we include all symbols, there could be memory issues.
Best
Loui
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.
Fred Painchaud
Hi Haakon,
Short note: I DMed you.
F
Haakon
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!