Interactive Brokers TWS API Trade Connector
Features
custom-ibtwsConnector Key✓
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 Type | IB OrderType |
|---|---|
MARKET | MKT |
MARKET_ON_CLOSE | MOC |
LIMIT | LMT |
LIMIT_ON_CLOSE | LOC |
STOP | STP |
STOP_LIMIT | STP_LMT |
PEG_TO_MARKET | PEG_MKT |
PEG_TO_PRIMARY | PEG_PRIM |
PEG_TO_MIDPOINT | PEG_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 Type | IB SecType |
|---|---|
EQUITY,ETF | STK |
OPTION | OPT |
FUTURE | FUT |
FX | CASH |
INDEX | IND |
BOND | BOND |
CFD | CFD |
SYNTHETIC | BAG |
Setting IB order property via attributes
Custom IB order's properties can be passed via StrategyParametersGrp.
| Tag | Tag Name | Req? | Description |
|---|---|---|---|
| 958 | StrategyParameterName | Y | Required. Name of parameter. Format: <Prefix>.<setterName>. |
| 959 | StrategyParameterType | N | Optional. Allowed values: string,boolean,int,long,double. |
| 960 | StrategyParameterValue | Y | Required. Value of the parameter. |
Supported prefixes:
Order- denotes property for IB order.AlgoParams- denotes item for IB Algorithm param list.
Below is an example for IB TWAP order attributes:
| Key | Value |
|---|---|
| 958 | Order.algoStrategy |
| 960 | Twap |
| 958 | AlgoParams.strategyType |
| 960 | Marketable |
| 958 | AlgoParams.startTime |
| 960 | 09:00:00 US/Eastern |
| 958 | AlgoParams.endTime |
| 960 | 16:00:00 US/Eastern |
| 958 | AlgoParams.allowPastEndTime |
| 960 | 1 |
Symbology
IB Contracts
Securities Columns
Column (prefixed with <attributeKey>) | Description |
|---|---|
Symbol | The underlying’s asset symbol. |
RootSymbol | The root symbol. Optional, Future.RootSymbol / Option.Underlying will be used if omitted. |
Expiry | The 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. |
Exchange | Contract exchange. |
SecurityType | The IB specific security’s type. Optional, it will be resolved form Instrument Type if omitted. |
PrimaryExchange | The contract’s primary exchange. Optional. For smart routed contracts, used to define contract in case of ambiguity. |
TradingClass | The trading class name for this contract. Optional. |
ContractMultiplier | The instrument’s multiplier (i.e. options, futures). Optional. |
UseMlegPriceMultiplier | If enabled - trade price for the leg will be adjusted using parent price multiplier. Optional. MLEG instruments only. |
Configuration
Connector settings
| Parameter | Default Value | Description |
|---|---|---|
| host | localhost | TWS or IB Gateway host. |
| port | 7496 | TWS or IB Gateway port. |
| clientId | 1 | Client ID |
| account | Default account. | |
| useAccount | false | The flag to define whether to use order account in preference of default account. |
| useExchange | false | Defines whether or not to use Exchange from order request. |
| reconnectInterval | 15s | Reconnection interval. The first time after connection lost connector tries to reconnect immediately. |
| heartbeatInterval | 10s | The interval between client heartbeats. |
| ➡️ Contract Details Rate Limits | ||
| contractRateLimit.maxRequestCount | 45 | Rate limit threshold for contract details requests. |
| contractRateLimit.rateLimitInterval | 1s | Interval for contract details request rate limit. |
Supported order attributes
| Key | Description |
|---|---|
| 1, 7001 | The account the trade will be allocated to. |
| 40, 7040 | The order’s type. Allowed values: TRAIL, TRAIL_LIMIT, TRAIL_LIT etc. |
| 47, 7047 | Rule80A. Allowed values: Individual, Agency, AgentOtherMember, IndividualPTIA, AgencyPTIA, AgentOtherMemberPTIA, IndividualPT, AgencyPT, AgentOtherMemberPT. |
| 59, 7059 | The time in force. Allowed values: DAY, GTC, OPG, IOC, GTD, GTT, AUC, FOK, GTX, DTC, Minutes. |
| 77, 7077 | OpenClose. 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, 7100 | The destination exchange. |
| 207, 7207 | The 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, 7167 | The 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, 7022 | Security’s identifier. Allowed values: CUSIP, SEDOL, ISIN, RIC. |
| 204, 7204 | Origin. 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, 7231 | The instrument’s multiplier (i.e. options, futures). |
| 439, 7439 | ExemptCode. Only available with IB Execution-Only accounts with applicable securities. Mark order as exempt from short sale uptick rule. |
| 6058 | TradingClass. 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 Params | Order Parameters - Repeating Group |
| 958 | Parameter name. Format: <Prefix>.<setterName>. Allowed prefix: Order, AlgoParams. |
| 959 | Parameter type. Allowed values: string,boolean,int,long,double. |
| 960 | Parameter value. |
Config sample
connectors {
IBTWS : ${template.connector.ibtws} {
settings {
host = "localhost"
port = 7497
clientId = 1
}
}
}