Supported Models

Interactive Brokers

Introduction

This page explains InteractiveBrokersBrokerageModel, including the asset classes it supports, its default security-level models, and it's default markets.

SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash);
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin);
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)

To view the implementation of this model, see the LEAN GitHub repository.

Asset Classes

The InteractiveBrokersBrokerageModel supports the following asset classes:

Orders

The InteractiveBrokersBrokerageModel supports several order types, order properties, and order updates.

Order Types

The following table describes the order types that the InteractiveBrokersBrokerageModel supports: supports. For specific details about each order type, refer to the IB documentation.

Order TypeIB Documentation Page
MarketOrderMarket Orders
LimitOrderLimit Orders
LimitIfTouchedOrderLimit if Touched Orders
StopMarketOrderStop Orders
StopLimitOrderStop-Limit Orders
TrailingStopOrderTrailing Stop Orders
MarketOnOpenOrderMarket-on-Open (MOO) Orders
MarketOnCloseOrderMarket-on-Close (MOC) Orders
ComboMarketOrderSpread Orders
ComboLimitOrderSpread Orders
ComboLegLimitOrderSpread Orders
ExerciseOptionOptions Exercise

The following table describes the available order types for each asset class that the InteractiveBrokersBrokerageModel supports:

Order TypeUS EquityEquity OptionsForexFuturesFutures OptionsIndex Options
MarketOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
LimitOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
LimitIfTouchedOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
StopMarketOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
StopLimitOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
TrailingStopOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
MarketOnOpenOrdergreen checkgreen checkgreen checkgreen check
MarketOnCloseOrdergreen checkgreen checkgreen checkgreen checkgreen checkgreen check
ComboMarketOrdergreen checkgreen checkgreen check
ComboLimitOrdergreen checkgreen checkgreen check
ComboLegLimitOrdergreen checkgreen checkgreen check
ExerciseOptiongreen check
Not supported for cash-settled Options
green check

Order Properties

The InteractiveBrokersBrokerageModel supports custom order properties. The following table describes the members of the InteractiveBrokersOrderProperties object that you can set to customize order execution. The table does not include the preceding methods for FA accounts.

PropertyDescription
TimeInForceA TimeInForce instruction to apply to the order. The following instructions are supported:
  • Day
  • GoodTilCanceled
  • GoodTilDate
OutsideRegularTradingHoursA flag to signal that the order may be triggered and filled outside of regular trading hours.
public override void Initialize()
{
    // Set the default order properties
    DefaultOrderProperties = new InteractiveBrokersOrderProperties
    {
        TimeInForce = TimeInForce.GoodTilCanceled,
        OutsideRegularTradingHours = false
    };
}

public override void OnData(Slice slice)
{
    // Use default order order properties
    LimitOrder(_symbol, quantity, limitPrice);
    
    // Override the default order properties
    LimitOrder(_symbol, quantity, limitPrice, 
               orderProperties: new InteractiveBrokersOrderProperties
               { 
                   TimeInForce = TimeInForce.Day,
                   OutsideRegularTradingHours = false
               });
    LimitOrder(_symbol, quantity, limitPrice, 
               orderProperties: new InteractiveBrokersOrderProperties
               { 
                   TimeInForce = TimeInForce.GoodTilDate(new DateTime(year, month, day)),
                   OutsideRegularTradingHours = true
               });
}
def Initialize(self) -> None:
    # Set the default order properties
    self.DefaultOrderProperties = InteractiveBrokersOrderProperties()
    self.DefaultOrderProperties.TimeInForce = TimeInForce.GoodTilCanceled
    self.DefaultOrderProperties.OutsideRegularTradingHours = False

def OnData(self, slice: Slice) -> None:
    # Use default order order properties
    self.LimitOrder(self.symbol, quantity, limit_price)
    
    # Override the default order properties
    order_properties = InteractiveBrokersOrderProperties()
    order_properties.TimeInForce = TimeInForce.Day
    order_properties.OutsideRegularTradingHours = True
    self.LimitOrder(self.symbol, quantity, limit_price, orderProperties=order_properties)

    order_properties.TimeInForce = TimeInForce.GoodTilDate(datetime(year, month, day))
    self.LimitOrder(self.symbol, quantity, limit_price, orderProperties=order_properties)

