I find out that stop market order is not triggered even when the price is way below stop price in backtesting mode.

For example, I would like to buy 100 AAPL stocks and I used self.StopMarketOrder('AAPL', 1, 304.53). And I changed the order fill time as the trailing stop bootcamp taught us: stopMarketOrderFillTime = datetime.min. I also change the universe resolution to minute even though I don't know if it helps like this: self.UniverseSettings.Resolution = Resolution.Minute.  I'm pretty sure the price was hit during the period of backtest. However, I still held the position even when the backtest ended. And I faced this problem many times.

I'd like to ask whether this kind of order only work in live mode. Or did I do it wrong?

The following is a simple example of what I did:

class Example(QCAlgorithm): def Initialize(self): self.UniverseSettings.Resolution = Resolution.Minute stopMarketOrderFillTime = datetime.min self.SetStartDate(2020, 5, 12) # Set Start Date self.SetCash(100000) # Set Strategy Cash # self.AddEquity("SPY", Resolution.Minute) self.AddEquity("SPY", Resolution.Minute) self.AddEquity("AAPL", Resolution.Minute) self.Schedule.On(self.DateRules.EveryDay("SPY"),self.TimeRules.AfterMarketOpen("SPY", 1), Action(self.Rebalance)) self.bought = False def Rebalance(self): if not self.bought: self.StopMarketOrder("AAPL",100,304) self.bought = True

 

Author