'Why are orders in my trading algo not working? (QuantConnect algo)

I'm currently learning and trying to build my first algo which is a variation of an opening range break on the SPY. Would anyone know why my orders aren't working?

Given that the logs are able to display the high and low of the first 30 min, I'm expecting the algo to check every time a bar closes whether it's higher than the high of first 30 min, or lower than the low of the first 30 min. If the above is true, then open an order, otherwise do nothing.

Appreciate any help! Cheers.

# First 30 minutes Highest High and Lowest Low

# -------------------------
STOCK = "SPY"; PERIOD = 30;
# -------------------------

class OpeningRangeBreakout(QCAlgorithm):
    
    # Order ticket for our stop order, Datetime when stop order was last hit
    stopMarketTicket = None
    stopMarketOrderFillTime = datetime.min
    highestSPYPrice = 0

    def Initialize(self):
        self.SetStartDate(2021, 9, 1)  
        self.SetEndDate(2021, 9, 10)  
        self.SetCash(100000) 
        self.stock = self.AddEquity(STOCK, Resolution.Minute).Symbol
        self.max = self.MAX(self.stock, PERIOD, Resolution.Minute, Field.High)
        self.min = self.MIN(self.stock, PERIOD, Resolution.Minute, Field.Low)
        self.SetWarmUp(PERIOD, Resolution.Minute)
        

    def OnData(self, data):
        # # 1. Plot the current SPY price to "Data Chart" on series "Asset Price"
        # self.Plot("Data Chart", "Asset Price", self.Securities["SPY"].Close)
        
        if self.IsWarmingUp: 
            return
        if not (self.max.IsReady or self.mi.IsReady): 
            return
        if self.Time.hour < 10 or self.Time.hour > 10 or self.Time.minute != 31: 
            return

        first_30_min_HH = self.max.Current.Value
        first_30_min_LL = self.min.Current.Value

        self.Log(f"First 30 min Highest High: {first_30_min_HH}")
        self.Log(f"First 30 min Lowest Low: {first_30_min_LL}")
        
        self.Plot("Data Chart", 'first_30_min_HH', first_30_min_HH)
        self.Plot("Data Chart", 'first_30_min_LL', first_30_min_LL)
        
        if not self.Portfolio.Invested:
            if data["SPY"].Close > first_30_min_HH:
                self.MarketOrder("SPY", 500)
                self.stopMarketTicket = self.StopMarketOrder("SPY", -500, 0.99 * self.Securities["SPY"].High)
                
            elif data["SPY"].Close < first_30_min_LL:
                self.MarketOrder("SPY", -500)
                self.stopMarketTicket = self.StopMarketOrder("SPY", +500, 1.01 * self.Securities["SPY"].Low)
        
        else:
            #2. Plot the moving stop price on "Data Chart" with "Stop Price" series name
            self.Plot("Data Chart", "Stop Price",  self.highestSPYPrice * 0.9)
            
            if self.Securities["SPY"].High > self.highestSPYPrice:
                #2. Save the new high to highestSPYPrice; then update the stop price to 90% of highestSPYPrice
                self.highestSPYPrice = self.Securities["SPY"].High
                updateFields = UpdateOrderFields()
                updateFields.StopPrice = self.highestSPYPrice * 0.99
                self.stopMarketTicket.Update(updateFields)
                
                #3. Print the new stop price with Debug()
                self.Debug("SPY: " + str(self.highestSPYPrice) + " Stop: " + str(updateFields.StopPrice)) 
            
    def OnOrderEvent(self, orderEvent):
        if orderEvent.Status != OrderStatus.Filled:
            return
        if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == orderEvent.OrderId:
            self.stopMarketOrderFillTime = self.Time
            ```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source