Hello! I was having some difficulties getting bracketed orders to work for me when entering using a market order and trying to place my limit out and stop markets from the on order event method so I wanted to share what I came up with to help anyone else save some time. Hope You enjoy and if your more experienced and see any flaws in my code, please let me know! 

 

class CrawlingFluorescentYellowCaribou(QCAlgorithm):
   
   stoplossTicket = None
   takeprofitTicket = None
   
   
   def Initialize(self):
       self.SetStartDate(2020, 1, 1)  
       self.SetEndDate(2020,3,1)
       self.SetCash(100000) 
       self.crudeoilwti = self.AddFuture(Futures.Energies.CrudeOilWTI, Resolution.Minute)
       self.rsi = self.RSI(self.crudeoilwti.Symbol, 3, Resolution.Minute)
       self.atr = self.ATR(self.crudeoilwti.Symbol, 20, Resolution.Minute)
       
       
       self.ema = self.EMA(self.crudeoilwti.Symbol, 20)
       self.RegisterIndicator(self.crudeoilwti.Symbol, self.ema, timedelta(days=1))
       
       self.adx = self.ADX(self.crudeoilwti.Symbol, 20)
       self.RegisterIndicator(self.crudeoilwti.Symbol, self.adx, timedelta(hours=1))
       
       self.Schedule.On(self.DateRules.EveryDay(self.crudeoilwti.Symbol), self.TimeRules.At(14, 00), self.ClosePositions)
       
       
       
       
       
       
   def OnData(self, data: Slice):
       if not self.ema.IsReady:
           return
       price = self.Securities[self.crudeoilwti.Symbol].Price
       strong_uptrend = price > self.ema.Current.Value and self.adx.Current.Value >= 50
       strong_downtrend = price < self.ema.Current.Value and self.adx.Current.Value >= 50
       atr = self.atr.Current.Value
       if self.Time.hour >= 9 and self.Time.hour <= 14:
          
       
           if not self.Portfolio.Invested:
               if strong_uptrend is True:
                   if self.rsi.Current.Value < 30:
                       self.MarketOrder(self.crudeoilwti.Symbol, 3)
                       self.stoplossTicket = self.StopMarketOrder(self.crudeoilwti.Symbol, -3, price - atr*.5 )
                       self.takeprofitTicket = self.LimitOrder(self.crudeoilwti.Symbol, -3, price + atr*1.5)
               if strong_downtrend is True:        
                   if self.rsi.Current.Value > 70:
                       self.MarketOrder(self.crudeoilwti.Symbol, -3)
                       self.stoplossTicket = self.StopMarketOrder(self.crudeoilwti.Symbol, 3, price + atr*.5)
                       self.takeprofitTicket = self.LimitOrder(self.crudeoilwti.Symbol, 3, price - atr*1.5)    
                   
   def OnOrderEvent(self, orderEvent):      
       if orderEvent.Status != OrderStatus.Filled:
           return
       
       if self.stoplossTicket is not None and self.stoplossTicket.OrderId == orderEvent.OrderId:
           self.Transactions.CancelOpenOrders()
           self.stoplossTicket = None
       if self.takeprofitTicket is not None and self.takeprofitTicket.OrderId == orderEvent.OrderId:
           self.Transactions.CancelOpenOrders()
           self.takeprofitTicket = None
   def ClosePositions(self):
      self.Liquidate(self.crudeoilwti.Symbol)
      self.Transactions.CancelOpenOrders()

 

 

Author