Order Types

Market Orders

Introduction

Market orders are instructions to trade a specific number of units, regardless of the fill price. If your highest priority is to fill an order as fast as possible, use market orders. If the order book of the security you're trading has sufficient liquidity when the brokerage receives your order, it immediately fills. However, if there is not enough liquidity by the brokerage, you may need to wait a few minutes to fill the order, you may get partial fills, and you may incur additional costs from market impact.

Place Orders

To send a market order, call the MarketOrder, Buy, or Order method and provide a Symbol and quantity. If you don't have sufficient capital for the order, it's rejected. By default, market orders are synchronous and fill immediately.

// Buy orders
MarketOrder("IBM", 100);
Buy("AAPL", 10);
Order("SPY", 20);

// Sell orders
MarketOrder("AMZN", -15);
Sell("TSLA", 25);
Order("SPY", -20);
# Buy orders
self.market_order("IBM", 100)
self.buy("AAPL", 10)
self.order("SPY", 20)

# Sell orders
self.market_order("AMZN", -15)
self.sell("TSLA", 25)
self.order("SPY", -20)

You can also provide a tag and order properties to the Order and MarketOrder methods.

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

If you place a market order during pre-market or post-market hours, LEAN converts your order to market on open order. To override this behavior, create a custom fill model. To be notified when the default fill model allows you to submit market orders outside of regular trading hours, subscribe to GitHub Issue #3947.

Monitor Order Fills

If the brokerage has sufficient liquidity in their order book, market orders fill immediately. Otherwise, you get partial fills. To monitor the fills of your order, save a reference to the order ticket.

var ticket = MarketOrder("XLK", 10);
Debug($"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}");
ticket = self.market_order("XLK", 10)
self.debug(f"Quantity filled: {ticket.quantity_filled}; Fill price: {ticket.average_fill_price}")

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

Synchronous Timeouts

Market orders are synchronous by default, so your algorithm waits for the order to fill before moving to the next line of code. If your order takes longer than five seconds to fill, your algorithm continues executing even if the trade isn't filled. To adjust the timeout period, set the Transactions.MarketOrderFillTimeout property.

// Adjust the market fill-timeout to 30 seconds.
Transactions.MarketOrderFillTimeout = TimeSpan.FromSeconds(30);
 # Adjust the market fill-timeout to 30 seconds.
self.transactions.market_order_fill_timeout = timedelta(seconds=30)

Market orders may take a few minutes to fill for illiquid assets such as out-of-the-money Options or penny stocks.

Place Asynchronous Orders

When you trade a large portfolio of assets, you may want to send orders in batches and not wait for the response of each one. To send asynchronous orders, set the asynchronous argument to true.

MarketOrder("IBM", 100, asynchronous: true);
self.market_order("IBM", 100, True)

Cancel Orders

To cancel a market order, call the Cancel method on the OrderTicket. If you don't have the order ticket, get it from the transaction manager. The Cancel method returns an OrderResponse object to signal the success or failure of the cancel request.

var response = ticket.Cancel("Cancelled trade");
if (response.IsSuccess)
{
    Debug("Order successfully cancelled");
}
response = ticket.cancel("Cancelled Trade")
if response.is_success:
    self.debug("Order successfully cancelled")

When you cancel an order, LEAN creates a CancelOrderRequest, which have the following attributes:

To get the CancelOrderRequest for an order, call the CancelRequest method on the order ticket. The method returns nullNone if the order hasn't been cancelled.

var request = ticket.cancel_order_request();
request = ticket.cancel_order_request()

Brokerage Support

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

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

To check if your brokerage has any special requirements for market orders, see the Orders section of the brokerage model documentation.

Requirements

Market orders must be submitted during market hours for all security types.

If your algorithm place market orders at or after the last minute of regular market hours, they will be converted into market-on-open orders and will have to observe their for the following asset types:

  1. Equities
  2. Equity Options
  3. Forex
  4. CFDs
  5. Index Options

Market orders for Futures and Future Options can be submitted during extended market hours, or they will be invalid.

Example

The following backtest verifies the MarketOrder behavior. The algorithm buys SPY on the first day and liquidates the position on the second day. The following table shows the first two trades in the backtest:

TimeSymbolPriceQuantityTypeStatusValueTag
2021-07-01T09:31:00ZSPY429.1110MarketFilled4291.10
2021-07-02T09:31:00ZSPY431.54-10MarketFilled-4315.40

On July 1, 2021, the algorithm buys SPY at $429.11. The fill model fills this order at the ask close price.

On July 2, 2021, the algorithm sells the SPY holdings at $431.54. The fill model fills this order at the bid close price.

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: