I have securities that are removed from my universe that I'm trying to exit, but I'm unable to do so. I'm not sure if this is because of an issue with the data, the underlying security (delisted, bad volume, etc.), or my code.

When I want to exit a position, I cancel all existing open orders, then place the security in a list (self.close). This happens at midnight right after UniverseSelection in the OnSecuritiesChanged method. Another method runs through that that list at 10:00AM on market open days and places trades as required (sell or cover).

Here is the relevant code:

# is called whenever the universe changes def OnSecuritiesChanged(self, changes): #self.Debug("{} Securities Changed".format(self.Time)) self.open = [] self.close = [] for security in changes.RemovedSecurities: self.CancelAllOrders(security.Symbol) if security.Invested: self.Debug("{} Removed Security {}".format(self.Time, security.Symbol)) self.close.append(security.Symbol) else: self.Debug("{} Removing security not invested in {}".format(self.Time, security.Symbol)) for security in changes.AddedSecurities: self.CancelAllOrders(security.Symbol) if not security.Invested: self.Debug("{} Added Security {}".format(self.Time, security.Symbol)) self.open.append(security.Symbol) else: self.Error("{} Adding security already invested in {}".format(self.Time, security.Symbol)) def UniverseSelection(self, coarse): #self.Debug("{} Universe Selection".format(self.Time)) # only use securites from US market with positive pricing filtered = list(filter(lambda c: (c.Market == "usa") and (c.Price > 0) and (c.Volume > MIN_VOLUME), coarse)) # and (c.HasFundamentalData) for f in filtered: if f.Symbol not in self.universe: history = self.History(f.Symbol, 25, Resolution.Daily) self.universe[f.Symbol] = SecurityData(f.Symbol, history) sd = self.universe[f.Symbol] sd.update(f.EndTime, f.AdjustedPrice) remaining = list(filter(lambda sd: sd.a, self.universe.values())) remaining.sort(key = lambda sd: sd.b, reverse = True) selected = [ sd.symbol for sd in remaining[:MAX_POSITIONS] ] if len(selected) > MAX_POSITIONS: self.Error("{} Selected more securities than required {} > {}".format(self.Time, len(selected), MAX_POSITIONS)) return selected def CancelAllOrders(self, symbol): self.Debug("{} Cancelling all orders for {}".format(self.Time, symbol)) openOrders = self.Transactions.CancelOpenOrders(symbol) for oo in openOrders: if not (oo.Status == OrderStatus.CancelPending): r = oo.Cancel() if not r.IsSuccess: self.Error("{} Failed to cancel open order {} of {} for reason: {}, {}".format(self.Time, oo.Quantity, oo.Symbol, r.ErrorMessage, r.ErrorCode))

The securities in question are MGT and RLOC. Here are some logs:

2018-10-10 00:00:00 2018-10-10 00:00:00 Cancelling all orders for MGT TEMHQPA3FAZP 2018-10-10 00:00:00 2018-10-10 00:00:00 Removed Security MGT TEMHQPA3FAZP 2018-10-10 00:00:00 2018-10-10 00:00:00 Cancelling all orders for RLOC UMQPTIBCU8V9 2018-10-10 00:00:00 2018-10-10 00:00:00 Removed Security RLOC UMQPTIBCU8V9 2018-10-11 00:00:00 2018-10-11 00:00:00 Cancelling all orders for MGT TEMHQPA3FAZP 2018-10-11 00:00:00 2018-10-11 00:00:00 Removed Security MGT TEMHQPA3FAZP 2018-10-11 00:00:00 2018-10-11 00:00:00 Cancelling all orders for RLOC UMQPTIBCU8V9 2018-10-11 00:00:00 2018-10-11 00:00:00 Removed Security RLOC UMQPTIBCU8V9 2018-10-11 00:00:00 2018-10-11 00:00:00 Cancelling all orders for NXTDW VTTIWRG7CIN9 2018-10-11 00:00:00 2018-10-11 00:00:00 Added Security NXTDW VTTIWRG7CIN9 2018-10-19 00:00:00 2018-10-19 00:00:00 Cancelling all orders for MGT TEMHQPA3FAZP 2018-10-19 00:00:00 2018-10-19 00:00:00 Removed Security MGT TEMHQPA3FAZP 2018-10-19 00:00:00 2018-10-19 00:00:00 Cancelling all orders for RLOC UMQPTIBCU8V9 2018-10-19 00:00:00 2018-10-19 00:00:00 Removed Security RLOC UMQPTIBCU8V9 2018-10-19 00:00:00 2018-10-19 00:00:00 Cancelling all orders for WHLRW VQ5L08FL3F51 2018-10-19 00:00:00 2018-10-19 00:00:00 Added Security WHLRW VQ5L08FL3F51 2018-10-25 00:00:00 2018-10-25 00:00:00 Cancelling all orders for ENRJ VRGUGPZORMUD 2018-10-25 00:00:00 2018-10-25 00:00:00 Removed Security ENRJ VRGUGPZORMUD 2018-10-25 00:00:00 2018-10-25 00:00:00 Cancelling all orders for MGT TEMHQPA3FAZP 2018-10-25 00:00:00 2018-10-25 00:00:00 Removed Security MGT TEMHQPA3FAZP 2018-10-25 00:00:00 2018-10-25 00:00:00 Cancelling all orders for RLOC UMQPTIBCU8V9

As you can see, these securities are never removed from my portfolio. It is also specific to these securities in a backtest run from 2015-2020. For other securities, my code exits positions properly.

I believe this is a security data problem because the documentation on Universes for SecurityChanges says:

# Gets the symbols that were removed by universe selection. This list may # include symbols that were removed, but are still receiving data due to # existing holdings or open orders (IReadOnlyList) self.RemovedSecurities;

This leads me to believe that I'm unable to exit because of volume. I'm wondering how I should handle cases like this.

Also unrelated:

Does the self.UniverseSettings.MinimumTimeInUniverse property utilize the self.UniverseSettings.Resolution property or is it fixed to days? For example:

self.UniverseSettings.Resolution = Resolution.Minute self.UniverseSettings.MinimumTimeInUniverse = 1000

Will this require securities to be in my universe for 1000 minutes or 1000 days?

Author