Hi,

I have a question while I'm following the bootcamp video.

It is on the "Buy & Hold with a Trailing Stop Tutorial - Managing Order Risk in QuantConnect" https://www.youtube.com/watch?v=oJstb5nMSZg&list=PLD7-B3LE6mz5jsEb127kdyJVMJrBNfbmI&index=4.

Below is the copy of the code

---------


from AlgorithmImports import *

class T03BuyAndHoldWithATrailStop(QCAlgorithm):

   stopMarketTicket=None
   stopMarketFilledTime=datetime.min

   def Initialize(self):
       # Locally Lean installs free sample data, to download more data please visit https://www.quantconnect.com/docs/v2/lean-cli/datasets/downloading-data
       self.SetStartDate(2018, 12, 1)  # Set Start Date
       self.SetEndDate(2019, 4, 1)  # Set End Date
       self.SetCash(100000)  # Set Strategy Cash
       self.spy=self.AddEquity("SPY", Resolution.Minute)
       self.spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
       self.lastOrderEvent = None
       
   def OnData(self, data: Slice):
       if not self.Portfolio.Invested:
           self.stopMarketTicket=self.StopMarketOrder("SPY", -500, 0.90*self.Securities["SPY"].Close)             
           self.MarketOrder("SPY", 500)
           
-----------
I don't understand how "StopMarketOrder" is running in this example. If "MarketOrder" is filled, then I'm expecting that self.Portfolio.Invested becomes true and then StopMarketOrder should not run.
Is there anyone who can teach me how "StopMarketOrder" is running ?