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.add_equity("SPY")
security.set_slippage_model(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 SetSecurityInitializerset_security_initializer 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.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.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 NullSlippageModel 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.

// In the Initialize method, set the slippage model
security.SetSlippageModel(new MySlippageModel());

// Define the custom slippage model
public class MySlippageModel : ISlippageModel 
{
    public decimal GetSlippageApproximation(Security asset, Order order) 
    {
        return asset.Price*0.0001m*(decimal) Math.Log10(2*(double) order.AbsoluteQuantity);
    }
}
# In the Initialize method, set the slippage model
security.set_slippage_model(MySlippageModel())

# Define the custom slippage model
class MySlippageModel:

    def get_slippage_approximation(self, asset: Security, order: Order) -> float:
        return asset.price * 0.0001 * np.log10(2*float(order.absolute_quantity))

For a full example algorithm, see this backtestthis backtest.

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: