I Am trying to implement the following order logic:

  • Once a stop buy price above the current price is reached, buy 100 of a particular security
  • Once the stop buy order has been triggered
    • trigger a stop loss order
    • trigger a profit target order 1
    • trigger a profit target order 2

The following pseudocode represents what I have come up with, so far:

def Initialize(self):
self.symbol = 'SPY'
self.stopBuyPrice = None
self.stopLossPrice = None
self.profitTarget1 = None
self.profitTarget = None

def OnData(self, data):
self.stopBuyPrice = data[self.symbol].CLose + 3
self.stopLossPrice = self.stopBuyPrice - 10
self.profitTarget1 = self.stopBuyPrice + 10
self.profitTarget2 = self.stopBuyPrice + 20

#Buy 100 once stopBuyPrice is reached
self.ticket = # ?

def OnOrderEvent(self, orderEvent):
if self.ticket.OrderId == orderEvent.OrderId and orderEvent.OrderStatus.Filled:
#Enter stop loss order
#Enter limit order 1
#Enter limit order 2


Is this the correct way to do this and how can I realize a stop buy order, i.e. an order that should be triggered once the security price reaches a fixed value above the current price?