Order Types

Combo Limit Orders

Introduction

Combo limit orders are individual orders that contain limit orders for muliple securities. Combo limit orders are different from combo leg limit orders because you must set the limit price of all the leg orders to be the same with combo limit orders. With combo leg limit orders, you can create the order without forcing each leg to have the same limit price. Combo limit orders currently only work for trading Option contracts and their underlying Equities.

Place Orders

To send a combo limit order, create multiple Leg objects to represent the legs of the combo order, then call the ComboLimitOrder method. At least one leg must have a positive quantity and a tleast one leg must have a negative quantity. The legs must each target a unique contract, but don't set the OrderPrice property for any of the legs. If you don't have sufficient capital for the order, it's rejected.

foreach (var kvp in slice.OptionChains)
{
    // Select contracts
    var contracts = kvp.Value.Contracts.Values.ToList();
    if (contracts.Count < 2) 
    {
        return;
    }

    // Create order legs
    var legs = new List&lgt;Leg>()
    {
        Leg.Create(contracts[0].Symbol, 1),
        Leg.Create(contracts[1].Symbol, -1)
    };

    // Calculate limit price
    var limitPrice = Math.Round(legs.Select(leg => Securities[leg.Symbol].Close).Sum() * 0.95m, 2);

    // Place order
    ComboLimitOrder(legs, quantity: 1, limitPrice: limitPrice);
}
for canonical_symbol, chain in slice.OptionChains.items():
    # Select contracts
    contracts = [c for c in chain][:2]
    if len(contracts) < 2:
        return

    # Create order legs
    legs = []
    quantities = [1, -1]
    for i, contract in enumerate(contracts):
        legs.append(Leg.Create(contract.Symbol, quantities[i]))
    
    # Calculate limit price
    limit_price = round(sum([self.Securities[leg.Symbol].Close for leg in legs]) * 0.95, 2)

    # Place order
    self.ComboLimitOrder(legs, 1, limit_price)

The quantity of the legs sets the ratio of the leg orders while the quantity argument of the ComboLimitOrder method sets the combo order size and acts as a global multiplier. In the preceding example, if we set the global multiplier to two, then the algorithm buys two units of the first contract and sells two units of the second contract. The quantity also sets the order direction of the combo limit order, which affects how the fill model fills the order.

You can also provide a tag and order properties to the ComboLimitOrder method.

ComboLimitOrder(legs, quantity, limitPrice, tag: tag, orderProperties: orderProperties);
self.ComboLimitOrder(legs, quantity, limit_price, tag=tag, orderProperties=order_properties)

Monitor Order Fills

Combo limit orders fill all the legs at the same time. Each leg can fill when the sum of the security prices of the legs pass the limit price of the combo order. To monitor the fills of your order, save a reference to the order tickets.

var tickets = ComboLimitOrder(legs, 1, limitPrice);
foreach (var ticket in tickets)
{
    Debug($"Symbol: {ticket.Symbol}; Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}");
}
tickets = self.ComboLimitOrder(legs, 1, limit_price)
for ticket in tickets:
    self.Debug(f"Symbol: {ticket.Symbol}; Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}")

For more information about how LEAN models order fills in backtests, see Trade Fills.

Update Orders

You can update the quantity, limit price, and tag of the limit orders in each leg until the combo order fills or the brokerage prevents modifications. To update an order, pass an UpdateOrderFields object to the Update method on the OrderTicket. To update the limit price of the combo order, you only need to update the limit price of one of the leg orders. The Update method returns an OrderResponse to signal the success or failure of the update request.

// Create a new order and save the order ticket
var tickets = ComboLimitOrder(legs, quantity: 1, limitPrice: limitPrice);

// Update the leg orders
foreach (var ticket in tickets)
{
    var response = ticket.Update(new UpdateOrderFields() 
    {
        Quantity = 2 * Math.Sign(ticket.Quantity),
        LimitPrice = ticket.Get(OrderField.LimitPrice) + 0.01m,
        Tag = $"Update #{ticket.UpdateRequests.Count + 1}"
    }); 

    // Check if the update was successful
    if (response.IsSuccess) 
    {
        Debug($"Order updated successfully for {ticket.Symbol}");
    }
}
# Create a new order and save the order tickets
tickets = self.ComboLimitOrder(legs, 1, limit_price)

# Update the leg orders
for ticket in tickets:
    update_settings = UpdateOrderFields()
    update_settings.Quantity = 2 * np.sign(ticket.Quantity)
    update_settings.LimitPrice = ticket.Get(OrderField.LimitPrice) + 0.01
    update_settings.Tag = f"Update #{len(ticket.UpdateRequests) + 1}"
    response = ticket.Update(update_settings)

    # Check if the update was successful
    if response.IsSuccess:
        self.Debug(f"Order updated successfully for {ticket.Symbol}")

To update individual fields of an order, call any of the following methods:

  • UpdateLimitPrice
  • UpdateQuantity
  • UpdateTag
var limitResponse = ticket.UpdateLimitPrice(limitPrice, tag);

var quantityResponse = ticket.UpdateQuantity(quantity, tag);

var tagResponse = ticket.UpdateTag(tag);

response = ticket.UpdateLimitPrice(limitPrice, tag)

response = ticket.UpdateQuantity(quantity, tag)

response = ticket.UpdateTag(tag)

When you update an order, LEAN creates an UpdateOrderRequest object, which have the following attributes:

To get a list of UpdateOrderRequest objects for an order, call the UpdateRequests method.

var updateRequests = orderTicket.UpdateRequests();
update_requests = order_ticket.UpdateRequests()

Brokerage Support

Each brokerage has a set of assets and order types they support. To avoid issues with combo limit orders, set the brokerage model to a brokerage that supports them.

SetBrokerageModel(BrokerageName.QuantConnectBrokerage);
self.SetBrokerageModel(BrokerageName.QuantConnectBrokerage)

To check if your brokerage has any special requirements for combo limit orders, see the Orders section of the brokerage integration documentation.

Requirements

Warning: include(/tmp/docs/5827695411a093733b17cfeabb49c9818c4b6502/Resourceslimit-orders-requirements.php): failed to open stream: No such file or directory in /tmp/docs/5827695411a093733b17cfeabb49c9818c4b6502/03 Writing Algorithms/22 Trading and Orders/03 Order Types/11 Combo Limit Orders/07 Requirements.php on line 3 Warning: include(): Failed opening '/tmp/docs/5827695411a093733b17cfeabb49c9818c4b6502/Resourceslimit-orders-requirements.php' for inclusion (include_path='/var/www/beta/core/libraries/Google:/var/www/beta:.:/usr/share/php') in /tmp/docs/5827695411a093733b17cfeabb49c9818c4b6502/03 Writing Algorithms/22 Trading and Orders/03 Order Types/11 Combo Limit Orders/07 Requirements.php on line 3

Example

The following backtest verifies the ComboLimitOrder behavior. The algorithm buys one contract and sells one contract at the same time. The following table shows the two trades in the backtest:

TimeSymbolPriceQuantityTypeStatusValueTag
2015-12-24T09:31:00ZGOOG 16011SC0074500016.501BuyFilled16.50
2015-12-24T09:31:00ZGOOG 160115C0074750014.60-1SellFilled-14.60

On December 24, 2015 at 9:31 AM Eastern Time (ET), the algorithm places a combo limit order to buy one GOOG 16011SC00745000 contract and sell one GOOG 160115C00747500 contracts. The limit price is 75% of the contract spread, which equals $2.02. The combo order doesn't fill immediately because the contract spread $2.40 > $2.02. By 9:36 AM, the spread drops to $1.90, which is below the limit price, so the fill model fills the combo limit order.

To reproduce these results, backtest the following algorithm:

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: