Margin Interest Rate
Key Concepts
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.
// In Initialize var security = AddEquity("SPY"); security.MarginInterestRateModel = MarginInterestRateModel.Null;
# In Initialize security = self.AddEquity("SPY") security.SetMarginInterestRateModel(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 SetSecurityInitializer
before you create the subscriptions.
// 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.MarginInterestRateModel = MarginInterestRateModel.Null; } }
# 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.SetMarginInterestRateModel(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 ApplyMarginInterestRate
method, which applies margin interest payments to the portfolio.
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 MyMarginInterestRateModel: def ApplyMarginInterestRate(self, marginInterestRateParameters: MarginInterestRateParameters) -> None: holdings = marginInterestRateParameters.Security.Holdings position_value = holdings.GetQuantityValue(holdings.Quantity) position_value.Cash.AddAmount(-1)
The ApplyMarginInterestRate
method is automatically called at the top of each hour.