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 get partial fills and may incur additional costs from market impact.

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.SetBrokerageModel(BrokerageName.QuantConnectBrokerage)

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

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.MarketOrder("IBM", 100)
self.Buy("AAPL", 10)
self.Order("SPY", 20)

# Sell orders
self.MarketOrder("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.MarketOrder(symbol, quantity, tag=tag, orderProperties=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.MarketOrder("XLK", 10)
self.Debug(f"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}")

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.MarketOrderFillTimeout = timedelta(seconds=30)

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.MarketOrder("IBM", 100, True)

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: