Can someone help me?
I built a base code that filters many stocks whose turnover is above their origin, and buys them according to additional conditions in on data.

And there is some error in adding the stocks in the universe into the array self.activeStocks:

 

class WellDressedSkyBlueSardine(QCAlgorithm):


 

    def Initialize(self):

        self.SetStartDate(2022, 1, 1)

        self.SetEndDate(2023, 1, 1)

        self.SetCash(10000)

        self.rebalanceTime = datetime.min

        self.activeStocks = set()


 

        self.AddUniverse(self.CoarseFilterFunction)

        self.UniverseSettings.Resolution = Resolution.Daily


 

        self.portfolioTargets = []

       

        self.stateData = { }



 

    def CoarseFilterFunction(self, coarse: List[CoarseFundamental]) -> List[Symbol]:

       

        if self.Time<= self.rebalanceTime:

                return self.Universe.Unchanged

        self.rebalanceTime = self.Time + timedelta(hours=2.9)

        for c in coarse:

            if c.Symbol not in self.stateData:

                self.stateData[c.Symbol] = SelectionData(c.Symbol, 30)

            avg = self.stateData[c.Symbol]

            avg.update(c.EndTime, c.AdjustedPrice, c.DollarVolume)


 

        # filter the values of selectionData(sd) above SMA

        values = [sd for sd in self.stateData.values() if sd.volume > sd.sma.Current.Value and sd.volume_ratio > 0]

       

        # sort sd by the largest % jump in volume.

        values.sort(key=lambda sd: sd.volume_ratio, reverse= True)


 

 

        entering_symbols = [sd.symbol for sd in values[:10]]

        self.Debug("Entering symbols: " + ", ".join(str(symbol) for symbol in entering_symbols))


 

        return entering_symbols



 

       


 

    #def FineFilter(self, fine):

    #     # Sort symbols by price from lowest to highest

    #    sorted_symbols = sorted(fine, key=lambda f: f.Price, reverse=True)


 

        # Return the symbol objects

    #    return [f.Symbol for f in sorted_symbols]


 

    def OnSecuritiesChanged(self, changes):

 

        for security in changes.AddedSecurities:

            self.activeStocks.add(security.Symbol)

            self.Debug("added to self.activeStocks" +str(security.Symbol))

 

        self.Debug("in activeStocks"+ str(self.activeStocks))


 

        # Adjust targets if universe has changed

        self.portfolioTargets = [PortfolioTarget(symbol, 1/len(self.activeStocks))

                                 for symbol in self.activeStocks]

        self.Debug("in portfolioTargets"+ str(self.portfolioTargets))


 

The first and second debug line print a lot of shares
But the last two debug lines print empty arrays.
Why??? It does not make sense?