Lean icon indicating copy to clipboard operation
Lean copied to clipboard

[Feature Request] Support for Bracket Order

Open fishstoryyy opened this issue 1 year ago • 5 comments

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 master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [ ] I have provided detailed steps to reproduce the issue

fishstoryyy avatar Aug 14 '24 04:08 fishstoryyy

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 avatar Nov 11 '24 04:11 pandaxbacon

@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.

fishstoryyy avatar Nov 11 '24 05:11 fishstoryyy

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

pandaxbacon avatar Nov 11 '24 06:11 pandaxbacon

This would be an amazing addition - I'm just coming here to voice my support!

justinfarrelldev avatar Dec 17 '24 22:12 justinfarrelldev

Would be really nice to have native support for brackets!

debegr92 avatar Mar 07 '25 16:03 debegr92

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.

drewns1 avatar Aug 15 '25 02:08 drewns1