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.
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.
The following table describes the available order types for each asset class that the InteractiveBrokersBrokerageModel
supports:
Order Type | US Equity | Equity Options | Forex | Futures | Futures Options | Index Options | CFD |
---|---|---|---|---|---|---|---|
Market | |||||||
Limit | |||||||
Limit if touched | |||||||
Stop market | |||||||
Stopl imit | |||||||
Trailing stop | |||||||
Market on open | |||||||
Market on close | |||||||
Combo market | |||||||
Combo limit | |||||||
Combo leg limit | |||||||
Exercise Option | Not supported for cash-settled Options |
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.
Property | Data Type | Description | Default Value |
---|---|---|---|
TimeInForce time_in_force | A TimeInForce instruction to apply to the order. The following instructions are supported:
| TimeInForce.GoodTilCanceled TimeInForce.GOOD_TIL_CANCELED | |
OutsideRegularTradingHours outside_regular_trading_hours | bool | A 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
:
SecurityType | Fill Model |
---|---|
Equity | EquityFillModel |
Future | FutureFillModel |
FutureOption | FutureOptionFillModel |
Remaining SecurityType values | ImmediateFillModel |
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 Class | Model |
---|---|
Equity Options | OptionMarginModel |
Futures | FutureMarginModel |
Future Options | FuturesOptionsMarginModel |
Index Options | OptionMarginModel |
Crypto | CashBuyingPowerModel for cash accounts or SecurityMarginModel for margin accounts |
CryptoFuture | CryptoFutureMarginModel |
Forex | CashBuyingPowerModel for cash accounts or SecurityMarginModel for margin accounts |
Other | SecurityMarginModel |
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 Type | Account Type | Settlement Model |
---|---|---|
Equity | Cash | DelayedSettlementModel with the default settlement rules |
Option | Cash | DelayedSettlementModel with the default settlement rules |
Future | Any | FutureSettlementModel |
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.
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"].quantity
Portfolio["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 DefaultOrderProperties
default_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 DefaultOrderProperties
default_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.Filled
OrderStatus.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)