Reality Modeling

Buying Power

Introduction

Buying power models (also known as margin models) control how much buying power or leverage your portfolio has to make trades. When you place an order, LEAN uses the buying power model to determine whether the order should be submitted so you avoid placing orders that would be rejected by the brokerage. Buying power calculations can be very complex and depend on many factors, including the brokerage or even the time of day. For example, the PatternDayTradingMarginModel lets you have 4x leverage during regular trading hours and 2x leverage during pre-market and post-market trading hours.

What is Buying Power?

Buying power is the total amount of money you can spend in your brokerage account. It depends on the security type and account type. On one hand, Option and Future contracts are leveraged securities with specific buying power rules. On the other hand, the buying power of cash accounts is just the cash in the account while the buying power of margin accounts is leveraged by the brokerage credit.

What is Margin?

Margin is a credit loan from your brokerage you receive after you deposit collateral into your margin account. You need margin to place short-biased trades and you can use margin to increase your buying power, but you incur interest fees. Margin is the dollar value of the loan that the brokerage gives you. A margin requirement of 25% means that to purchase $10,000 worth of securities, you need at least $2,500 worth of collateral in your brokerage account to open the trade and you can borrow the rest on margin. Maintenance margin is the minimum equity (equity = total portfolio value - borrowed funds) you must have in your brokerage account to stay in your positions. If the value of your portfolio falls below the maintenance margin, you receive a margin call. If you receive a margin call, you either need to add more capital to your brokerage account or the brokerage will liquidate some of your holdings to reduce your exposure and their risk.

What is Leverage?

Leverage is using borrowed money to increase your buying power. Leverage has an inverse relationship with your margin requirement and maintenance margin. If you have a margin requirement of 50%, you can use up to 1 / 50% = 2 leverage. Trading with leverage can be risky. It can boost your returns on profitable trades but can make your losing trades more expensive. If you have $10,000 in your brokerage margin account and purchase $20,000 worth of securities, you are trading with 2x leverage. If the value of the securities in your portfolio drops by 50% when you have a 2x leverage position, you lose all of your equity.

Set Models

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

// In Initialize
var security = AddEquity("SPY");
security.SetBuyingPowerModel(new SecurityMarginModel(3m));
# In Initialize
security = self.AddEquity("SPY")
security.SetFeeModel(SecurityMarginModel(3))

You can also set the buying power 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.SetBuyingPowerModel(new SecurityMarginModel(3m));
}
# In Initialize
self.SetSecurityInitializer(self.CustomSecurityInitializer)
AddEquity("SPY")

def CustomSecurityInitializer(self, security: Security) -> None:
    security.SetBuyingPowerModel(SecurityMarginModel(3))

Default Behavior

The brokerage model of your algorithm automatically sets the buying power model of each security. The default brokerage model is the DefaultBrokerageModel, which sets the buying power model based on the asset class of the security. The following table shows the default buying power model of each asset class:

Asset ClassModel
Equity OptionsOptionMarginModel
FuturesFutureMarginModel
Future OptionsFuturesOptionsMarginModel
Index OptionsOptionMarginModel
OtherCashBuyingPowerModel for cash accounts or SecurityMarginModel for margin accounts.

Model Structure

Buying power models should extend the BuyingPowerModel class. Extensions of the BuyingPowerModel class should implement the following methods:

class MyBuyingPowerModel : BuyingPowerModel
{
    public MyBuyingPowerModel(
        decimal leverage = 2m,
        decimal requiredFreeBuyingPowerPercent = 0m)
        : base(leverage, requiredFreeBuyingPowerPercent)
    {
    }

    public override decimal GetLeverage(Security security)
    {
       return base.GetLeverage(security);
    }

    public override void SetLeverage(Security security, decimal leverage)
    {
        base.SetLeverage(security, leverage);
    }

    public override InitialMargin GetInitialMarginRequiredForOrder(
        InitialMarginRequiredForOrderParameters parameters)
    {
        return base.GetInitialMarginRequiredForOrder(parameters);
    }

    public override MaintenanceMargin GetMaintenanceMargin(
        MaintenanceMarginParameters parameters)
    {
        return base.GetMaintenanceMargin(parameters);
    }

    protected override decimal GetMarginRemaining(
        SecurityPortfolioManager portfolio,
        Security security,
        OrderDirection direction)
    {
        return base.GetMarginRemaining(portfolio, security, direction);
    }

    public override InitialMargin GetInitialMarginRequirement(
        InitialMarginParameters parameters)
    {
        return base.GetInitialMarginRequirement(parameters);
    }

    public override HasSufficientBuyingPowerForOrderResult HasSufficientBuyingPowerForOrder(HasSufficientBuyingPowerForOrderParameters parameters)
    {
        return base.HasSufficientBuyingPowerForOrder(parameters);
    }

    public override GetMaximumOrderQuantityResult GetMaximumOrderQuantityForDeltaBuyingPower(GetMaximumOrderQuantityForDeltaBuyingPowerParameters parameters)
    {
        return base.GetMaximumOrderQuantityForDeltaBuyingPower(parameters);
    }

    public override GetMaximumOrderQuantityResult GetMaximumOrderQuantityForTargetBuyingPower(GetMaximumOrderQuantityForTargetBuyingPowerParameters parameters)
    {
        return base.GetMaximumOrderQuantityForTargetBuyingPower(parameters);
    }

    public override ReservedBuyingPowerForPosition GetReservedBuyingPowerForPosition(
        ReservedBuyingPowerForPositionParameters parameters)
    {
        return base.GetReservedBuyingPowerForPosition(parameters);
    }

    public override BuyingPower GetBuyingPower(BuyingPowerParameters parameters)
    {
        return base.GetBuyingPower(parameters);
    }
}
class MyBuyingPowerModel(BuyingPowerModel):
    def __init__(self,
         leverage = 2: float,
         requiredFreeBuyingPowerPercent = 0: float):
        super().__init__(leverage, requiredFreeBuyingPowerPercent)

    def GetLeverage(self, security: Security) -> float: 
        return super().GetLeverage(security)

    def SetLeverage(self, security: Security, leverage: float) -> None: 
        super().SetLeverage(security, leverage)

    def GetInitialMarginRequiredForOrder(self,
         parameters: InitialMarginRequiredForOrderParameters) -> InitialMargin:
        return super().GetInitialMarginRequiredForOrder(parameters)

    def GetMaintenanceMargin(self,
         parameters: MaintenanceMarginParameters) -> MaintenanceMargin: 
        return super().GetMaintenanceMargin(parameters)

    def GetMarginRemaining(self,
         portfolio: SecurityPortfolioManager,
         security: Security,
         direction: OrderDirection) -> float: 
        return super().GetMarginRemaining(portfolio, security, direction)

    def GetInitialMarginRequirement(self,
         parameters: InitialMarginParameters) -> InitialMargin:
        return super().GetInitialMarginRequirement(parameters)

    def HasSufficientBuyingPowerForOrder(self, 
         parameters: HasSufficientBuyingPowerForOrderParameters
        ) -> HasSufficientBuyingPowerForOrderResult: 
        return super().HasSufficientBuyingPowerForOrder(parameters)

    def GetMaximumOrderQuantityForDeltaBuyingPower(self, 
         parameters: GetMaximumOrderQuantityForDeltaBuyingPowerParameters
        ) -> GetMaximumOrderQuantityResult:
        return super().GetMaximumOrderQuantityForDeltaBuyingPower(parameters)

    def GetMaximumOrderQuantityForTargetBuyingPower(self, 
         parameters: GetMaximumOrderQuantityForTargetBuyingPowerParameters
        ) -> GetMaximumOrderQuantityResult:
        return super().GetMaximumOrderQuantityForTargetBuyingPower(parameters)

    def GetReservedBuyingPowerForPosition(self, 
         parameters: ReservedBuyingPowerForPositionParameters
        ) -> ReservedBuyingPowerForPosition:
        return super().GetReservedBuyingPowerForPosition(parameters)

    def GetBuyingPower(self,
         parameters: BuyingPowerParameters) -> BuyingPower:
        return super().GetBuyingPower(parameters)

Set Asset Leverage

The buying power model sets the leverage for each security in your algorithm, but you can override its leverage settings after the buying power model is set.

To set the leverage when you create a security subscription, pass in a leverage argument.

// In Initialize
AddEquity("SPY", leverage: 3);
# In Initialize
AddEquity("SPY", leverage=3)

You can also set the asset leverage in a security initializer. In order to set the leverage of securities in the security initializer, call SetSecurityInitializer before you create security subscriptions and before you call SetBrokerageModel. If you pass in a leverage argument when you create the security subscription, the leverage argument takes precedence over the SetLeverage call in the security initializer.

// In Initialize
SetSecurityInitializer(CustomSecurityInitializer);
AddEquity("SPY");

private void CustomSecurityInitializer(Security security)
{
    security.SetLeverage(3);
}
# In Initialize
self.SetSecurityInitializer(self.CustomSecurityInitializer)
AddEquity("SPY")

def CustomSecurityInitializer(self, security: Security) -> None:
    security.SetLeverage(3)

To set the leverage for all securities in a universe, set the UniverseSettings.Leverage property.

// In Initialize
UniverseSettings.Leverage = 3;
# In Initialize
self.UniverseSettings.Leverage = 3

In live trading, LEAN doesn't ignore the leverage you set. However, if you set a different leverage from what your brokerage provides, it creates a mismatch between the buying power in your algorithm and the buying power the brokerage gives you. In this case, orders can pass the validations in LEAN but your brokerage may reject them.

PDT Rule

If all of the following statements are true, you are classified as a pattern day trader:

  • You reside in the United States.
  • You trade in a margin account.
  • You execute 4+ intraday US Equity trades within 5 business days.
  • Your intraday US Equity trades represent more than 6% of your total trades.

Pattern day traders must maintain a minimum equity of $25,000 in their margin account to continue trading. For more information about pattern day trading, see Am I a Pattern Day Trader? on the FINRA website.

The PatternDayTradingMarginModel doesn't enforce minimum equity rules and doesn't limit your trades, but it adjusts your available leverage based on the market state. During regular market hours, you can use up to 4x leverage. During extended market hours, you can use up to 2x leverage.

security.MarginModel = new PatternDayTradingMarginModel();
security.MarginModel = PatternDayTradingMarginModel()

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: