Order Types

Market On Close Orders

Introduction

Market on close (MOC) orders fill at the official closing auction price for a security. Markets that operate 24/7 don't support MOC orders.

Brokerage Support

Each brokerage has a set of assets and order types they support. To avoid issues with MOC 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 MOC orders, see the Orders section of the brokerage integration documentation.

Place Orders

To send a MOC order, call the MarketOnCloseOrder method with a Symbol and quantity. If you don't have sufficient capital for the order, it is rejected.

// Buy 100 shares of AAPL at the market open
MarketOnCloseOrder("AAPL", 100);

// Sell 100 shares of AAPL at the market open
MarketOnCloseOrder("AAPL", -100);
# Buy 100 shares of AAPL at the market open
self.MarketOnCloseOrder("AAPL", 100)

# Sell 100 shares of AAPL at the market open
self.MarketOnCloseOrder("AAPL", -100)

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

MarketOnCloseOrder(symbol, quantity, tag: tag, orderProperties: orderProperties);
self.MarketOnCloseOrder(symbol, quantity, tag=tag, orderProperties=order_properties)

By default, you must place MOC orders at least 15.5 minutes before the close, but some exchanges let you submit them closer to the market closing time. To adjust the buffer period that's required, set the MarketOnCloseOrder.SubmissionTimeBuffer property.

Orders.MarketOnCloseOrder.SubmissionTimeBuffer = TimeSpan.FromMinutes(10);
MarketOnCloseOrder.SubmissionTimeBuffer = timedelta(minutes=10)

Monitor Order Fills

MOC orders fill at the closing auction. If the auction is heavily skewed to one side of the market, your MOC order may not fill. To monitor the fills of your order, save a reference to the order ticket.

// Buy 10 shares of SPY at the market close
var ticket = MarketOnCloseOrder("SPY", 10);
Debug($"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}");
# Buy 10 shares of SPY at the market close
ticket = self.MarketOnCloseOrder("SPY", 10)
self.Debug(f"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}")

You won't know the fill price of the order until after the market closes. If the asset price moves before the market close to where you can't afford the quantity of the order, the brokerage rejects your order. To increase the probability of a successful trade, leave a sufficient buying power buffer to handle the price movements before the close.

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

Update Orders

You can update the quantity and tag of MOC orders until the order fills or the brokerage prevents modifications. To update an order, pass an UpdateOrderFields object to the Update method on the OrderTicket. 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 ticket = MarketOnCloseOrder("SLV", 25, "original tag");

// Update the order
var response = ticket.Update(new UpdateOrderFields()
{
    Quantity = 50,
    Tag = "new tag"
});

// Check if the update was successful
if (response.IsSuccess)
{
    Debug("Order updated successfully");
}
# Create a new order and save the order ticket
ticket = self.MarketOnOpenOrder("SLV", 25, "original tag")

# Update the order
update_settings = UpdateOrderFields()
update_settings.Quantity = 50
update_settings.Tag = "new tag"
response = ticket.Update(update_settings)

# Check if the update was successful
if response.IsSuccess:
    self.Debug("Order updated successfully")

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

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

var tagResponse = ticket.UpdateTag(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()

Example

The following backtest verifies the MarketOnCloseOrder behavior. The following table shows the first trade in the backtest:

Submitted TimeFilled TimeSymbolPriceQuantityTypeStatusValueTag
2021-07-01T10:31:00Z2021-07-01T16:00:00ZSPY430.4310Market On CloseFilled4304.30

On July 1, 2021 at 10:31 AM Eastern Time (ET), the algorithm places a market on open order to buy SPY. The fill model fills the order on July 1, 2021 at 4:00 PM ET at a price of $430.43, which is the official closing auction price for July 1, 2021.

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: