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:
- Log in to the IBKR Client Portal.
- Navigate to Performance & Reports > Flex Queries.
- Obtain Token: Click the gear icon or Flex Web Service Configuration. Enable the service and copy the Current Token.
- 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.
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)
Metadata
Release files for py-ibkr 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_ibkr-0.1.8.tar.gz | 24.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_ibkr-0.1.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 42.3 kB
Release files / py_ibkr-0.1.8.tar.gz
| Download URL | py_ibkr-0.1.8.tar.gz |
|---|---|
| Size | 24.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49b7d9f564dcda616bbf7c67eefe4dd7d95afd8ccdc5d13761ae046d24364aaf
|
|
BLAKE2b-256 checksum How to use checksums |
a342fe8ae67b91def9106445637b083d8cf79cb021f2bb57cb1f0e63b00873df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","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}
|
Release files / py_ibkr-0.1.8-py3-none-any.whl
| Download URL | py_ibkr-0.1.8-py3-none-any.whl |
|---|---|
| Size | 18.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c901604c44cd76822b5a9f1e30f861c77c2cd86b9567648f40e6087a12c9abb
|
|
BLAKE2b-256 checksum How to use checksums |
56b69286fd391e18173fc671a2ae848d94b5048d29f683b7bff4b10aa8fe8739
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","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}
|