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 AllShortableSymbols and ShortableQuantity methods. The AllShortableSymbols method receives the local time of the algorithm and returns a dictionary that represents the shortable quantity for each Symbol in the algorithm. The ShortableQuantity method receives a Symbol and the local time of the algorithm and 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 Dictionary<Symbol, long> AllShortableSymbols(DateTime localTime)
    {
        return new Dictionary<Symbol, long>() {
            { QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA), 0 },
            { QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA), 10 }
        };
    }

    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 AllShortableSymbols(self, localTime: datetime) -> Dict[Symbol,float]:
        return {
            Symbol.Create("SPY", SecurityType.Equity, Market.USA): 0,
            Symbol.Create("AAPL", SecurityType.Equity, Market.USA): 10
        }
    
    def ShortableQuantity(self, symbol: Symbol, localTime: datetime) -> float:
        return 10000

For a full example algorithm, see this backtestthis backtest.

Get Short Availability

To check if a security has shares available to short, call the Shortable method.

// Check if there are any shares available to short a security
var isShortable = Shortable(symbol);

// Check if there are a certain quantity of shares available
var isShortableQuantity = Shortable(symbol, quantity);
# Check if there are any shares available to short a security
is_shortable = self.Shortable(symbol)

# Check if there are a certain quantity of shares available
is_shortable_quantity = self.Shortable(symbol, quantity)

To check how many shares are available for a security to short, call the ShortableQuantity method.

var quantity = ShortableQuantity(symbol);
quantity = self.ShortableQuantity(symbol)

To get all the symbols that are shortable and their respective shortable quantities, call the AllShortableSymbols method.

var quantityBySymbol = AllShortableSymbols();
quantity_by_symbol = self.AllShortableSymbols()

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. Subscribe to GitHub Issue #4563 to track the feature progress.

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: