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.
public override void Initialize() { var security = AddEquity("SPY"); // Set the fill model for the requested security to backtest with the most realistic scenario // ImmediateFillModel provide no delay from network issue or brokerage rules security.SetFillModel(new ImmediateFillModel()); }
def initialize(self) -> None: security = self.add_equity("SPY") # Set the fill model for the requested security to backtest with the most realistic scenario # ImmediateFillModel provide no delay from network issue or brokerage rules 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.
public class BrokerageModelExampleAlgorithm : QCAlgorithm { public override void Initialize() { // In the Initialize method, set the security initializer to seed initial the prices and models of assets. SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices))); } } public 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()); } }
class BrokerageModelExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: # 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.
public class CustomFillModelExampleAlgorithm : QCAlgorithm { public override void Initialize() { // In the Initialize method, set the custom fill model for an added security to use the custom model var security = AddEquity("SPY"); 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); } }
class CustomFillModelExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: # In the Initialize method, set the custom fill model for an added security to use the custom model security = self.add_equity("SPY") 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.
public override void Initialize() { // Adjust the stale price time span to be 10 minutes. Settings.StalePriceTimeSpan = TimeSpan.FromMinutes(10); }
def initialize(self) -> None: # Adjust the stale price time span to be 10 minutes. self.settings.stale_price_time_span = timedelta(minutes=10)
Examples
The following examples demonstrate common practices for implementing a customized fill model.
Example 1: Volume Share Fill
The following algorithm longs the top 10 liquid constituents and short the bottom 10 liquid constituents of QQQ. To realistically fill the less liquid stocks, we implement a fill model that only fills with at most 30% of the previous second bar.
public class VolumeShareSlippageModelAlgorithm : QCAlgorithm { private List_longs = new(); private List _shorts = new(); public override void Initialize() { SetStartDate(2021, 1, 1); SetEndDate(2022, 1, 1); // The security initializer applies the VolumeShareFillModel to all assets. SetSecurityInitializer(new VolumeShareFillSecurityInitializer(this)); // Request extended market hour SPY data for trading. var qqq = AddEquity("QQQ").Symbol; // Weekly portfolio updating to allow time to capitalize on the popularity gap. UniverseSettings.Schedule.On(DateRules.WeekStart()); // Add universe to trade on the most and least liquid stocks among QQQ constituents. AddUniverse( // First, we select all QQQ constituents for the next filter on liquidity. Universe.ETF(qqq, Market.USA, UniverseSettings, (constituents) => constituents.Select(c => c.Symbol)), FundamentalSelection ); // Set a scheduled event to rebalance the portfolio at the start of every week. Schedule.On( DateRules.WeekStart(qqq), TimeRules.AfterMarketOpen(qqq), Rebalance ); } private IEnumerable FundamentalSelection(IEnumerable fundamentals) { var sortedByDollarVolume = fundamentals.OrderBy(x => x.DollarVolume).ToList(); // Add the 10 most liquid stocks to the universe to long later. _longs = sortedByDollarVolume.TakeLast(10) .Select(x => x.Symbol) .ToList(); // Add the 10 least liquid stocks to the universe to short later. _shorts = sortedByDollarVolume.Take(10) .Select(x => x.Symbol) .ToList(); return _longs.Union(_shorts); } private void Rebalance() { // Equally invest in the selected stocks to dissipate capital risk evenly. // Dollar neutral of long and short stocks to eliminate systematic risk, only capitalize the popularity gap. var targets = _longs.Select(symbol => new PortfolioTarget(symbol, 0.05m)).ToList(); targets.AddRange(_shorts.Select(symbol => new PortfolioTarget(symbol, -0.05m)).ToList()); // Liquidate the stocks that are not the most and least popular to release funds for higher expected return trades. SetHoldings(targets, liquidateExistingHoldings: true); } private class VolumeShareFillModel : FillModel { private readonly QCAlgorithm _algorithm; private readonly decimal _maximumRatio; private readonly Dictionary _absoluteRemainingByOrderId = new(); public VolumeShareFillModel(QCAlgorithm algorithm, decimal maximumRatio = 0.3m) : base() { _algorithm = algorithm; _maximumRatio = maximumRatio; } public override OrderEvent MarketFill(Security asset, MarketOrder order) { decimal absoluteRemaining; if (!_absoluteRemainingByOrderId.TryGetValue(order.Id, out absoluteRemaining)) { absoluteRemaining = order.AbsoluteQuantity; } // Create the object var fill = base.MarketFill(asset, order); // Set the fill amount fill.FillQuantity = Math.Sign(order.Quantity) * 10m; if (Math.Min(Math.Abs(fill.FillQuantity), absoluteRemaining) == absoluteRemaining) { fill.FillQuantity = Math.Sign(order.Quantity) * absoluteRemaining; fill.Status = OrderStatus.Filled; _absoluteRemainingByOrderId.Remove(order.Id); } else { fill.Status = OrderStatus.PartiallyFilled; _absoluteRemainingByOrderId[order.Id] = absoluteRemaining - Math.Abs(fill.FillQuantity); var price = fill.FillPrice; //_algorithm.Debug($"{_algorithm.Time} - Partial Fill - Remaining {_absoluteRemainingByOrderId[order.Id]} Price - {price}"); } return fill; } } private class VolumeShareFillSecurityInitializer : BrokerageModelSecurityInitializer { private VolumeShareFillModel _fillModel; public VolumeShareFillSecurityInitializer(QCAlgorithm algorithm) : base(algorithm.BrokerageModel, new FuncSecuritySeeder(algorithm.GetLastKnownPrices)) { // Create a slippage model to fill only 30% of the volume of the previous second bar to fill illiquid stocks realistically. _fillModel = new VolumeShareFillModel(algorithm, 0.3m); } public override void Initialize(Security security) { base.Initialize(security); security.SetFillModel(_fillModel); } } }
class VolumeShareFillModelAlgorithm(QCAlgorithm): longs = [] shorts = [] def initialize(self) -> None: self.set_start_date(2021, 1, 1) self.set_end_date(2022, 1, 1) # The security initializer applies the VolumeShareFillModel to all assets. self.set_security_initializer(VolumeShareFillSecurityInitializer(self)) # Request extended market hour SPY data for trading. qqq = self.add_equity("QQQ").symbol # Weekly portfolio updating to allow time to capitalize on the popularity gap. self.universe_settings.schedule.on(self.date_rules.week_start()) # Set the resolution to second since the fill model is based on the second bar. self.universe_settings.resolution = Resolution.SECOND # Add universe to trade on the most and least liquid stocks among QQQ constituents. self.add_universe( self.universe.etf(qqq, Market.USA, self.universe_settings, lambda constituents: [c.symbol for c in constituents]), self.fundamental_selection ) # Set a scheduled event to rebalance the portfolio at the start of every week. self.schedule.on( self.date_rules.week_start(qqq), self.time_rules.after_market_open(qqq), self.rebalance ) def fundamental_selection(self, fundamentals: List[Fundamental]) -> List[Symbol]: sorted_by_dollar_volume = sorted(fundamentals, key=lambda f: f.dollar_volume) # Add the 10 most liquid stocks to the universe to long later. self.longs = [f.symbol for f in sorted_by_dollar_volume[-10:]] # Add the 10 least liquid stocks to the universe to short later. self.shorts = [f.symbol for f in sorted_by_dollar_volume[:10]] return self.longs + self.shorts def rebalance(self) -> None: # Equally invest in the selected stocks to dissipate capital risk evenly. # Dollar neutral of long and short stocks to eliminate systematic risk, only capitalize the popularity gap. targets = [PortfolioTarget(symbol, 0.05) for symbol in self.longs] targets += [PortfolioTarget(symbol, -0.05) for symbol in self.shorts] # Liquidate the ones not being the most and least popular stocks to release funds for higher expected return trades. self.set_holdings(targets, liquidate_existing_holdings=True) class VolumeShareFillModel(FillModel): def __init__(self, algorithm: QCAlgorithm, maximum_ratio: float = 0.3): self.algorithm = algorithm self.maximum_ratio = maximum_ratio self.absolute_remaining_by_order_id = {} def market_fill(self, asset, order): absolute_remaining = self.absolute_remaining_by_order_id.get(order.id, order. AbsoluteQuantity) fill = super().market_fill(asset, order) # Set the fill amount to 30% of the previous second trade bar. fill.fill_quantity = np.sign(order.quantity) * asset.volume * self.maximum_ratio if (min(abs(fill.fill_quantity), absolute_remaining) == absolute_remaining): fill.fill_quantity = np.sign(order.quantity) * absolute_remaining fill.status = OrderStatus.FILLED self.absolute_remaining_by_order_id.pop(order.id, None) else: fill.status = OrderStatus.PARTIALLY_FILLED self.absolute_remaining_by_order_id[order.id] = absolute_remaining - abs(fill.fill_quantity) price = fill.fill_price return fill class VolumeShareFillSecurityInitializer(BrokerageModelSecurityInitializer): def __init__(self, algorithm: QCAlgorithm) -> None: super().__init__(algorithm.brokerage_model, FuncSecuritySeeder(algorithm.get_last_known_prices)) # Create a slippage model to fill only 30% of the volume of the previous second bar to fill illiquid stocks realistically. self.fill_model = VolumeShareFillModel(algorithm, 0.3) def initialize(self, security: Security) -> None: super().initialize(security) security.set_fill_model(self.fill_model)
Other Examples
For more examples, see the following algorithms: