Supported Models

Interactive Brokers

Introduction

This page explains the 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.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.CASH)
self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, 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
MarketMarket Orders
LimitLimit Orders
Limit if touchedLimit if Touched Orders
Stop marketStop Orders
Stop limitStop-Limit Orders
Trailing stopTrailing Stop Orders
Market on openMarket-on-Open (MOO) Orders
Market on closeMarket-on-Close (MOC) Orders
Combo marketSpread Orders
Combo limitSpread Orders
Combo leg limitSpread Orders
Exercise OptionOptions Exercise

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

Order TypeUS EquityEquity OptionsForexFuturesFutures OptionsIndex OptionsCFD
Marketgreen checkgreen checkgreen checkgreen checkgreen checkgreen checkgreen check
Limitgreen checkgreen checkgreen checkgreen checkgreen checkgreen checkgreen check
Limit if touchedgreen checkgreen checkgreen checkgreen checkgreen checkgreen checkgreen check
Stop marketgreen checkgreen checkgreen checkgreen checkgreen checkgreen checkgreen check
Stopl imitgreen checkgreen checkgreen checkgreen checkgreen checkgreen checkgreen check
Trailing stopgreen checkgreen checkgreen checkgreen checkgreen checkgreen checkgreen check
Market on opengreen checkgreen checkgreen checkgreen check
Market on closegreen checkgreen check
Combo marketgreen checkgreen checkgreen check
Combo limitgreen checkgreen checkgreen check
Combo leg limitgreen checkgreen checkgreen check
Exercise Optiongreen 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 methods for FA accounts.

PropertyData TypeDescriptionDefault Value
TimeInForcetime_in_forceA TimeInForce instruction to apply to the order. The following instructions are supported:
  • DayDAY
  • GoodTilCanceledGOOD_TIL_CANCELED
  • GoodTilDategood_til_date
TimeInForce.GoodTilCanceledTimeInForce.GOOD_TIL_CANCELED
OutsideRegularTradingHoursoutside_regular_trading_hoursboolA 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.default_order_properties = InteractiveBrokersOrderProperties()
    self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED
    self.default_order_properties.outside_regular_trading_hours = False

def on_data(self, slice: Slice) -> None:
    # Use default order order properties
    self.limit_order(self._symbol, quantity, limit_price)
    
    # Override the default order properties
    order_properties = InteractiveBrokersOrderProperties()
    order_properties.time_in_force = TimeInForce.DAY
    order_properties.outside_regular_trading_hours = True
    self.limit_order(self._symbol, quantity, limit_price, order_properties=order_properties)

    order_properties.time_in_force = TimeInForce.good_til_date(datetime(year, month, day))
    self.limit_order(self._symbol, quantity, limit_price, order_properties=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 NullSlippageModel.

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, 10x leverage for CFDs, 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 default settlement rules
OptionCashDelayedSettlementModel with the default settlement rules
FutureAnyFutureSettlementModel

For all other cases, the InteractiveBrokersBrokerageModel uses the ImmediateSettlementModel.

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

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

// For remaining cases:
security.SetSettlementModel(new ImmediateSettlementModel());
# For US Equities with a cash account:
security.set_settlement_model(DelayedSettlementModel(Equity.DEFAULT_SETTLEMENT_DAYS, Equity.DEFAULT_SETTLEMENT_TIME))

# For Equity Options with a cash account:
security.set_settlement_model(DelayedSettlementModel(Option.DEFAULT_SETTLEMENT_DAYS, Option.DEFAULT_SETTLEMENT_TIME))

# For remaining cases:
security.set_settlement_model(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
CfdInteractiveBrokers

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.default_order_properties = InteractiveBrokersOrderProperties()
self.default_order_properties.fa_group = "TestGroupEQ"
self.default_order_properties.fa_method = "EqualQuantity"
self.default_order_properties.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.

LEAN supports several allocation methods for FA group orders. If you intend to use the same group allocation method for every order, set the DefaultOrderPropertiesdefault_order_properties of your algorithm, which sets the order properties for all of your orders.

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);  
}
def initialize(self) -> None:
    # Set the default order properties
    self.default_order_properties = InteractiveBrokersOrderProperties()
    self.default_order_properties.fa_group = "TestGroupEQ"
    self.default_order_properties.fa_method = "EqualQuantity"
    self.default_order_properties.account = "DU123456"

def on_data(self, slice: Slice) -> None:
    # Use default order order properties
    LimitOrder(_symbol, quantity, limitPrice);

To adjust the order properties of an order, change the DefaultOrderPropertiesdefault_order_properties or pass an order properties object to the order method. The following sections explain the FA group allocation methods.

Equal Quantity

This group allocation method distributes shares equally between all accounts in the group. When you use this method, you need to specify an order quantity.

For example, say your Account Group includes four accounts and you place an order to buy 400 shares of a stock. In this case, each account receives 100 shares. If your Account Group includes six accounts, each account receives 66 shares, and then 1 share is allocated to each account until all are distributed. After you submit the order, your algorithm receives order events to track the order progress. When all the shares are bought, the order status is OrderStatus.FilledOrderStatus.FILLED. If one of the accounts in the group can't afford 10 of the shares it needs to buy, 10 shares are cancelled and you'll only end up buying 390 shares in total.

LimitOrder(
    _symbol, quantity, limitPrice, 
    orderProperties: new InteractiveBrokersOrderProperties
    { 
        FaMethod = "EqualQuantity" 
    }
);
order_properties = InteractiveBrokersOrderProperties()
order_properties.fa_method = "EqualQuantity"
self.limit_order(self._symbol, quantity, limit_price, order_properties=order_properties)

Net Liquidation Value

This group allocation method 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. When you use this method, you need to specify an order quantity.

For example, say your account group includes three accounts, A, B and C with Net Liquidation values of $25,000, $50,000 and $100,000, respectively. In this case, the system calculates a ratio of 1:2:4. If you place an order for 700 shares of a stock, it allocates 100 shares to Client A, 200 shares to Client B, and 400 shares to Client C.

LimitOrder(_symbol, quantity, limitPrice, 
    orderProperties: new InteractiveBrokersOrderProperties
    { 
        FaMethod = "NetLiq" 
    }
);
order_properties = InteractiveBrokersOrderProperties()
order_properties.fa_method = "NetLiq"
self.limit_order(self._symbol, quantity, limit_price, order_properties=order_properties)

Available Equity

This group allocation method 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. When you use this method, you need to specify an order quantity.

For example, say your account group includes three accounts, A, B and C with available equity of $25,000, $50,000 and $100,000, respectively. In this case, the system calculates a ratio of 1:2:4. If you place an order for 700 shares of a stock, it allocates 100 shares to Client A, 200 shares to Client B, and 400 shares to Client C.

LimitOrder(_symbol, quantity, limitPrice, 
    orderProperties: new InteractiveBrokersOrderProperties
    { 
        FaMethod = "AvailableEquity" 
    }
);
order_properties = InteractiveBrokersOrderProperties()
order_properties.fa_method = "AvailableEquity"
self.limit_order(self._symbol, quantity, limit_price, order_properties=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: