Supported Models

Immediate Model

Introduction

The ImmediateFillModel is the default fill model if you trade non-Equity assets with the DefaultBrokerageModel. This fill model fills trades completely and immediately.

security.SetFillModel(new ImmediateFillModel());
security.SetFillModel(ImmediateFillModel())

The fill logic of each order depends on the order type, the data format of the security subscription, and the order direction. The following tables show the fill price of each order given these factors. To determine the fill price of an order, the fill model first checks the most recent tick for the security. If your security subscription doesn't provide tick data, the fill model checks the most recent QuoteBar. If your security subscription doesn't provide quote data, the fill model checks the most recent TradeBar.

To view the implementation of this model, see the LEAN GitHub repository.

Market Orders

The following table describes the fill price of market orders for each data format and order direction:

Data FormatTickTypeOrder DirectionFill Price
TickQuoteBuyquote price + slippage
TickQuoteSellquote price - slippage
TickTradeBuytrade price + slippage
TickTradeSelltrade price - slippage
QuoteBar
Buyask close price + slippage
QuoteBar
Sellbid close price - slippage
TradeBar
Buyclose price + slippage
TradeBar
Sellclose price - slippage

The model only fills market orders if the exchange is open.

Limit Orders

The following table describes the fill logic of limit orders for each data format and order direction:

Data FormatTickTypeOrder Direction Fill ConditionFill Price
TickQuoteBuyquote price < limit pricemin(quote price, limit price)
TickQuoteSellquote price > limit pricemax(quote price, limit price)
TickTradeBuytrade price < limit pricemin(trade price, limit price)
TickTradeSelltrade price > limit pricemax(trade price, limit price)
QuoteBar
Buyask low price < limit pricemin(ask high price, limit price)
QuoteBar
Sellbid high price > limit pricemax(bid low price, limit price)
TradeBar
Buylow price < limit pricemin(high price, limit price)
TradeBar
Sellhigh price > limit pricemax(low price, limit price)

The model won't fill limit orders with stale data.

Limit if Touched Orders

The model converts a limit if touched order to a limit order when the trigger condition is met. The following table describes the trigger condition of limit if touched orders for each data format and order direction:

Data FormatTickTypeOrder DirectionTrigger Condition
TickQuoteBuyquote price <= trigger price
TickQuoteSellquote price >= trigger price
TickTradeBuytrade price <= trigger price
TickTradeSelltrade price >= trigger price
TradeBar
Buylow price <= trigger price
TradeBar
Sellhigh price >= trigger price

Once the limit if touched order triggers, to fill the order, the model checks the bid price for buy orders and the ask price for sell orders.

To get the bid price, the model uses the following procedure:

  1. If the subscription provides Tick data and the most recent batch of ticks contains a buy quote, use the bid price of the most recent quote tick.
  2. If the subscription provides QuoteBar data, use the closing bid price of the most recent QuoteBar.

To get the ask price, the model uses the following procedure:

  1. If the subscription provides Tick data and the most recent batch of ticks contains a sell quote, use the ask price of the most recent quote tick.
  2. If the subscription provides QuoteBar data, use the closing ask price of the most recent QuoteBar.

If neither of the preceding procedures yield a result, the model uses the following procedure to get the bid or ask price:

  1. If the subscription provides Tick data and the most recent batch of ticks contains a tick of type TickType.Trade, use the last trade price.
  2. If the subscription provides TradeBar data, use the closing bid price of the most recent QuoteBar.

Buy orders fill when the bid price <= limit price and sell orders fill when the ask price >= limit price. The order fills at the limit price. The model won't trigger or fill limit if touched orders with stale data.

Stop Market Orders

The following table describes the fill logic of stop market orders for each data format and order direction. Once the stop condition is met, the model fills the orders and sets the fill price.

Data FormatTickTypeOrder Direction Stop ConditionFill Price
TickQuoteBuyquote price > stop pricemax(stop price, quote price + slippage)
TickQuoteSellquote price < stop pricemin(stop price, quote price - slippage)
TickTradeBuytrade price > stop pricemax(stop price, last trade price + slippage)
TickTradeSelltrade price < stop pricemin(stop price, last trade price - slippage)
QuoteBar
Buyask high price > stop pricemax(stop price, ask close price + slippage)
QuoteBar
Sellbid low price < stop pricemin(stop price, bid close price - slippage)
TradeBar
Buyhigh price > stop pricemax(stop price, close price + slippage)
TradeBar
Selllow price < stop pricemin(stop price, close price - slippage)

The model only fills stop market orders when the exchange is open. The model won't fill stop market orders with stale data.

Stop Limit Orders

The following table describes the fill logic of stop limit orders for each data format and order direction. Once the stop condition is met, the model starts to check the fill condition. Once the fill condition is met, the model fills the orders and sets the fill price.

Data FormatTickTypeOrder DirectionStop ConditionFill ConditionFill Price
TickQuoteBuyquote price > stop pricequote price < limit pricemin(quote price, limit price)
TickQuoteSellquote price < stop pricequote price > limit pricemax(quote price, limit price)
TickTradeBuytrade price > stop pricetrade price < limit pricemin(trade price, limit price)
TickTradeSelltrade price < stop pricetrade price > limit pricemax(trade price, limit price)
QuoteBar
Buyask high price > stop priceask close price < limit pricemin(ask high price, limit price)
QuoteBar
Sellbid low price < stop pricebid close price > limit pricemax(bid low price, limit price)
TradeBar
Buyhigh price > stop priceclose price < limit pricemin(high price, limit price)
TradeBar
Selllow price < stop priceclose price > limit pricemax(low price, limit price)

The model won't fill stop limit orders with stale data.

Market on Open Orders

The following table describes the fill price of market on open orders for each data format and order side:

Data FormatTickTypeOrder DirectionFill Price
TickQuoteBuyquote price + slippage
TickQuoteSellquote price - slippage
TickTradeBuytrade price + slippage
TickTradeSelltrade price - slippage
QuoteBar
Buyask open price + slippage
QuoteBar
Sellbid open price - slippage
TradeBar
Buyopen price + slippage
TradeBar
Sellopen price - slippage

The model won't fill market on open orders during pre-market hours.

Market on Closed Orders

The following table describes the fill price of market on close orders for each data format and order side:

Data FormatTickTypeOrder DirectionFill Price
TickQuoteBuyquote price + slippage
TickQuoteSellquote price - slippage
TickTradeBuytrade price + slippage
TickTradeSelltrade price - slippage
QuoteBar
Buyask close price + slippage
QuoteBar
Sellbid close price - slippage
TradeBar
Buyclose price + slippage
TradeBar
Sellclose price - slippage

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: