Hi, 

I am very new to QuantConnect, so the question may seem stupid. After studying the Bootcamp, I am trying to write my first toy example, which is to select the top 1 weighted stock from ETF “SPY”. When I back test this example, no trade occurs, and I received the following error message:

Backtest Handled Error: AAPL R735QTJ8XC9X: The security does not have an accurate price as it has not yet received a bar of data.

Can anyone point to me where I got wrong? Any help is much appreciated!

Thanks

 

from AlgorithmImports import *
# Initialize asynchronous settings for speed and use the ETFConstituentsUniverseSelectionModel
# to select the top 1 SPY constituents by weight, focusing on blue-chip stocks with minimal risk.


class FrameworkETFConstituentsUniverseSelectionAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)  
        self.set_end_date(2024, 1, 15)
        self.set_cash(1000000)
        # self.spy = self.add_equity("SPY").symbol
        self.universe_settings.asynchronous = True  
        self.add_universe_selection(ETFConstituentsUniverseSelectionModel("SPY", universe_filter_func=self.etf_constituents_filter))
        self.universe_settings.resolution = Resolution.DAILY
        self.weight_by_symbol = {}


    def etf_constituents_filter(self, constituents: List[ETFConstituentUniverse]) -> List[Symbol]:
        # The top 10 weighted securities are considered better active selections
        # Save the weights for position sizing
        selected = sorted([c for c in constituents if c.weight],
            key=lambda c: c.weight, reverse=True)[:1]
        self.weight_by_symbol = {c.symbol: c.weight for c in selected}
       
        return list(self.weight_by_symbol.keys())



    def on_securities_changed(self, changes: SecurityChanges) -> None:
        # Liquidate the ones not in top 10 weights
        # for security in changes.removed_securities:
        #     history = self.history(security.symbol, 7, Resolution.DAILY)
        #     self.log(f"Removed: {security.symbol}")
           
               
        self.changes = changes


        for security in changes.removed_securities:
            if security.invested:
                self.liquidate(security.symbol)


        for security in changes.added_securities:
            # Historical data
            history = self.History(security.symbol, 7, Resolution.DAILY)
            self.log(f"Added: {security.symbol}")
            self.log(f'We got {len(history)} from our history request for {security.symbol}')
            self.set_holdings(security.symbol, 0.1)