Skip to main content

Minimal async client for Interactive Brokers FlexQuery Web Service

Project description

ibkr-flex-client

Minimal async Python client for Interactive Brokers' Flex Web Service.

Handles the two-step API flow (SendRequest → GetStatement) with automatic retry on rate limits and processing delays. Returns a thin XML wrapper — you bring your own Pydantic models for parsing.

Prerequisites

You need a Flex Web Service token and at least one query ID from IB Account Management → Reports → Flex Queries → Flex Web Service. The token is account-scoped; each query ID corresponds to a specific report configuration.

Installation

pip install -e path/to/ibkr-flex-client

Or as a git submodule:

git submodule add git@github.com:vjt/ibkr-flex-client.git vendor/ibkr-flex-client
pip install -e vendor/ibkr-flex-client

Requires Python 3.12+ and aiohttp.

Usage

import aiohttp
from ibkr_flex_client import FlexClient, FlexStatement, FlexError

async def main():
    client = FlexClient(token="YOUR_TOKEN", query_id="YOUR_QUERY_ID")

    async with aiohttp.ClientSession() as session:
        statement: FlexStatement = await client.fetch(session)

    # Statement metadata
    print(statement.account_id)  # "U1234567"
    print(statement.from_date)   # date(2026, 1, 1)
    print(statement.to_date)     # date(2026, 3, 27)

    # Iterate over XML elements and parse into your own models
    for elem in statement.iter("Trade"):
        symbol = elem.get("symbol")
        quantity = float(elem.get("quantity", "0"))
        price = float(elem.get("tradePrice", "0"))
        print(f"{symbol}: {quantity} @ {price}")

    # Raw XML is available if needed
    print(len(statement.xml), "bytes")

API

FlexClient(*, token, query_id, max_retries=10, backoff_base=10.0)

Async client for IB's Flex Web Service.

  • token: Flex Web Service token from IB Account Management.
  • query_id: Flex Query ID for the specific report.
  • max_retries: Max poll attempts for GetStatement (default 10).
  • backoff_base: Base seconds for exponential backoff (default 10).

await client.fetch(session) -> FlexStatement

Fetches the report. Handles rate limiting (IB error 1018) and still-processing (1019) with automatic retry. Raises FlexError on auth failures, timeouts, or malformed responses.

FlexStatement(xml)

Thin wrapper around the IB FlexQuery XML response.

Attribute Type Description
account_id str IB account ID
from_date date Statement start date
to_date date Statement end date
xml str Raw XML string

statement.iter(tag) -> Iterator[Element]

Iterates all XML elements matching tag. Use this to extract trade fills, NAV entries, conversion rates, or any other section from the Flex report. Element attributes are strings — cast them to your own types.

FlexError

Exception raised on all API and parsing errors.

parse_ib_date(date_str) -> date

Parses IB's yyyyMMdd format. Raises ValueError on bad input.

Error Handling

IB's Flex Web Service has aggressive rate limiting. The client handles this transparently:

IB Error Meaning Client Behavior
1018 Rate limited Exponential backoff, retry
1019 Still processing Fixed short backoff, retry
1012 Token expired Raises FlexError immediately
Other Various failures Raises FlexError immediately

Testing

pip install -e ".[dev]"
pytest tests/ -x -q

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibkr_flex_client-0.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ibkr_flex_client-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file ibkr_flex_client-0.1.0.tar.gz.

File metadata

  • Download URL: ibkr_flex_client-0.1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for ibkr_flex_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aaf5d5bffa5602d5b958859faa21d910c4cbba9cef0c8c36eab60926f17f5ca7
MD5 574f6e6a0289c7dae4ada4af8d261e21
BLAKE2b-256 d5f510fc5e88fe4611ec088eda3de04660f3caa0f41cfe3c9ba16949833bdb5f

See more details on using hashes here.

File details

Details for the file ibkr_flex_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ibkr_flex_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df05a790d918c66f7f641fc5386125ef7e401f6feecd143217a1016f58913387
MD5 1b6a1e192b6e94992f6d48b32a505102
BLAKE2b-256 fb499b471a91bbf31c8f9f2f8fc1345f51c4a0efa1dbd77e154e0c353103d9cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page