In this example, we show how to place a combine three orders to emulate the bracket order bahavior.
Please also take a look at Levitikon's Support for one-cancels-the-other order (OCO) thread.

In this technique, we define a variable that will refer to the entry ticket. We check whether it is None to make sure only one "bracket" order is active:

# In Initialize: self.entry_ticket = None # In OnData if self.entry_ticket is None: # Contract selection logic # Place limit order below market price. Wait for rebound to buy self.entry_ticket = self.LimitOrder(front.Symbol, 3, price - d.Decimal(0.01))

In OnOrderEvent event handler, we wait for the entry order to be filled to place the take profit and stop loss order:

def OnOrderEvent(self, orderEvent): #self.Log(str(orderEvent)) if orderEvent.Status != OrderStatus.Filled: return if self.entry_ticket is not None: # When entry order is filled, place TP and SL orders if orderEvent.OrderId == self.entry_ticket.OrderId: price = orderEvent.FillPrice self.LimitOrder(orderEvent.Symbol, -3, price + d.Decimal(0.04)) self.StopMarketOrder(orderEvent.Symbol, -3, price - d.Decimal(0.03)) # Otherwise, one of the exit orders was filled, so cancel the open orders else: self.Transactions.CancelOpenOrders(orderEvent.Symbol) self.entry_ticket = None

When one of the exit orders is filled, the other other is cancelled and entry_ticket is set to None so that we can place a new entry order.

QC Community, please feel free to suggest other approaches!

Author