Supported Models
Latest Price Model
Introduction
The LatestPriceFillModel
fills trades completely and immediately.
security.SetFillModel(new LatestPriceFillModel());
security.set_fill_model(LatestPriceFillModel())
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 there is no QuoteBar
available, it uses the OHLC prices from the Security
object. If there is a QuoteBar
available, the fill model gets the most recent TradeBar
. If the TradeBar
is more recent, it uses the TradeBar
data. Otherwise, it uses the QuoteBar
data.
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 Format | TickType | Order Direction | Fill Price |
---|---|---|---|
Tick | Quote | Buy | quote price + slippage |
Tick | Quote | Sell | quote price - slippage |
Tick | Trade | Buy | trade price + slippage |
Tick | Trade | Sell | trade price - slippage |
QuoteBar | Buy | ask close price + slippage | |
QuoteBar | Sell | bid close price - slippage | |
TradeBar | Buy | close price + slippage | |
TradeBar | Sell | close price - slippage |
The model only fills market orders if the exchange is open and it immediately fills them. If your algorithm places a market order at 10 AM, it fills at 10 AM.
Limit Orders
The following table describes the fill logic of limit orders for each data format and order direction:
Data Format | TickType | Order Direction | Fill Condition | Fill Price |
---|---|---|---|---|
Tick | Quote | Buy | quote price < limit price | min(quote price, limit price) |
Tick | Quote | Sell | quote price > limit price | max(quote price, limit price) |
Tick | Trade | Buy | trade price < limit price | min(trade price, limit price) |
Tick | Trade | Sell | trade price > limit price | max(trade price, limit price) |
QuoteBar | Buy | ask low price < limit price | min(ask high price, limit price) | |
QuoteBar | Sell | bid high price > limit price | max(bid low price, limit price) | |
TradeBar | Buy | low price < limit price | min(high price, limit price) | |
TradeBar | Sell | high price > limit price | max(low price, limit price) |
The model won't fill limit orders with stale data or data with the order timestamp to avoid look-ahead bias.
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 Format | TickType | Order Direction | Trigger Condition |
---|---|---|---|
Tick | Quote | Buy | quote price <= trigger price |
Tick | Quote | Sell | quote price >= trigger price |
Tick | Trade | Buy | trade price <= trigger price |
Tick | Trade | Sell | trade price >= trigger price |
TradeBar | Buy | low price <= trigger price | |
TradeBar | Sell | high 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:
- 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. - If the subscription provides
QuoteBar
data, use the closing bid price of the most recentQuoteBar
.
To get the ask price, the model uses the following procedure:
- 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. - If the subscription provides
QuoteBar
data, use the closing ask price of the most recentQuoteBar
.
If neither of the preceding procedures yield a result, the model uses the following procedure to get the bid or ask price:
- If the subscription provides
Tick
data and the most recent batch of ticks contains a tick of typeTickType.Trade
, use the last trade price. - If the subscription provides
TradeBar
data, use the closing bid price of the most recentQuoteBar
.
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 or data with the order timestamp to avoid look-ahead bias.
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 Format | TickType | Order Direction | Stop Condition | Fill Price |
---|---|---|---|---|
Tick | Quote | Buy | quote price > stop price | max(stop price, quote price + slippage) |
Tick | Quote | Sell | quote price < stop price | min(stop price, quote price - slippage) |
Tick | Trade | Buy | trade price > stop price | max(stop price, last trade price + slippage) |
Tick | Trade | Sell | trade price < stop price | min(stop price, last trade price - slippage) |
QuoteBar | Buy | ask high price > stop price | max(stop price, ask close price + slippage) | |
QuoteBar | Sell | bid low price < stop price | min(stop price, bid close price - slippage) | |
TradeBar | Buy | high price > stop price | max(stop price, close price + slippage) | |
TradeBar | Sell | low price < stop price | min(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 or data with the order timestamp to avoid look-ahead bias.
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 Format | TickType | Order Direction | Stop Condition | Fill Condition | Fill Price |
---|---|---|---|---|---|
Tick | Quote | Buy | quote price > stop price | quote price < limit price | min(quote price, limit price) |
Tick | Quote | Sell | quote price < stop price | quote price > limit price | max(quote price, limit price) |
Tick | Trade | Buy | trade price > stop price | trade price < limit price | min(trade price, limit price) |
Tick | Trade | Sell | trade price < stop price | trade price > limit price | max(trade price, limit price) |
QuoteBar | Buy | ask high price > stop price | ask close price < limit price | min(ask high price, limit price) | |
QuoteBar | Sell | bid low price < stop price | bid close price > limit price | max(bid low price, limit price) | |
TradeBar | Buy | high price > stop price | close price < limit price | min(high price, limit price) | |
TradeBar | Sell | low price < stop price | close price > limit price | max(low price, limit price) |
The model won't fill stop limit orders with stale data or data with the order timestamp to avoid look-ahead bias.
Trailing Stop Orders
The following table describes the fill logic of trailing stop 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 Format | TickType | Order Direction | Stop Condition | Fill Price |
---|---|---|---|---|
Tick | Quote | Buy | quote price > stop price | max(stop price, quote price + slippage) |
Tick | Quote | Sell | quote price < stop price | min(stop price, quote price - slippage) |
Tick | Trade | Buy | trade price > stop price | max(stop price, last trade price + slippage) |
Tick | Trade | Sell | trade price < stop price | min(stop price, last trade price - slippage) |
QuoteBar | Buy | ask high price > stop price | max(stop price, ask close price + slippage) | |
QuoteBar | Sell | bid low price < stop price | min(stop price, bid close price - slippage) | |
TradeBar | Buy | high price > stop price | max(stop price, close price + slippage) | |
TradeBar | Sell | low price < stop price | min(stop price, close price - slippage) |
While the stop condition is not met, the model updates the stop price under certain conditions. The following table shows the update condition and stop price value for currency-based trailing amounts:
Data Format | TickType | Order Direction | Update Condition | Stop Price |
---|---|---|---|---|
Tick | Quote | Buy | stop price - quote price <= trailing amount | quote price + trailing amount |
Tick | Quote | Sell | quote price - stop price <= trailing amount | quote price - trailing amount |
Tick | Trade | Buy | stop price - trade price <= trailing amount | trade price + trailing amount |
Tick | Trade | Sell | trade price - stop price <= trailing amount | trade price - trailing amount |
QuoteBar | Buy | stop price - ask low price <= trailing amount | ask low price + trailing amount | |
QuoteBar | Sell | bid high price - stop price <= trailing amount | bid high price - trailing amount | |
TradeBar | Buy | stop price - low price <= trailing amount | low price + trailing amount | |
TradeBar | Sell | high price - stop price <= trailing amount | high price - trailing amount |
The following table shows the update condition and stop price value for percentage-based trailing amounts.
Data Format | TickType | Order Direction | Update Condition | Stop Price |
---|---|---|---|---|
Tick | Quote | Buy | stop price - quote price <= quote price * trailing amount | quote price * (1 + trailing amount) |
Tick | Quote | Sell | quote price - stop price <= quote price * trailing amount | quote price * (1 - trailing amount) |
Tick | Trade | Buy | stop price - trade price <= trade price * trailing amount | trade price * (1 + trailing amount) |
Tick | Trade | Sell | trade price - stop price <= trade price * trailing amount | trade price * (1 - trailing amount) |
QuoteBar | Buy | stop price - ask low price <= ask low price * trailing amount | ask low price * (1 + trailing amount) | |
QuoteBar | Sell | bid high price - stop price <= bid high price * trailing amount | bid high price * (1 - trailing amount) | |
TradeBar | Buy | stop price - low price <= stop price * trailing amount | low price * (1 + trailing amount) | |
TradeBar | Sell | high price - stop price <= high price * trailing amount | high price * (1 - trailing amount) |
The model only fills trailing stop orders when the exchange is open.
The model won't fill trailing stop orders with stale data or data with the order timestamp to avoid look-ahead bias.
Market on Open Orders
The following table describes the fill price of market on open orders for each data format and order side:
Data Format | TickType | Order Direction | Fill Price |
---|---|---|---|
Tick | Quote | Buy | quote price + slippage |
Tick | Quote | Sell | quote price - slippage |
Tick | Trade | Buy | trade price + slippage |
Tick | Trade | Sell | trade price - slippage |
QuoteBar | Buy | ask open price + slippage | |
QuoteBar | Sell | bid open price - slippage | |
TradeBar | Buy | open price + slippage | |
TradeBar | Sell | open price - slippage |
The model won't fill market on open orders during pre-market hours.
The model won't fill market on open orders with stale data or data with the order timestamp to avoid look-ahead bias.
Market on Close Orders
The following table describes the fill price of market on close orders for each data format and order side:
Data Format | TickType | Order Direction | Fill Price |
---|---|---|---|
Tick | Quote | Buy | quote price + slippage |
Tick | Quote | Sell | quote price - slippage |
Tick | Trade | Buy | trade price + slippage |
Tick | Trade | Sell | trade price - slippage |
QuoteBar | Buy | ask close price + slippage | |
QuoteBar | Sell | bid close price - slippage | |
TradeBar | Buy | close price + slippage | |
TradeBar | Sell | close price - slippage |
The model won't fill market on close orders with stale data or data with the order timestamp to avoid look-ahead bias.
Combo Market Orders
The following table describes the fill price of combo market orders for each data format and order direction:
Data Format | TickType | Order Direction | Fill Price |
---|---|---|---|
Tick | Quote | Buy | Ask quote price + slippage |
Tick | Quote | Sell | Bid quote price - slippage |
Tick | Trade | Buy | Trade price + slippage |
Tick | Trade | Sell | Trade price - slippage |
QuoteBar | Buy | Ask close price + slippage | |
QuoteBar | Sell | Bid close price - slippage | |
TradeBar | Buy | Close price + slippage | |
TradeBar | Sell | Close price - slippage |
The model only fills combo market orders if all the following conditions are met:
- The exchange is open
- The data isn't stale
- All the legs can fill in the same time step after the order time step
The fill quantity of each leg is the product of the leg order quantity and the combo market order quantity.
Combo Limit Orders
To fill combo limit orders, the fill model calculates the aggregate price of the combo order, which is the sum of prices for each security in the order legs. The price of each security is a function of the data format and order direction. Legs with a positive order quantity increase the aggregate price and legs with a negative quantity decrease the aggregate price. The following table shows how the fill model calculates the security prices.
Data Format | TickType | Combo Order Direction | Leg Order Direction | Price |
---|---|---|---|---|
Tick | Quote | Buy or sell | Buy | Ask price |
Tick | Quote | Buy or sell | Sell | Bid price |
Tick | Trade | Buy or sell | Buy or sell | Trade price |
QuoteBar | Buy | Buy | Ask low price | |
QuoteBar | Buy | Sell | Bid low price | |
QuoteBar | Sell | Buy | Ask high price | |
QuoteBar | Sell | Sell | Bid high price | |
TradeBar | Buy | Buy or sell | Low price | |
TradeBar | Sell | Buy or sell | High price |
After the fill model calculates the aggregate price of the combo order, it checks if it should fill the order. The following table describes the fill condition of the combo order and the fill price price of each leg:
Data Format | TickType | Combo Order Direction | Fill Condition | Leg Order Direction | Fill Price |
---|---|---|---|---|---|
Tick | Quote | Buy | Aggregate price < combo limit price | Buy or sell | Quote price |
Tick | Quote | Sell | Aggregate price > combo limit price | Buy or sell | Quote price |
Tick | Trade | Buy | Aggregate price < combo limit price | Buy or sell | Trade price |
Tick | Trade | Sell | Aggregate price > combo limit price | Buy or sell | Trade price |
QuoteBar | Buy | Aggregate price < combo limit price | Buy | Ask low price | |
QuoteBar | Buy | Aggregate price < combo limit price | Sell | Bid low price | |
QuoteBar | Sell | Aggregate price > combo limit price | Buy | Ask high price | |
QuoteBar | Sell | Aggregate price > combo limit price | Sell | Bid high price | |
TradeBar | Buy | Aggregate price < combo limit price | Buy or sell | Low price | |
TradeBar | Sell | Aggregate price > combo limit price | Buy or sell | High price |
The model only fills combo limit orders if the data isn't stale and all the legs can fill in the same time step after the order time step. The fill quantity of each leg is the product of the leg order quantity and the combo order quantity.
Combo Leg Limit Orders
The following table describes the fill logic of combo leg limit orders for each data format and order direction. The order direction in the table represents the order direction of the order leg, not the order direction of the combo order.
Data Format | TickType | Order Direction | Fill Condition | Fill Price |
---|---|---|---|---|
Tick | Quote | Buy | Ask price < limit price | min(ask price, limit price) |
Tick | Quote | Sell | Bid price > limit price | max(bid price, limit price) |
Tick | Trade | Buy | Trade price < limit price | min(trade price, limit price) |
Tick | Trade | Sell | Trade price > limit price | max(trade price, limit price) |
QuoteBar | Buy | Ask low price < limit price | min(ask high price, limit price) | |
QuoteBar | Sell | Bid high price > limit price | max(bid low price, limit price) | |
TradeBar | Buy | Low price < limit price | min(high price, limit price) | |
TradeBar | Sell | High price > limit price | max(low price, limit price) |
The model only fills combo leg limit orders if all the following conditions are met:
- The exchange is open
- The data isn't stale
- All the legs can fill in the same time step after the order time step
The fill quantity is the product of the leg order quantity and the combo order quantity.