Order Types

Market Orders


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, Sell, or Ordermarket_order, buy, sell, 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 MarketOrderorder and market_order 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.MarketOrderFillTimeouttransactions.market_order_fill_timeout 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 Cancelcancel method on the OrderTicket. If you don't have the order ticket, get it from the transaction manager. The Cancelcancel 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 CancelRequestcancel_order_request 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.


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


You can submit market orders during regular trading hours for all asset classes. For Futures and Future Options, you can place market orders during regular and extended market hours. For all other asset classes, if you place market orders at/after the last minute of regular market hours, LEAN converts the market orders into market-on-open orders as long as the asset class supports them. To view the trading hours of each asset class, follow these steps:

  1. Open the Asset Classes documentation.
  2. Click an asset class.
  3. Click Market Hours.


The following backtest verifies the MarketOrdermarket_order 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:


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: