ibind Documentation

repository·master·Indexed 19 days ago

https://github.com/voyz/ibind

An unofficial Python client library for the Interactive Brokers Client Portal Web API 1.0. It provides REST and WebSocket interfaces, supporting headless authentication via OAuth 1.0a. Key components include IbkrClient for synchronous request/response interactions and IbkrWsClient for asynchronous real-time data streaming.

Tokens
1.5K
Snippets
10
Records
10
Agent score
16%

What's inside ibind

  1. How IbkrClient and IbkrWsClient work together

    master

    IBind provides two primary client classes for interacting with the Interactive Brokers Client Portal Web API 1.0. Their usage patterns differ significantly:

    IbkrClient (REST API)

    Used for synchronous-style request/response interactions. You construct the client with appropriate arguments and call API methods directly. It includes features like automated question/answer handling, parallel requests, and rate limiting.

    IbkrWsClient (WebSocket API)

    Used for real-time data streaming. It is asynchronous and runs on a separate thread. Using it requires managing three distinct areas:

    1. Lifecycle: You must construct it, start it, and manage its lifecycle on the originating thread.
    2. Subscriptions: It is subscription-based; you must specify channels to subscribe to and remember to unsubscribe later.
    3. Data Consumption: It uses a queue system where you access and consume data from specific channel queues.

    Users are encouraged to start with IbkrClient before moving to the more complex IbkrWsClient.

    from ibind import IbkrClient, IbkrWsClient
  2. Use IbkrClient for REST API calls

    master

    To use the REST API, instantiate IbkrClient and call its methods. Most response objects contain a .data attribute containing the payload.

    from ibind import IbkrClient
    
    # Construct the client
    client = IbkrClient()
    
    # Call some endpoints
    print('\n#### check_health ####')
    print(client.check_health())
    
    print('\n\n#### tickle ####')
    print(client.tickle().data)
    
    print('\n\n#### get_accounts ####')
    print(client.portfolio_accounts().data)
  3. Use IbkrWsClient for WebSocket data streaming

    master

    To use the WebSocket API, you must ensure the IBIND_ACCOUNT_ID and IBIND_CACERT environment variables are set. The client is started with start=True, and you consume data by checking if a specific channel key has data available in its queue.

    from ibind import IbkrWsKey, IbkrWsClient
    
    # Construct the client. Assumes IBIND_ACCOUNT_ID and IBIND_CACERT environment variables have been set.
    ws_client = IbkrWsClient(start=True)
    
    # Choose the WebSocket channel
    ibkr_ws_key = IbkrWsKey.PNL
    
    # Subscribe to the PNL channel
    ws_client.subscribe(channel=ibkr_ws_key.channel)
    
    # Wait for new items in the PNL queue.
    while True:
      while not ws_client.empty(ibkr_ws_key):
        print(ws_client.get(ibkr_ws_key))
  4. Manage subscriptions with IbkrSubscriptionProcessor

    master

    IbkrSubscriptionProcessor is a specialized processor designed for handling WebSocket-based subscriptions within the IbkrWsClient ecosystem.

    from ibind import IbkrSubscriptionProcessor
    
    # Typically used in conjunction with IbkrWsClient
    processor = IbkrSubscriptionProcessor()
  5. Use IbkrWsClient for WebSocket streaming

    master

    The IbkrWsClient is used for real-time data streaming via WebSockets. It manages the connection lifecycle and allows for subscribing to specific data channels.

    from ibind import IbkrWsClient
    
    # Example usage (implementation details depend on IbkrWsClient methods)
    ws_client = IbkrWsClient(account_id='...', ...)
    # ws_client.connect()
  6. Initialize ibind logging

    master

    Use ibind_logs_initialize() to set up the standard logging configuration for the library, which is useful for debugging connection issues or monitoring API responses.

    from ibind import ibind_logs_initialize
    
    ibind_logs_initialize()
  7. Use IbkrClient for REST API interactions

    master

    The IbkrClient is the primary class for performing synchronous or asynchronous REST-based queries and actions against the IBKR API. It is the main entry point for standard request-response operations.

    from ibind import IbkrClient
    
    # Example usage (implementation details depend on IbkrClient methods)
    client = IbkrClient(account_id='...', ...)
    result = client.some_method()
  8. Construct order requests with OrderRequest

    master

    Use the OrderRequest class to define the parameters for a new order. Note that make_order_request is deprecated and should be avoided in favor of direct OrderRequest instantiation or other supported factory methods.

    from ibind import OrderRequest
    
    # Define your order parameters
    order = OrderRequest(
        # ... parameters
    )
  9. Handle broker-related errors with ExternalBrokerError

    master

    When interacting with the IBKR API, errors originating from the broker side are raised as ExternalBrokerError. Catch this exception to handle issues like insufficient funds, invalid symbols, or connectivity problems specific to the broker.

    from ibind import ExternalBrokerError
    
    try:
        client.execute_order(order)
    except ExternalBrokerError as e:
        print(f"Broker error occurred: {e}")