Updates

The InteractiveBrokersBrokerageModel supports order updates.

Financial Advisor Group Orders

To place FA group orders, see Financial Advisors.

Fractional Trading

The InteractiveBrokersBrokerageModel doesn't support fractional trading.

Handling Splits

If you're using raw data normalization and you have active orders with a limit, stop, or trigger price in the market for a US Equity when a stock split occurs, the following properties of your orders automatically adjust to reflect the stock split:

  • Quantity
  • Limit price
  • Stop price
  • Trigger price

Order Size Limits

The InteractiveBrokersBrokerageModel enforces the Spot Currency Minimum/Maximum Order Sizes from the IB website.

Fills

The following table shows the fill model that the InteractiveBrokersBrokerageModel uses for each SecurityType:

SecurityTypeFill Model
EquityEquityFillModel
FutureFutureFillModel
FutureOptionFutureOptionFillModel
Remaining SecurityType valuesImmediateFillModel

Slippage

The InteractiveBrokersBrokerageModel uses the ConstantSlippageModel with zero slippage.

security.SetSlippageModel(new ConstantSlippageModel(0));
security.SetSlippageModel(ConstantSlippageModel(0))

Fees

The InteractiveBrokersBrokerageModel uses the InteractiveBrokersFeeModel with the default argument values. We model current Interactive Brokers fees on all assets.

Buying Power

The InteractiveBrokersBrokerageModel 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
CryptoCashBuyingPowerModel for cash accounts or SecurityMarginModel for margin accounts
CryptoFutureCryptoFutureMarginModel
ForexCashBuyingPowerModel for cash accounts or SecurityMarginModel for margin accounts
OtherSecurityMarginModel

If you have a margin account, the InteractiveBrokersBrokerageModel allows 2x leverage for Equities, 50x leverage for Forex, and 1x leverage for the remaining asset classes.

Settlement

The InteractiveBrokersBrokerageModel uses the ImmediateSettlementModel in most cases. If you trade US Equities or Equity Options with a cash account, it uses the DelayedSettlementModel with the default settlement rules.

The following table shows which settlement model the InteractiveBrokersBrokerageModel uses based on the security type and your account type:

Security TypeAccount TypeSettlement Model
EquityCashDelayedSettlementModel with the Warning: Undefined variable $includeLinks in /tmp/docs/c3aac5f8ee7065002e92aeecc79c79c6d945b376/Resources/reality-modeling/default-settlement-models.php on line 13 default settlement rules
OptionCashDelayedSettlementModel with the Warning: Undefined variable $includeLinks in /tmp/docs/c3aac5f8ee7065002e92aeecc79c79c6d945b376/Resources/reality-modeling/default-settlement-models.php on line 18 default settlement rules
FutureAnyFutureSettlementModel

For all other cases, the InteractiveBrokersBrokerageModel uses the ImmediateSettlementModel.

// For US Equities with a cash account:
security.SettlementModel = new DelayedSettlementModel(Equity.DefaultSettlementDays, Equity.DefaultSettlementTime);

// For Equity Options with a cash account:
security.SettlementModel = new DelayedSettlementModel(Option.DefaultSettlementDays, Option.DefaultSettlementTime);

// For remaining cases:
security.SettlementModel = new ImmediateSettlementModel();
# For US Equities with a cash account:
security.SettlementModel = DelayedSettlementModel(Equity.DefaultSettlementDays, Equity.DefaultSettlementTime)

# For Equity Options with a cash account:
security.SettlementModel = DelayedSettlementModel(Option.DefaultSettlementDays, Option.DefaultSettlementTime)

# For remaining cases:
security.SettlementModel = ImmediateSettlementModel()

Interactive Brokers doesn't provide information on which assets aren't settled, so we assume each live trading session starts with its cash fully settled.

Margin Interest Rate

The InteractiveBrokersBrokerageModel uses the NullMarginInterestRateModel.

Default Markets

The following table describes the default markets of each SecurityType for the InteractiveBrokersBrokerageModel:

SecurityTypeMarket
EquityUSA
OptionUSA
FutureCME
FutureOptionCME
IndexUSA
IndexOptionUSA
ForexOanda

Financial Advisors

IB supports FA accounts for Trading Firm and Institution organizations. FA accounts enable certified professionals to use a single trading algorithm to manage several client accounts.

To place trades using a subset of client accounts, create Account Groups in Trader Workstation and then define the InteractiveBrokersOrderProperties when you create orders.

DefaultOrderProperties = new InteractiveBrokersOrderProperties
{
    FaGroup = "TestGroupEQ",
    FaMethod = "EqualQuantity",
    Account = "DU123456"
};
self.DefaultOrderProperties = InteractiveBrokersOrderProperties()
self.DefaultOrderProperties.FaGroup = "TestGroupEQ"
self.DefaultOrderProperties.FaMethod = "EqualQuantity"
self.DefaultOrderProperties.Account = "DU123456"

SecurityHolding objects aggregate your positions across all the account groups. If you have two groups where group A has 10 shares of SPY and group B has -10 shares of SPY, then self.Portfolio["SPY"].QuantityPortfolio["SPY"].Quantity is zero.

The following table shows the supported allocation methods for FA group orders:

FaMethodDescription
"EqualQuantity"Distributes shares equally between all accounts in the group. If you use this method, specify an order quantity.
"NetLiq"Distributes shares based on the net liquidation value of each account. The system calculates ratios based on the net liquidation value in each account and allocates shares based on these ratios. If you use this method, specify an order quantity.
"AvailableEquity"Distributes shares based on the amount of available equity in each account. The system calculates ratios based on the available equity in each account and allocates shares based on these ratios. If you use this method, specify an order quantity.
"PctChange"Increases or decreases an already existing position. Positive percents increase positions and negative percents decrease positions. If you use this method, specify a percent instead of an order quantity.
public override void Initialize()
{
    // Set the default order properties
    DefaultOrderProperties = new InteractiveBrokersOrderProperties()
    {
        FaGroup = "TestGroupEQ",
        FaMethod = "EqualQuantity",
        Account = "DU123456"
    };
}

public override void OnData(Slice slice)
{
    // Use default order order properties
    LimitOrder(_symbol, quantity, limitPrice);

    // Override the default order properties
    // "NetLiq" requires a order size input
    LimitOrder(_symbol, quantity, limitPrice, 
               orderProperties: new InteractiveBrokersOrderProperties
               { 
                   FaMethod = "NetLiq" 
               });

    // "AvailableEquity" requires a order size input
    LimitOrder(_symbol, quantity, limitPrice, 
               orderProperties: new InteractiveBrokersOrderProperties
               { 
                   FaMethod = "AvailableEquity" 
               });
    
    // "PctChange" requires a percentage of portfolio input
    SetHoldings(_symbol, pctPortfolio,
                orderProperties: new InteractiveBrokersOrderProperties
                { 
                    FaMethod = "PctChange" 
                });
}
def Initialize(self) -> None:
    # Set the default order properties
    self.DefaultOrderProperties = InteractiveBrokersOrderProperties()
    self.DefaultOrderProperties.FaGroup = "TestGroupEQ"
    self.DefaultOrderProperties.FaMethod = "EqualQuantity"
    self.DefaultOrderProperties.Account = "DU123456"

def OnData(self, slice: Slice) -> None:
    # Override the default order properties
    # "NetLiq" requires a order size input
    order_properties = InteractiveBrokersOrderProperties()
    order_properties.FaMethod = "NetLiq"
    self.LimitOrder(self.symbol, quantity, limit_price, orderProperties=order_properties)

    # "AvailableEquity" requires a order size input
    order_properties.FaMethod = "AvailableEquity"
    self.LimitOrder(self.symbol, quantity, limit_price, orderProperties=order_properties)

    # "PctChange" requires a percentage of portfolio input
    order_properties.FaMethod = "PctChange"
    self.SetHoldings(self.symbol, pct_portfolio, orderProperties=order_properties)

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: