Margin Interest Rate

Key Concepts

Introduction

Margin interest is a cost associated with trading on margin. Margin interest rate models model margin interest cash flows by directly adding or removing cash from your portfolio.

Set Models

The brokerage model of your algorithm automatically sets the margin interest rate model for each security, but you can override it. To manually set the margin interest rate model of a security, assign a model to the MarginInterestRateModel property of the Security object.

public override void Initialize()
{
    var security = AddEquity("SPY");
    // Null margin interest rate model is non-realistic, you should set it according to your broker information
    security.MarginInterestRateModel = MarginInterestRateModel.Null;
}
def initialize(self) -> None:
    security = self.add_equity("SPY")
    # Null margin interest rate model is non-realistic, you should set it according to your broker information
    security.set_margin_interest_rate_model(MarginInterestRateModel.NULL)

You can also set the margin interest rate 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.MarginInterestRateModel = MarginInterestRateModel.Null;    }
}
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_margin_interest_rate_model(MarginInterestRateModel.NULL)

To view all the pre-built margin interest rate models, see Supported Models.

Default Behavior

The brokerage model of your algorithm automatically sets the margin interest rate model of each security. The default brokerage model is the DefaultBrokerageModel, which sets the NullMarginInterestRateModel.

Model Structure

Margin interest rate models should implement the IMarginInterestRateModel interface. Extensions of the IMarginInterestRateModel interface must implement the ApplyMarginInterestRateapply_margin_interest_rate method, which applies margin interest payments to the portfolio.

public class CustomMarginInterestRateModelExampleAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        var security = AddEquity("SPY");
        // You should set it according to your broker information for the least actual-expected discrepancies
        security.SetMarginInterestRateModel(new MyMarginInterestRateModel());
    }
}

// Define the custom margin interest rate model
public class MyMarginInterestRateModel : IMarginInterestRateModel 
{
    public void ApplyMarginInterestRate(MarginInterestRateParameters marginInterestRateParameters) 
    {
        var holdings = marginInterestRateParameters.Security.Holdings;
        var positionValue = holdings.GetQuantityValue(holdings.Quantity);
        positionValue.Cash.AddAmount(-1);
    }
}
class CustomMarginInterestRateModelExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        security = self.add_equity("SPY")
        # You should set it according to your broker information for the least actual-expected discrepancies
        security.set_margin_interest_rate_model(MyMarginInterestRateModel())

# Define the custom margin interest rate model
class MyMarginInterestRateModel:

    def apply_margin_interest_rate(self, margin_interest_rate_parameters: MarginInterestRateParameters) -> None:
        holdings = margin_interest_rate_parameters.security.holdings
        position_value = holdings.get_quantity_value(holdings.quantity)
        position_value.cash.add_amount(-1)

For a full example algorithm, see this backtestthis backtest.

The ApplyMarginInterestRateapply_margin_interest_rate method is automatically called at the top of each hour.

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: