def TrailBracketOrder(self, parentOrderId, childOrderId, action, quantity, limitPrice, trailAmount): # This will be our main or "parent" order parent = Order() parent.orderId = parentOrderId parent.action = action parent.orderType = "LMT" parent.totalQuantity = 1000 #quantity parent.lmtPrice = limitPrice parent.transmit = False stopLoss = Order() stopLoss.orderId = childOrderId logging.info("Action is " + action) if action == "Buy": stopLoss.action = "Sell" stopLoss.trailStopPrice = limitPrice - (limitPrice * .02) if action == "Sell": stopLoss.action = "Buy" stopLoss.trailStopPrice = limitPrice + (limitPrice * .02) stopLoss.orderType = "TRAIL" stopLoss.auxPrice = limitPrice #trailAmount #trailAmount stopLoss.totalQuantity = 1000 #quantity stopLoss.parentId = parentOrderId stopLoss.transmit = True bracketOrder = [parent, stopLoss] return bracketOrder
def placeOrder(self, contract: Contract, order: Order) -> Trade: """ Place a new order or modify an existing order. Returns a Trade that is kept live updated with status changes, fills, etc. """ orderId = order.orderId or self.client.getReqId() self.client.placeOrder(orderId, contract, order) now = datetime.datetime.now(datetime.timezone.utc) if not isinstance(order, Order): order = Order(**order.__dict__) trade = self.wrapper.trades.get(orderId) if trade: # this is a modification of an existing order assert trade.orderStatus.status in OrderStatus.ActiveStates logEntry = TradeLogEntry(now, trade.orderStatus.status, 'Modify') trade.log.append(logEntry) else: # this is a new order order.orderId = orderId orderStatus = OrderStatus(status=OrderStatus.PendingSubmit) logEntry = TradeLogEntry(now, orderStatus.status, '') trade = Trade(contract, order, orderStatus, [], [logEntry]) self.wrapper.trades[orderId] = trade _logger.info(f'placeOrder: {trade}') return trade