Hi there, 

Can't understand why this code generates a fatal error

from AlgorithmImports import *

class LongOnlyEquityTradingAlgorithm(QCAlgorithm):
    def Initialize(self):
        # Set the starting and ending dates for the backtest
        self.SetStartDate(2024, 1, 1)
        self.SetEndDate(2024, 12, 31)
        
        # Set the initial cash for the backtest
        self.SetCash(100000)
        
        # Define the universe selection parameters
        self.num_stocks = 300
        self.price_threshold = 1.0
        
        # Initialize dictionaries to hold stock data
        self.Myuniverse = []
        self.selected_stocks = []
        self.stock_data = {}
        
        # Schedule the universe selection function to run at the market open
        self.AddUniverse(self.CoarseSelectionFilter)
        
        # Schedule the stock selection function to run at 9:35 AM
        try:
            self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 5), self.SelectStocks)
        except Exception as e:
            self.Debug(f"Error scheduling SelectStocks: {e}")
            
    def CoarseSelectionFilter(self, coarse):
        # Filter the universe to the most liquid small and mid-cap stocks above $1 in price
        filtered = [x for x in coarse if x.Price > self.price_threshold]
        sorted_by_liquid = sorted(filtered, key=lambda x: x.DollarVolume, reverse=True)
        self.Myuniverse = [x.Symbol for x in sorted_by_liquid[:self.num_stocks]]
        return self.Myuniverse

    def SelectStocks(self):
        # Filter the universe based on the previous day's closing price and Bollinger Bands
        self.selected_stocks = []
        for symbol in self.Myuniverse:
            history = self.History(symbol, 21, Resolution.Daily)
            if not history.empty:
                close_prices = history['close']
                if len(close_prices) == 21:
                    upper_band = close_prices.rolling(20).mean() + 2 * close_prices.rolling(20).std()
                    if close_prices[-2] > upper_band[-2]:
                        self.selected_stocks.append(symbol)
        
        # Further narrow down the universe based on the best 5-minute returns
        five_minute_returns = {}
        for symbol in self.selected_stocks:
            history = self.History(symbol, 5, Resolution.Minute)
            if not history.empty:
                open_price = history['open'][0]
                close_price = history['close'][-1]
                five_minute_returns[symbol] = (close_price - open_price) / open_price
        
        sorted_by_returns = sorted(five_minute_returns.items(), key=lambda x: x[1], reverse=True)
        self.selected_stocks = [x[0] for x in sorted_by_returns[:20]]
        
        # Initialize stock data for monitoring and add consolidators
        for symbol in self.selected_stocks:
            self.stock_data[symbol] = {
                'entry_price': None,
                'trailing_stop': None
            }
            # Create a consolidator for 5-minute bars
            consolidator = TradeBarConsolidator(timedelta(minutes=5))
            consolidator.DataConsolidated += self.OnDataConsolidated
            self.SubscriptionManager.AddConsolidator(symbol, consolidator)
    
    def OnDataConsolidated(self, sender, bar):
        symbol = bar.Symbol
        history = self.History(symbol, 21, Resolution.Minute)
        if not history.empty:
            close_prices = history['close']
            volumes = history['volume']
            
            upper_band = close_prices.rolling(20).mean() + 2 * close_prices.rolling(20).std()
            if close_prices[-1] > upper_band[-1] and volumes[-1] > 2 * volumes[0]:
                if not self.Portfolio[symbol].Invested:
                    self.SetHoldings(symbol, 1.0 / len(self.selected_stocks))
                    self.stock_data[symbol]['entry_price'] = close_prices[-1]
                    self.stock_data[symbol]['trailing_stop'] = close_prices[-1] * 0.98
    
    def OnData(self, data):
        for symbol in self.selected_stocks:
            if symbol in self.Portfolio:
                if symbol in data:
                    current_price = data[symbol].Close
                    entry_price = self.stock_data[symbol]['entry_price']
                    trailing_stop = self.stock_data[symbol]['trailing_stop']
                    
                    # Update trailing stop
                    if current_price > entry_price * 1.10:
                        self.Liquidate(symbol)
                    elif current_price > trailing_stop:
                        self.stock_data[symbol]['trailing_stop'] = current_price * 0.98
                    elif current_price > trailing_stop:
                        self.Liquidate(symbol)
                else:
                    self.Debug(f"Data for {symbol} is not available in the current slice.")