Skip to main content

A modern, Pydantic-based parser for Interactive Brokers (IBKR) Flex Query reports

Project description

py-ibkr

A modern, Pydantic-based parser for Interactive Brokers (IBKR) Flex Query reports. This version replaces the legacy ibflex library with strict type checking and improved support for newer IBKR XML fields.

Features

  • Pydantic Models: All data is parsed into typed Pydantic models with validation.
  • Robust Parsing: Handles "messy" IBKR data (e.g., legacy enums like Deposits/Withdrawals, inconsistent date formats).
  • Forward Compatible: Designed to handle new fields gracefully.

Installation

uv pip install py-ibkr
# or
pip install py-ibkr

Command Line Interface

py-ibkr includes a zero-dependency CLI for downloading Flex Queries.

# Download to a file
py-ibkr download --token YOUR_TOKEN --query-id YOUR_QUERY_ID --output report.xml

# Download with date range overrides (ISO format YYYY-MM-DD supported)
py-ibkr download -t YOUR_TOKEN -q YOUR_QUERY_ID --from-date 2023-01-01 --to-date 2023-01-31

# Alternatively, use a .env file (automatically loaded if present):
# IBKR_FLEX_TOKEN=your_token
# IBKR_FLEX_QUERY_ID=your_query_id
py-ibkr download -o report.xml

# Download to stdout (pipe to other tools)
py-ibkr download | xmllint --format -

Setup: Obtaining your Token and Query ID

To use the automated downloader, you must enable the Flex Web Service in your Interactive Brokers account:

  1. Log in to the IBKR Client Portal.
  2. Navigate to Performance & Reports > Flex Queries.
  3. Obtain Token: Click the gear icon or Flex Web Service Configuration. Enable the service and copy the Current Token.
  4. Obtain Query ID: Create a new Flex Query (e.g., Activity). After saving it, the Query ID will be visible in the list of your Flex Queries.

[!NOTE] Flex Queries are typically generated once per day after market close. Trade Confirmations are available with a 5-10 minute delay.

Usage

Downloading a Flex Query Report

You can automatically download reports using the FlexClient. This requires a Token and Query ID from the IBKR Client Portal.

from py_ibkr import FlexClient, parse

# 1. Initialize the client
client = FlexClient()

# 2. Download the report (handles the request-poll-fetch protocol)
xml_data = client.download(
    token="YOUR_IBKR_TOKEN",
    query_id="YOUR_QUERY_ID"
)

# 3. Parse the downloaded data
response = parse(xml_data)

print(f"Query Name: {response.queryName}")

Parsing a Flex Query File

for statement in response.FlexStatements: print(f"Account: {statement.accountId}")

# Access Trades
for trade in statement.Trades:
    print(f"Symbol: {trade.symbol}, Quantity: {trade.quantity}, Price: {trade.tradePrice}")
    
# Access Cash Transactions
for cash_tx in statement.CashTransactions:
    print(f"Type: {cash_tx.type}, Amount: {cash_tx.amount}")

### Models

You can import models directly for type hinting:

```python
from py_ibkr import Trade, CashTransaction

def process_trade(trade: Trade):
    print(trade.symbol)

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

py_ibkr-0.1.7.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

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

py_ibkr-0.1.7-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file py_ibkr-0.1.7.tar.gz.

File metadata

  • Download URL: py_ibkr-0.1.7.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_ibkr-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9ac81158581e50d28270a4f6641a67ff912b1b99af29aff0e0751e2ca3d969f1
MD5 f849893e9b82fa2a8bc1160ffe79cb12
BLAKE2b-256 b42eee4dce9543e222bd177b90dc94cdba9eac4b9fa6ec9b96828b11e9b239d3

See more details on using hashes here.

File details

Details for the file py_ibkr-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: py_ibkr-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_ibkr-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b4a2f3b276a4baf0ce577d90831e0850da4d3ae400e6c60994cff4bc7bf6d401
MD5 b715a7b4ecc540876d404d09c928eac5
BLAKE2b-256 cb640d2e83bb382800a576e99e080c0e1f0d54fffaf4a251c1977a3557da9697

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