Short Availability

Key Concepts

Introduction

To short a security, you need to borrow shares from another investor or organization that owns the shares. A shortable provider is a model that tracks the number of shares that are available for you to borrow. A shortable provider can make your backtest results more realistic because it doesn't let you place a short trade if there are no shares available for you to borrow.

Set Providers

The brokerage model of your algorithm automatically sets the settlement model for each security, but you can override it. To manually set the shortable provider of a security, call the SetShortableProvider method on the Security object.

// In Initialize
var security = AddEquity("SPY");
security.SetShortableProvider(new LocalDiskShortableProvider("axos"));
# In Initialize
security = self.AddEquity("SPY")
security.SetShortableProvider(LocalDiskShortableProvider("axos"))

You can also set the shortable provider in a security initializer. If your algorithm has a 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(CustomSecurityInitializer);
AddEquity("SPY");

private void CustomSecurityInitializer(Security security)
{
    security.SetShortableProvider(new LocalDiskShortableProvider("axos"));
}
# In Initialize
self.SetSecurityInitializer(self.CustomSecurityInitializer)
self.AddEquity("SPY")

def CustomSecurityInitializer(self, security: Security) -> None:
    security.SetShortableProvider(LocalDiskShortableProvider("axos"))

If you call the SetSecurityInitializer method, it overwrites the default security initializer. The default security initializer uses the security-level reality models of the brokerage model to set the following reality models of each security:

To extend upon the default security initializer instead of overwriting it, create a custom BrokerageModelSecurityInitializer.

// 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.SetShortableProvider(new LocalDiskShortableProvider("axos"));    
    }
}
# 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.SetShortableProvider(LocalDiskShortableProvider("axos"))

To view all the pre-built shortable providers, see Supported Providers.

Default Behavior

The brokerage model of your algorithm automatically sets the shortable provider for each security. The default brokerage model is the DefaultBrokerageModel, which uses the NullShortableProvider.

Provider Structure

Shortable providers must implement the IShortableProvider interface. Shortable providers that implement the IShortableProvider interface must implement the FeeRate, RebateRate, and ShortableQuantity methods. These methods receives a Symbol and the local time of the algorithm. The FeeRate method returns the borrow fee rate. The RebateRate method returns the borrow rebate rate. The ShortableQuantity returns the shortable quantity.

// In the Initialize method, set the shortable provider
security.SetShortableProvider(new MyShortableProvider());

// Define the custom shortable provider  
class MyShortableProvider : IShortableProvider
{
    public decimal FeeRate(Symbol symbol, DateTime localTime)
    {
        return 0.0025m;
    }

    public decimal RebateRate(Symbol symbol, DateTime localTime)
    {
        return 0.0507m;
    }

    public long? ShortableQuantity(Symbol symbol, DateTime localTime)
    {
        return 10000;
    }
}
# In the Initialize method, set the shortable provider
security.SetShortableProvider(MyShortableProvider())

# Define the custom shortable provider 
class MyShortableProvider(NullShortableProvider):

    def FeeRate(self, symbol: Symbol, localTime: datetime) -> float:
        return 0.0025

    def RebateRate(self, symbol: Symbol, localTime: datetime) -> float:
        return 0.0507
    
    def ShortableQuantity(self, symbol: Symbol, localTime: datetime) -> float:
        return 10000

For a full example algorithm, see this backtestthis backtest.

Order Failures

If you place an order to short more shares than are available to borrow, LEAN rejects the order and applies the following tag: "Order exceeds maximum shortable quantity for Symbol ticker (requested short: quantity)".

Borrowing Costs

In live trading, your brokerage charges an interest fee when you borrow shares to short a security. In backtesting, we don't currently simulate short interest fees. You can evaluate the borrowing cost with the FeeRate and RebateRate methods.

To get valid borrowing rates, use the InteractiveBrokersShortableProvider.

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: