Slippage
Key Concepts
Introduction
Slippage is the difference between the fill price you expect to get for an order and the actual fill price. Since the price can move in the direction of your trade or against the direction of your trade while you wait for the order to fill, slippage can be positive or negative. Slippage models model slippage to make backtest results more realistic.
Factors Impacting Slippage
There are many factors that can impact slippage, including the trading engine, brokerage connection, and market dynamics.
Trading Engine
How long does it take from when you place an order to when it's sent to the brokerage? Longer delays lead to more slippage.
Brokerage Connection
How long does it take for your brokerage to receive an order that you send? Slow internet connections, long travel distances, and poor infrastructure lead to more slippage
Market Dynamics
How volatile is the current market environment? More volatility leads to more slippage.
Does the market consist of sophisticated microsecond arbitrageurs? If your order creates an arbitrage opportunity, it can cause more slippage.
Set Models
The brokerage model of your algorithm automatically sets the slippage model for each security, but you can override it. To manually set the slippage model of a security, call the SetSlippageModel
method on the Security
object.
// In Initialize var security = AddEquity("SPY"); security.SetSlippageModel(new VolumeShareSlippageModel());
# In Initialize security = self.AddEquity("SPY") security.SetSlippageModel(VolumeShareSlippageModel())
You can also set the slippage 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
before you create the subscriptions.
// In Initialize SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices))); // Outside of the algorithm class 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.SetSlippageModel(new VolumeShareSlippageModel()); } }
# In Initialize self.SetSecurityInitializer(MySecurityInitializer(self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices))) # 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.SetSlippageModel(VolumeShareSlippageModel())
To view all the pre-built slippage models, see Supported Models.
Default Behavior
The brokerage model of your algorithm automatically sets the slippage model of each security. The default brokerage model is the DefaultBrokerageModel
, which uses the ConstantSlippageModel to model zero slippage for all securities.
Model Structure
Slippage models should implement the ISlippageModel
interface. Extensions of the ISlippageModel
interface must implement the GetSlippageApproximation
method, which calculates the slippage quantity.
public class MySlippageModel : ISlippageModel { public decimal GetSlippageApproximation(Security asset, Order order) { return asset.Price*0.0001m*(decimal) Math.Log10(2*(double) order.AbsoluteQuantity); } }
class MySlippageModel: def GetSlippageApproximation(self, asset: Security, order: Order) -> float: return asset.Price * 0.0001 * np.log10(2*float(order.AbsoluteQuantity))