class BootCampTask(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(2018, 12, 1)
self.SetEndDate(2018, 12, 10)
self.SetCash(100000)
spy = self.AddEquity("SPY", Resolution.Daily)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
def OnData(self, data):
if (self.Time - self.stopMarketOrderFillTime).days < 15:
return
if not self.Portfolio.Invested:
self.MarketOrder("SPY", 500)
self.stopMarketTicket = self.StopMarketOrder("SPY", -500, 0.9 * self.Securities["SPY"].Close)
self.Debug("original stop price: {}".format(self.stopMarketTicket.Get(OrderField.StopPrice)))
else:
#1. Check if the SPY price is higher that highestSPYPrice.
if self.Securities["SPY"].Close > self.highestSPYPrice:
self.Debug("stop price before update: {}".format(self.stopMarketTicket.Get(OrderField.StopPrice)))
#2. Save the new high to highestSPYPrice; then update the stop price to 90% of highestSPYPrice
self.highestSPYPrice = self.Securities["SPY"].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.Securities["SPY"].Close * 0.9
self.stopMarketTicket.Update(updateFields)
self.Debug("new stop price should be: {}".format(self.Securities["SPY"].Close * 0.9))
# alternative way to update?
self.stopMarketTicket.UpdateStopPrice(self.Securities["SPY"].Close * 0.9)
#3. Print the new stop price with Debug()
#self.Debug("stop price after update: {}".format(self.stopMarketTicket.Get(OrderField.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