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 SetSlippageModelset_slippage_model method on the Security object.

public override void Initialize()
{
    var security = AddEquity("SPY");
    // Set the slippage model for the requested security to backtest with the most realistic scenario
    // VolumeShareSlippageModel has slippage size affected by the volume of the order compared to the actual filled volume of the bar
    // It is only valid for the security with a volume property, while being more accurate with denser resolution
    security.SetSlippageModel(new VolumeShareSlippageModel());
}
def initialize(self) -> None:
    security = self.add_equity("SPY")
    # Set the slippage model for the requested security to backtest with the most realistic scenario
    # VolumeShareSlippageModel has slippage size affected by the volume of the order compared to the actual filled volume of the bar
    # It is only valid for the security with a volume property, while being more accurate with denser resolution
    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.

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.SetSlippageModel(new VolumeShareSlippageModel());    }
}
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_slippage_model(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 GetSlippageApproximationget_slippage_approximation method, which calculates the slippage quantity.

public class CustomSlippageModelExampleAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        // In the Initialize method, set the custom slippage model for an added security to use the custom model
        var security = AddEquity("SPY");
        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);
    }
}
class CustomSlippageModelExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        # In the Initialize method, set the custom slippage model for an added security to use the custom model
        security = self.add_equity("SPY")
        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: