Skip to main content

Interactive Brokers TWS API Trade Connector

Features

custom-ibtwsConnector Key
Instruments
FXEQUITYFUTUREOPTION
Request Types
SUBMITCANCELREPLACE
Order Types
MARKETLIMITSTOPSTOP_LIMITMARKET_ON_CLOSELIMIT_ON_CLOSEPEG_TO_MARKETPEG_TO_MIDPOINTPEG_TO_PRIMARY
Time In Force
DAYGTCAT_THE_OPENINGIOCFOKGOOD_TILL_CROSSINGGTD
Order ID Mapper
CUSTOM
Order Status Request
Cancel On Disconnect
Commission
Post Only
Fast Cancel
Fast Replace

Notes

TWS API Documentation

TWS Configuration

note

The connector requests open orders on (re)connection. Disable the following option in TWS:
API - Settings / Download open orders on connection

Order Types

See more about order types supported by TWS API.

The IB OrderType is resolved according to the mapping below.
The specific IB OrderType can be overridden via 40 order attribute.

Order TypeIB OrderType
MARKETMKT
MARKET_ON_CLOSEMOC
LIMITLMT
LIMIT_ON_CLOSELOC
STOPSTP
STOP_LIMITSTP_LMT
PEG_TO_MARKETPEG_MKT
PEG_TO_PRIMARYPEG_PRIM
PEG_TO_MIDPOINTPEG_MID

Security Types

The IB SecType is resolved according to the mapping below.
The specific IB SecType can be either set via <attributeKey>SecurityType column in Security Metadata or passed directly in 167 order attribute.

Instrument TypeIB SecType
EQUITY,ETFSTK
OPTIONOPT
FUTUREFUT
FXCASH
INDEXIND
BONDBOND
CFDCFD
SYNTHETICBAG

Setting IB order property via attributes

Custom IB order's properties can be passed via StrategyParametersGrp.

TagTag NameReq?Description
958StrategyParameterNameYRequired. Name of parameter. Format: <Prefix>.<setterName>.
959StrategyParameterTypeNOptional. Allowed values: string,boolean,int,long,double.
960StrategyParameterValueYRequired. Value of the parameter.

Supported prefixes:

Below is an example for IB TWAP order attributes:

KeyValue
958Order.algoStrategy
960Twap
958AlgoParams.strategyType
960Marketable
958AlgoParams.startTime
96009:00:00 US/Eastern
958AlgoParams.endTime
96016:00:00 US/Eastern
958AlgoParams.allowPastEndTime
9601

Symbology

IB Contracts

Securities Columns

Column (prefixed with <attributeKey>)Description
SymbolThe underlying’s asset symbol.
RootSymbolThe root symbol. Optional, Future.RootSymbol / Option.Underlying will be used if omitted.
ExpiryThe last trading day or contract month (for Options and Futures). The format YYYYMM will be interpreted as Contract Month whereas YYYYMMDD will be interpreted as Last Trading Day. Optional, Future.ExpirationDate / Option.ExpirationDate will be used if omitted.
ExchangeContract exchange.
SecurityTypeThe IB specific security’s type. Optional, it will be resolved form Instrument Type if omitted.
PrimaryExchangeThe contract’s primary exchange. Optional. For smart routed contracts, used to define contract in case of ambiguity.
TradingClassThe trading class name for this contract. Optional.
ContractMultiplierThe instrument’s multiplier (i.e. options, futures). Optional.
UseMlegPriceMultiplierIf enabled - trade price for the leg will be adjusted using parent price multiplier. Optional. MLEG instruments only.

Configuration

Connector settings

ParameterDefault ValueDescription
hostlocalhostTWS or IB Gateway host.
port7496TWS or IB Gateway port.
clientId1Client ID
accountDefault account.
useAccountfalseThe flag to define whether to use order account in preference of default account.
useExchangefalseDefines whether or not to use Exchange from order request.
reconnectInterval15sReconnection interval. The first time after connection lost connector tries to reconnect immediately.
heartbeatInterval10sThe interval between client heartbeats.
➡️ Contract Details Rate Limits
contractRateLimit.maxRequestCount45Rate limit threshold for contract details requests.
contractRateLimit.rateLimitInterval1sInterval for contract details request rate limit.

Supported order attributes

KeyDescription
1, 7001The account the trade will be allocated to.
40, 7040The order’s type. Allowed values: TRAIL, TRAIL_LIMIT, TRAIL_LIT etc.
47, 7047Rule80A. Allowed values: Individual, Agency, AgentOtherMember, IndividualPTIA, AgencyPTIA, AgentOtherMemberPTIA, IndividualPT, AgencyPT, AgentOtherMemberPT.
59, 7059The time in force. Allowed values: DAY, GTC, OPG, IOC, GTD, GTT, AUC, FOK, GTX, DTC, Minutes.
77, 7077OpenClose. For institutional customers only. Valid values are O (open) and C (close). Available for institutional clients to determine if this order is to open or close a position. When Action = BUY and OpenClose = O this will open a new position. When Action = BUY and OpenClose = C this will close and existing short position.
100, 7100The destination exchange.
207, 7207The contract’s primary exchange. For smart routed contracts, used to define contract in case of ambiguity.Should be defined as native exchange of contract. For exchanges which contain a period in name, will only be part of exchange name prior to period, i.e. ENEXT for ENEXT.BE.
167, 7167The security’s type. Allowed values: STK, OPT, FUT, CONTFUT, CASH, BOND, CFD, FOP, WAR, IOPT, FWD, BAG, IND, BILL, FUND, FIXED, SLB, NEWS, CMDTY, BSK, ICU, ICS, CRYPTO.
22, 7022Security’s identifier. Allowed values: CUSIP, SEDOL, ISIN, RIC.
204, 7204Origin. The order’s origin. Same as TWS “Origin” column. Identifies the type of customer from which the order originated. Valid values: 0 – Customer, 1 – Firm.
231, 7231The instrument’s multiplier (i.e. options, futures).
439, 7439ExemptCode. Only available with IB Execution-Only accounts with applicable securities. Mark order as exempt from short sale uptick rule.
6058TradingClass. The trading class name for this contract. Available in TWS contract description window as well. For example, GBL Dec ’13 future’s trading class is FGBL.
➡️ Algo ParamsOrder Parameters - Repeating Group
958Parameter name. Format: &lt;Prefix&gt;.&lt;setterName&gt;. Allowed prefix: Order, AlgoParams.
959Parameter type. Allowed values: string,boolean,int,long,double.
960Parameter value.

Config sample

connectors {
IBTWS : ${template.connector.ibtws} {
settings {
host = "localhost"
port = 7497

clientId = 1
}
}
}

Dev Documentation