Skip to content

[Feature Request] Support for Bracket Order #8253

Description

@fishstoryyy

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

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue

Activity

  1. changed the title [-][Feature Request support for Bracket Order[/-] [+][Feature Request] Support for Bracket Order[/+] on Aug 14, 2024
  2. pandaxbacon commented on Nov 11, 2024

    @pandaxbacon

    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.

  3. fishstoryyy commented on Nov 11, 2024

    @fishstoryyy
    Author

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

  4. pandaxbacon commented on Nov 11, 2024

    @pandaxbacon

    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

  5. justinfarrelldev commented on Dec 17, 2024

    @justinfarrelldev

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

  6. debegr92 commented on Mar 7, 2025

    @debegr92

    Would be really nice to have native support for brackets!

  7. drewns1 commented on Aug 15, 2025

    @drewns1

    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.

  8. Martin-Molinero commented on Sep 25, 2026

    @Martin-Molinero
    Member

    Native contingent orders are available since Lean 18130 (#9828): bracket_order(symbol, quantity, take_profit_price, stop_loss_price, limit_price=None), plus one_cancels_other_order, one_triggers_other_order and one_updates_other_order for 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions