Lean
Lean copied to clipboard
[Feature Request] Support for Bracket Order
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: https://github.com/QuantConnect/Lean/issues/5219
Actual Behavior
No native support of bracket orders.
Potential Solution
Reproducing the Problem
System Information
Checklist
- [x] I have completely filled out this template
- [x] I have confirmed that this issue exists on the current
masterbranch - [x] I have confirmed that this is not a duplicate issue by searching issues
- [ ] I have provided detailed steps to reproduce the issue
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 order
Source: https://interactivebrokers.github.io/tws-api/bracket_order.html
Mainly 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 orders
i 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.