Trade Fills
Key Concepts
Introduction
A trade fill is the quantity and price at which your brokerage executes your order in the market. Fill models model how each type of order fills to accurately simulate the behavior of a real brokerage. Fill models determine the price and quantity of your fills, can incorporate spread costs, and work with the slippage model to add slippage into the fill price. If you trade US Equities, our built-in fill models can fill your orders at the official opening and closing auction prices.
Set Models
The brokerage model of your algorithm automatically sets the fill model for each security, but you can override it. To manually set the fill model of a security, call the SetFillModel
set_fill_model
method on the Security object.
// In Initialize var security = AddEquity("SPY"); security.SetFillModel(new ImmediateFillModel());
# In Initialize security = self.add_equity("SPY") security.set_fill_model(ImmediateFillModel())
You can also set the fill model in a security initializer. If your algorithm has a dynamic universe, use the security initializer technique. In order to initialize single security subscriptions with the security initializer, call SetSecurityInitializer
set_security_initializer
before you create the subscriptions.
// In the Initialize method, set the security initializer to seed initial the prices and models of assets. SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices))); class MySecurityInitializer : BrokerageModelSecurityInitializer { public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder) : base(brokerageModel, securitySeeder) {} public override void Initialize(Security security) { // First, call the superclass definition. // This method sets the reality models of each security using the default reality models of the brokerage model. base.Initialize(security); // Next, overwrite some of the reality models security.SetFillModel(new ImmediateFillModel()); } }
# In the Initialize method, set the security initializer to seed initial the prices and models of assets. self.set_security_initializer(MySecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices))) # Outside of the algorithm class class MySecurityInitializer(BrokerageModelSecurityInitializer): def __init__(self, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder) -> None: super().__init__(brokerage_model, security_seeder) def initialize(self, security: Security) -> None: # First, call the superclass definition. # This method sets the reality models of each security using the default reality models of the brokerage model. super().initialize(security) # Next, overwrite some of the reality models security.set_fill_model(ImmediateFillModel())
To view all the pre-built fill models, see Supported Models.
Default Behavior
The brokerage model of your algorithm automatically sets the fill model of each security. The default brokerage model is the DefaultBrokerageModel
, which sets the EquityFillModel for Equities, the FutureFillModel for Futures, the FutureOptionFillModel for Future Options, and the ImmediateFillModel for all other asset classes.
Model Structure
Fill Models should extend the FillModel
class. To implement your own fill model, override the methods in the FillModel
class you wish to change.
The class has a dedicated method for each order type. Most of the methods receive a Security
and Order
object and return an OrderEvent
object that contains information about the order status, fill quantity, and errors.
// In the Initialize method, set the custom fill model security.SetFillModel(new MyFillModel()); // Define the custom fill model outside of the algorithm public class MyFillModel : FillModel { public override OrderEvent MarketFill(Security asset, MarketOrder order) { return base.MarketFill(asset, order); } public override OrderEvent LimitFill(Security asset, LimitOrder order) { return base.LimitFill(asset, order); } public override OrderEvent LimitIfTouchedFill(Security asset, LimitIfTouchOrder order) { return base.LimitIfTouchedFill(asset, order); } public override OrderEvent StopMarketFill(Security asset, StopMarketOrder order) { return base.StopMarketFill(asset, order); } public override OrderEvent StopLimitFill(Security asset, StopLimitOrder order) { return base.StopLimitFill(asset, order); } public override OrderEvent TrailingStopFill(Security asset, TrailingStopOrder order) { return TrailingStopFill(asset, order); } public override OrderEvent MarketOnOpenFill(Security asset, MarketOnOpenOrder order) { return base.MarketOnOpenFill(asset, order); } public override OrderEvent MarketOnCloseFill(Security asset, MarketOnCloseOrder order) { return base.MarketOnCloseFill(asset, order); } public override List<OrderEvent> ComboMarketFill(Order order, FillModelParameters parameters) { return base.ComboMarketFill(order, parameters); } public override List<OrderEvent> ComboLimitFill(Order order, FillModelParameters parameters) { return base.ComboLimitFill(order, parameters); } public override List<OrderEvent> ComboLegLimitFill(Order order, FillModelParameters parameters) { return base.ComboLegLimitFill(order, parameters); } }
# In the Initialize method, set the custom fill model security.set_fill_model(MyFillModel()) # Define the custom fill model outside of the algorithm class MyFillModel(FillModel): def market_fill(self, asset: Security, order: MarketOrder) -> OrderEvent: return super().market_fill(asset, order) def limit_fill(self, asset: Security, order: LimitOrder) -> OrderEvent: return super().limit_fill(asset, order) def limit_if_touched_fill(self, asset: Security, order: LimitIfTouchedOrder) -> OrderEvent: return super().limit_if_touched_fill(asset, order) def stop_market_fill(self, asset: Security, order: StopMarketOrder) -> OrderEvent: return super().stop_market_fill(asset, order) def stop_limit_fill(self, asset: Security, order: StopLimitOrder) -> OrderEvent: return super().stop_limit_fill(asset, order) def trailing_stop_fill(self, asset: Security, order: TrailingStopOrder) -> OrderEvent: return super().trailing_stop_fill(asset, order) def market_on_open_fill(self, asset: Security, order: MarketOnOpenOrder) -> OrderEvent: return super().market_on_open_fill(asset, order) def market_on_close_fill(self, asset: Security, order: MarketOnCloseOrder) -> OrderEvent: return super().market_on_close_fill(asset, order) def combo_market_fill(self, order: Order, parameters: FillModelParameters) -> List[OrderEvent]: return super().combo_market_fill(order, parameters) def combo_limit_fill(self, order: Order, parameters: FillModelParameters) -> List[OrderEvent]: return super().combo_limit_fill(order, parameters) def combo_leg_limit_fill(self, order: Order, parameters: FillModelParameters) -> List[OrderEvent]: return super().combo_leg_limit_fill(order, parameters)
For a full example algorithm, see this backtestthis backtest.
The FillModelParameters
class has the following properties:
Partial Fills
In live trading, your orders can partially fill. For example, if you have a buy limit order at the bid price for 100 shares and someone sells 10 shares with a market order, your order is partially filled. In backtests, the pre-built fill models assume orders completely fill. To simulate partial fills in backtests, create a custom fill model.
Stale Fills
Stale fills occur when you fill an order with price data that is timestamped an hour or more into the past. Stale fills usually only occur if you trade illiquid assets or if your algorithm uses daily data but you trade intraday with Scheduled Events. If your order is filled with stale data, the fill price may not be realistic. The pre-built fill models can only fill market orders with stale data. To adjust the length of time that needs to pass before an order is considered stale, set the StalePriceTimeSpan
stale_price_time_span
setting.
Settings.StalePriceTimeSpan = TimeSpan.FromMinutes(10);
self.settings.stale_price_time_span = timedelta(minutes=10)