Skip to main content

Crate ibapi

Crate ibapi 

Source
Expand description

githubcrates-iolicense


A comprehensive Rust implementation of the Interactive Brokers TWS API, providing a robust and user-friendly interface for TWS and IB Gateway. Designed with simplicity in mind, it integrates smoothly into trading systems.

API Documentation:

This fully featured API enables the retrieval of account information, access to real-time and historical market data, order management, market scanning, and access to news and Wall Street Horizons (WSH) event data. Future updates will focus on bug fixes, maintaining parity with the official API, and enhancing usability.

§Example

Connect to TWS / IB Gateway and place a market order:

use ibapi::prelude::*;

#[tokio::main]
async fn main() {
    let client = Client::connect("127.0.0.1:4002", 100)
        .await
        .expect("connection failed");

    let contract = Contract::stock("AAPL").build();
    let order_id = client
        .order(&contract)
        .buy(100)
        .market()
        .submit()
        .await
        .expect("order submission failed");
    println!("submitted order id: {order_id}");
}

For broader usage — quick start, examples, migration from v2, full API tour — see the README and the docs/ directory.

Modules§

accounts
Describes items present in an account.
client
TWS API Client.
config
APIs for reading TWS/Gateway configuration (API, precautions, orders, lock-and-exit settings). TWS/Gateway configuration read and write.
contracts
A Contract object represents trading instruments such as a stocks, futures or options.
display_groups
Display groups subscription support
errors
Error types for the IBAPI library.
market_data
APIs for retrieving market data Market data types and functionality
news
APIs for retrieving news data including articles, bulletins, and providers News data retrieval and management functionality.
orders
Data types for building and placing orders.
prelude
A prelude module for convenient importing of commonly used types. A prelude module for convenient importing of commonly used types and traits.
protocol
Protocol version checking and constants for TWS API features. Protocol version checking and constants for TWS API features.
scanner
APIs for working with the market scanner. Market scanner functionality for discovering trading opportunities.
subscriptions
Subscription types for streaming data Subscription types for sync/async streaming data.
trace
Server interaction tracing for debugging and monitoring Server interaction tracing for debugging and monitoring
wsh
APIs for working with Wall Street Horizon: Earnings Calendar & Event Data. Wall Street Horizon: Earnings Calendar & Event Data.

Structs§

Client
Asynchronous TWS API Client
ClientBuilder
Builder for an async Client. Acquire via Client::builder.
Notice
An error message from the TWS API.

Enums§

ConnectivityStatus
Connectivity sub-state of a data-farm notice within WARNING_CODE_RANGE.
Error
The main error type for IBAPI operations.
IncomingMessages
Messages emitted by TWS/Gateway over the market data socket.
NoticeCategory
Typed classification of a Notice by TWS error-code range.
OutgoingMessages
Outgoing message opcodes understood by TWS/Gateway.
StartupMessage
Typed handshake-time messages delivered to ClientBuilder::startup_callback.

Constants§

DATA_ADVISORY_CODES
Data-advisory codes: frames that look like errors but reject nothing — the request stands and data keeps flowing. Each one describes what will arrive (a fallback, a partial entitlement, or a reset of what was already delivered), so they are informational notices, not errors. Classifying them as errors would terminate the subscription before its data arrives.
HANDSHAKE_DECODE_FAILURE_CODE
Synthesized notice code emitted when a typed handshake decoder fails for a known IncomingMessages kind (OpenOrder, OrderStatus, AccountValue/PortfolioValue/AccountUpdateTime/AccountDownloadEnd, ExecutionData, CommissionsReport, CompletedOrder). Distinct from HANDSHAKE_UNKNOWN_FRAME_CODE so consumers can separate TWS schema drift from rust-ibapi decoder bugs. See Notice::is_handshake_synthetic.
HANDSHAKE_UNKNOWN_FRAME_CODE
Synthesized notice code emitted when a handshake-time frame’s IncomingMessages kind has no typed StartupMessage variant. Negative (TWS uses 0+); the other client-synthesized codes are HANDSHAKE_DECODE_FAILURE_CODE, UNKNOWN_MESSAGE_TYPE_CODE, SUBSCRIPTION_LAG_CODE, NOTICE_STREAM_LAG_CODE, and TRANSPORT_RECONNECT_CODE. See Notice::is_handshake_synthetic.
NOTICE_STREAM_LAG_CODE
Synthesized notice code delivered in-band on the notice stream when its consumer fell behind the notice fan-out: the broadcast channel evicted the oldest notices, and this notice — carrying the dropped count in its message — is what the consumer sees in their place.
ORDER_CANCELLED_CODE
Error code indicating an order was cancelled (confirmation, not an error).
ORDER_MESSAGE_CODE
Generic order-message code whose text determines whether TWS reports a warning or an error.
ORDER_REJECTION_CODE_RANGE
Range of error codes that can represent order rejections from TWS (200-399).
SUBSCRIPTION_LAG_CODE
Synthesized notice code emitted in-band on an async subscription whose consumer fell behind its broadcast channel: the channel evicted the oldest frames, and this notice — carrying the dropped count in its message — is what the consumer sees in their place.
SYSTEM_MESSAGE_CODES
System message codes indicating connectivity status.
TRANSPORT_RECONNECT_CODE
Synthesized notice code published to the notice stream (sync and async) after the transport finishes reconnecting its socket to TWS/Gateway: a new connection whose server-side state starts empty, so every request subscription made on the previous connection is gone, and unrouted-notice state — notably the 1100/1101/1102 connection-status sequence — describes the previous connection only.
UNKNOWN_MESSAGE_TYPE_CODE
Synthesized notice code emitted when a frame arrives whose 4-byte message id maps to no known IncomingMessages kind, so nothing can route it.
WARNING_CODE_RANGE
Range of error codes that are considered warnings: the whole 21xx band.

Functions§

register_timezone_alias
Register a custom mapping from a gateway-supplied timezone name to an IANA zone. Call before Client::connect for the mapping to apply during the connection handshake.