Hey Robot Trader,
I had a similiar issue, the Exercise Hint is incorrect also. I'll attack my full code that allowed me to pass the exercise, but the problem is probably creating and storing the stop market order. In the example 'ibmstockprice' must be calculated by grabbing the SPY equity close then multipliying by 0.90 'self.Securities['SPY'].Close * 0.90'. When you debug the order ID you must include an additional 'self'. 'self.Debug(self.stopMarketTicket.OrderId) '. I am just learning as well so this explanation may not be totally correct, but this is what got the exercise to work for me.
Cheers, Redpoint
class BootCampTask(QCAlgorithm):
# Order ticket for our stop order, Datetime when stop order was last hit
stopMarketTicket = None
stopMarketFillTime = datetime.min
def Initialize(self):
self.SetStartDate(2018, 12, 1)
self.SetEndDate(2019, 4, 1)
self.SetCash(100000)
spy = self.AddEquity("SPY", Resolution.Daily)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
def OnData(self, data):
#4. Check that at least 15 days (~2 weeks) have passed since we last hit our stop order
if (self.Time - self.stopMarketFillTime).days < 15:
return
if not self.Portfolio.Invested:
self.MarketOrder("SPY", 500)
#1. Create a stop market order, save the order ticket to stopMarketTicket
self.stopMarketTicket = self.StopMarketOrder("SPY", -500, self.Securities['SPY'].Close * 0.90)
self.Debug(self.stopMarketTicket.OrderId)
def OnOrderEvent(self, orderEvent):
if orderEvent.Status != OrderStatus.Filled:
return
# Printing the security fill prices.
self.Debug(self.Securities["SPY"].Close)
#2. Check if we hit our stop loss (Compare the orderEvent.Id with the stopMarketTicket.OrderId)
# It's important to first check if the ticket isn't null (i.e. making sure it has been submitted)
if self.stopMarketTicket is not None and orderEvent.OrderId == self.stopMarketTicket.OrderId:
#3. Store datetime
self.stopMarketFillTime = self.Time