开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Re: One cancel all, trailing stop loss OR exit on close


 

Below is the code I use - Its a bracket order with a parent stop order, and 3 child orders - a profit taker, a stop loss, and a flatten order. the Flatten order does what you want - close the order at a certain time. The below is based on the ib_insync python library, but should give you an idea of how to mimic in your code. You basically place each orders one after the other ( the stoploss is the last order to be placed, as it has Transmit=True)

parent = StopOrder(
? ? ? ? action,
? ? ? ? quantity,
? ? ? ? stopPrice,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=False,
? ? ? ? outsideRth=True,
? ? ? ? tif="GTD",
? ? ? ? goodTillDate=util.formatIBDatetime(flattenTime if gtd is None else gtd),
? ? ? ? **kwargs,
? ? )

? ? takeProfit = LimitOrder(
? ? ? ? reverseAction,
? ? ? ? quantity,
? ? ? ? takeProfitPrice,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=False,
? ? ? ? parentId=parent.orderId,
? ? ? ? tif="GTC",
? ? ? ? outsideRth=True,
? ? ? ? **kwargs,
? ? )

? ? flatten = MarketOrder(
? ? ? ? reverseAction,
? ? ? ? quantity,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=False,
? ? ? ? parentId=parent.orderId,
? ? ? ? tif="GAT",
? ? ? ? goodAfterTime=util.formatIBDatetime(flattenTime), ?# "17:50:00"
? ? ? ? outsideRth=True,
? ? ? ? **kwargs,
? ? )
? ? stopLoss = StopOrder(
? ? ? ? reverseAction,
? ? ? ? quantity,
? ? ? ? stopLossPrice,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=True,
? ? ? ? parentId=parent.orderId,
? ? ? ? outsideRth=True,
? ? ? ? tif="GTC",
? ? ? ? **kwargs,
? ? )

Join [email protected] to automatically receive all group messages.