Hello QuantConnect community,
I'm currently building an algorithm that relies on the ScheduledUniverseSelectionModel so I can pick stocks for my universe at a certain time. However, the problem is that it retains all of the stocks in the universe even after the day ends. So the next day, when I run self.Securities, it includes all of the securities from the previous day. I tried doing RemoveSecurity, but that didn't seem to work. Is there a way I can remove my securities (kind of like the fundamental / coarse universe selection) or have it reset each day like how it does in the liquid universe selection methods?
Thank you!
class Algo(QCAlgorithm):
def Initialize(self):
self.SetCash(10000)
self.SetStartDate(2020, 9, 1)
self.SetEndDate(2020, 10, 1)
self.AddEquity("SPY")
self.AddUniverseSelection(ScheduledUniverseSelectionModel(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9, 25), self.AddStocks))
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 5), self.RemoveStocks)
def AddStocks(self, dateTime):
data = pandas.read_csv(io.StringIO(self.Download("https://dropbox.com/my_stocks.csv")
for symbol in data[data.columns[data.columns.get_loc(dateTime.strftime("%F"))]].to_list():
symbols.append(Symbol.Create(symbol, SecurityType.Equity, Market.USA))
return symbols
def RemoveStocks(self):
map(lambda x: self.RemoveSecurity(x.Key) if x.Key != "SPY" else None, self.Securities)
Karthik Kailash
self.Securities contains information about all the securities your algorithm has ever interacted with. self.ActiveSecurities should show you what's in your current universe. And if you want to drop stocks out of your universe, just stop returning them from your universe selection function - that function is run every day so I believe you should be able to accomplish what you are saying.
Burton Algorithms
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!