Repository navigation
[Feature Request] Support for Bracket Order #8253
Description
Activity
- changed the title
[-][Feature Request support for Bracket Order[/-][+][Feature Request] Support for Bracket Order[/+]on Aug 14, 2024 second to @fishstoryyy
IBRK TWS API supports bracket order natively
def start(self): contract = Contract() contract.symbol = "AAPL" contract.secType = "STK" contract.exchange = "SMART" contract.currency = "USD" parent_order = self.create_order("BUY", 100, 150, "LMT") take_profit = self.create_order("SELL", 100, 160, "LMT", parent_order.orderId) stop_loss = self.create_order("SELL", 100, 140, "STP", parent_order.orderId) self.placeOrder(parent_order.orderId, contract, parent_order) self.placeOrder(take_profit.orderId, contract, take_profit) self.placeOrder(stop_loss.orderId, contract, stop_loss) def create_order(self, action, quantity, price, order_type, parent_id=None): order = Order() order.action = action order.totalQuantity = quantity order.orderType = order_type order.lmtPrice = price if order_type == "LMT" else None order.auxPrice = price if order_type == "STP" else None order.transmit = False if parent_id else True order.parentId = parent_id if parent_id else 0 return orderSource:
https://interactivebrokers.github.io/tws-api/bracket_order.htmlMainly these couple lines
self.placeOrder(parent_order.orderId, contract, parent_order) self.placeOrder(take_profit.orderId, contract, take_profit) self.placeOrder(stop_loss.orderId, contract, stop_loss)I would like to contribute to the code base, can someone advice which file is a good starting point i can start looking into. Thanks.
@pandaxbacon I think this would be a entirely new order type (
Common/Orders/OrderTypes.cs) and the engine needs to be able to recognize and handle this order type correctly.dont think we need to add any new order type; cause bracket order is not a primitive order type
bracket or attached order is just a combination of limit ordersi will spend sometime to look into the code base
This would be an amazing addition - I'm just coming here to voice my support!
Would be really nice to have native support for brackets!
Would love to see support for Alpaca's native bracket orders added! Currently there isn't a good workaround, since Alpaca won't allow three individual orders to be open at the same time.
Reacted by teelingNative contingent orders are available since Lean 18130 (#9828):
bracket_order(symbol, quantity, take_profit_price, stop_loss_price, limit_price=None), plusone_cancels_other_order,one_triggers_other_orderandone_updates_other_orderfor other shapes. They work in backtesting and are placed natively by Interactive Brokers (QuantConnect/Lean.Brokerages.InteractiveBrokers#265), TradeStation (QuantConnect/Lean.Brokerages.TradeStation#109), Charles Schwab (QuantConnect/Lean.Brokerages.CharlesSchwab#116), Alpaca (QuantConnect/Lean.Brokerages.Alpaca#83) and Binance spot (QuantConnect/Lean.Brokerages.Binance#85). Each brokerage model lists the shapes it supports. Thanks for the request!Reacted by Justin Farrell and continuous-eli
Expected Behavior
Native support for bracket order (IBRK TWS Api bracket order example)
Currently, the workaround to emulate bracket orders is through
onOrderEvent(discussion) However, brokerages such as IBKR supports submitting all orders as a bracket, which is not currently supported in Lean (IBKR plug in, bracket orders involves assigning parent orders). Attaching an order with a stop loss and take profit is common and the value of supporting bracket orders natively in Lean helps reduce code needed and avoid delays in order status transmissions. Also in live trading, being able to place bracket orders alleviates the unreliability by a huge degree as it eliminates possible issues such as server goes down, network issues, market data issues, etc. Please consider offer this! thank you.related issue: #5219
Actual Behavior
No native support of bracket orders.
Potential Solution
Reproducing the Problem
System Information
Checklist
masterbranch