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 validate_trail(order: Order, price: BarData) -> Union[None, float]: price = (price.open, price.high, price.low, price.close) # check if BUY order hit if order.action.upper() == 'BUY': if order.trailStopPrice <= max(price): return order.trailStopPrice else: order.trailStopPrice = min(order.trailStopPrice, min(price) + order.auxPrice) return False # check if SELL order hit if order.action.upper() == 'SELL': if order.trailStopPrice >= min(price): return order.trailStopPrice else: order.trailStopPrice = max(order.trailStopPrice, max(price) - order.auxPrice) return False