Skip to main content

Module ibkr

Module ibkr 

Source
Expand description

Interactive Brokers ExecutionClient implementation.

Uses the ibapi crate for IB TWS/Gateway connectivity. Supports equities, futures, options, and forex.

§Testing Status

NOT TESTED in CI. IBKR has not confirmed permission to use credentials for CI, and requires IB Gateway/TWS running locally.

Tested locally: All execution tests (connection, orders, account streaming) run on paper trading accounts — no market data subscriptions required.

§Connection

Requires TWS or IB Gateway running locally with API enabled:

ApplicationLive PortPaper Port
TWS74967497
IB Gateway40014002

Enable API in TWS/Gateway: Configure → API → Settings → Enable ActiveX and Socket Clients. For order placement, uncheck “Read-Only API”.

§Architecture

  • Connection: TCP socket to TWS/Gateway
  • Orders: Subscription-based events (OrderStatus, ExecutionData, CommissionReport)
  • Account: Subscription-based position and balance updates

§Limitations

  • Order types: Market, Limit, Stop, StopLimit, TrailingStop, TrailingStopLimit, and Bracket (entry + take-profit + stop-loss) supported. No Algo orders.
  • TimeInForce: No post_only (IB has no maker-only orders)
  • No auto-reconnect: Caller responsibility per library philosophy

§Caller Responsibilities (Reconnection & Recovery)

Unlike WebSocket-based connectors (Binance, Alpaca), this client does not auto-reconnect. The caller must handle:

  1. Disconnect detection: Monitor ExecutionClient::account_stream for EOF or errors
  2. Reconnection: Call IbkrClient::connect_sync with a new client ID
  3. Fill recovery: After reconnect, call ExecutionClient::fetch_trades to query executions since disconnect, then deduplicate against known fills
  4. Order reconciliation: Call ExecutionClient::fetch_open_orders to reconcile open-order state (order lifecycle events during disconnect are lost)
  5. Stale state cleanup: Periodically call IbkrClient::clear_stale_executions, IbkrClient::clear_stale_order_ids, and IbkrClient::clear_stale_pending_cancels

Rationale: IBKR uses TCP to local TWS/Gateway, not cloud WebSocket. Reconnection requires IB Gateway availability and client ID coordination — decisions that belong in the caller’s wrapper, not the library.

§See Also

Re-exports§

pub use order::BracketOrderRequest;
pub use order::BracketOrderResult;
pub use execution::parse_ib_timestamp;

Modules§

account
contract
Contract builders for IB integration.
execution
order

Structs§

ContractConfig
Pre-configured contract for startup registration.
IbkrClient
Interactive Brokers execution client.
IbkrConfig
Configuration for the IBKR execution client.