Settlement

Key Concepts

Introduction

After you trade an asset, the brokerage needs to settle the funds in your account. The most common type of settlement is immediate, where the funds are immediately available for trading after the transaction. In some cases, you may have delayed settlement, where you sell an asset and need to wait a few days to spend the cash you receive from the sale. A settlement model simulates these settlement rules.

Set Models

The brokerage model of your algorithm automatically sets the settlement model for each security, but you can override it. To manually set the settlement model of a security, set the SettlementModel property on the Security object.

// In Initialize
var security = AddEquity("SPY");
// Set a delayed settlement model that settles 7 days after the trade at 8 AM
security.SettlementModel = new DelayedSettlementModel(7, TimeSpan.FromHours(8));
# In Initialize
security = self.AddEquity("SPY")
# Set a delayed settlement model that settles 7 days after the trade at 8 AM
security.SettlementModel = DelayedSettlementModel(7, timedelta(hours=8))

You can also set the settlement model 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.SettlementModel = new DelayedSettlementModel(7, TimeSpan.FromHours(8));
}
# In Initialize
self.SetSecurityInitializer(self.CustomSecurityInitializer)
self.AddEquity("SPY")

def CustomSecurityInitializer(self, security: Security) -> None:
    security.SettlementModel = DelayedSettlementModel(7, timedelta(hours=8))

To view all the pre-built settlement models, see Supported Models.

Default Behavior

The brokerage model of your algorithm automatically sets the settlement model for each security. The default brokerage model is the DefaultBrokerageModel, which sets the ImmediateSettlementModel in most cases. If you trade US Equities or Equity Options with a cash account, it sets the DelayedSettlementModel.

The default delayed settlement rule for US Equity trades is T+3 at 8 AM Eastern Time (ET). For example, if you sell on Monday, the trade settles on Thursday at 8 AM. The default delayed settlement rule for Future and Option contracts is T+1 at 8 AM.

Model Structure

Settlement models must extend the ISettlementModel interface. Extensions of the ISettlementModel interface must implement the ApplyFunds method, which applies the cash settlement rules.

public interface ISettlementModel
{
    void ApplyFunds(
        SecurityPortfolioManager portfolio,
        Security security,
        DateTime applicationTimeUtc,
        string currency,
        decimal amount);
}

LEAN doesn't currently support custom option exercise model in Python.

The following table describes the arguments of the ApplyFunds method:

ArgumentData TypeDescription
portfolioSecurityPortfolioManagerThe algorithm's portfolio
securitySecurityThe fill's security
applicationTimeUtcDateTimeThe fill time in Coordinated Universal Time (UTC)
currencystringThe currency symbol
amountdecimalThe amount of cash to apply

You likely don't need to create a custom settlement model because the supported models already implement immediate and delayed settlement rules.

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: