Python parser for Interactive Brokers Flex XML statements
ibflex2 converted Interactive Brokers Flex XML brokerage statement data into typed Python objects, so it could be processed and analyzed in scripts, notebooks, and pipelines. The sections below describe what the code in this repository does, not a package you should adopt today.
This module is for reading IBKR brokerage reports — it does not place trades.
Supports Python 3.9-3.13. The parser has no required dependency beyond defusedxml; the optional client for fetching Flex statements depends on requests (install ibflex2[web]).
Why this fork
Parses the current IBKR Flex XML format. Many fields and several whole sections (e.g. StockGrantActivity) added since 2021.
Opt-in unknown-attribute tolerance so brand-new IBKR fields don’t raise. See Tolerance below.
Security: parses XML through defusedxml to guard against XXE, billion-laughs, and external-entity attacks.
Maintained tooling: pyproject.toml, ruff, mypy, pytest matrix on Linux/macOS/Windows × Python 3.9-3.13. Coverage gate at 95% (currently 100% on every module except utils.py).
Schema-drift CI: a weekly job scrapes the IBKR Activity Flex Query Reference and opens a tracking issue when fields appear that aren’t declared in ibflex.Types.
If the original csingley/ibflex package resumes releases, this fork will be retired. See CHANGELOG.md for what’s changed.
Installation
pip install ibflex2 # parser only pip install ibflex2[web] # parser + flexget download client
Flex Parser
ibflex.parser parses Flex-format XML data into a hierarchy of Python objects whose structure matches the original Flex statements, with values converted to Python types (datetime.datetime, decimal.Decimal, etc.)
from ibflex import parser
response = parser.parse("2025-01_ibkr.xml")
# or:
response = parser.parse(xml_bytes)
statement = response.FlexStatements[0]
for trade in statement.Trades:
print(f"{trade.tradeDate} {trade.buySell.name} "
f"{abs(trade.quantity)} {trade.symbol} @ {trade.tradePrice} "
f"{trade.currency}")
Every Flex section is exposed as a Tuple of dataclass instances, e.g. statement.OpenPositions, statement.CashTransactions, statement.Trades. See ibflex/Types.py for every field on every dataclass.
Tolerance
IBKR adds new attributes to existing Flex sections regularly. To avoid FlexParserError when a brand-new attribute appears in your reports, opt into tolerance mode:
from ibflex import parser
# Process-wide:
parser.enable_unknown_attribute_tolerance()
response = parser.parse("recent_report.xml")
parser.disable_unknown_attribute_tolerance()
# Or scope-limited (recommended):
with parser.unknown_attribute_tolerance():
response = parser.parse("recent_report.xml")
Unknown attributes and unknown element types are silently skipped. Known attributes still parse normally. The flag is module-global and not thread-safe; use a single tolerance setting per process or guard with locking if needed.
If you find an unknown attribute that should be recognized, please open a new-field issue with the IBKR field name and an example XML snippet.
Flex report configuration
Configure Flex statements through Interactive Brokers account management: Reports > Flex Queries > Custom Flex Queries > Configure.
You can configure whatever you like and ibflex2 should parse it, with these caveats:
Use ISO-8601 date format (yyyy-MM-dd) or the default yyyyMMdd. European-style dd/MM/yyyy is not supported.
Use a delimiter between dates and times. The default semicolon is fastest to process.
For Trades, do not select the top options “Symbol Summary”, “Asset Class”, or “Orders”. (You can check the “Asset Class” box further down in the XML attribute list — that’s fine.)
Flex Client
Once you’ve defined Flex queries, you can generate an access token under Reports > Settings > FlexWeb Service. With the token and the query ID you can download statements over HTTP:
from ibflex import client, parser
statement_xml = client.download(token="111111111111111111111111",
query_id="111111")
response = parser.parse(statement_xml)
You can also run the bundled flexget script:
flexget -t 111111111111111111111111 -q 111111 > 2025-01_ibkr.xml
The client retries on transient Server busy (5 s backoff) and Throttled (10 s backoff) responses, and raises StatementGenerationTimeout after max_tries (default 5).
Development
With Nix (matches CI exactly):
nix develop pytest --cov=ibflex --cov-fail-under=95 tests/ mypy ibflex tests ruff check ibflex tests
Without Nix:
pip install -e .[web,dev] pytest tests/ mypy ibflex tests ruff check ibflex tests
See CONTRIBUTING.md for the new-field workflow, fixture conventions, and release process.
Resources
capgains — calculates realized gains using ibflex
ib-flex-analyzer — analyze Flex reports with pandas
Metadata
Release files for ibflex2 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ibflex2-1.0.1.tar.gz | 63.7 kB | With provenance | Details for ibflex2-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ibflex2-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for ibflex2-1.0.1-py3-none-any.whl |
Total release size: 96.4 kB
Release files / ibflex2-1.0.1.tar.gz
| Download URL | ibflex2-1.0.1.tar.gz |
|---|---|
| Size | 63.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17231bbe9c1e73e8f5451ac56abb7e78328060a61d1f0f989c6d134ebc661ecd
|
|
BLAKE2b-256 checksum How to use checksums |
c19008f5e2e839b9cfbd63916e7bfa39453a3a31851557402c8ee7498baa247f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / ibflex2-1.0.1-py3-none-any.whl
| Download URL | ibflex2-1.0.1-py3-none-any.whl |
|---|---|
| Size | 32.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d5ff25ddd76f8df59c0453c60ba83438edb05a9abf5fd09c6e6e5dd405834c5
|
|
BLAKE2b-256 checksum How to use checksums |
3f2f6be59342ca7004a59f51909aca34b651d0ed3c21dc8cfa6186284807de3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logSecurity for ibflex2 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for ibflex2-1.0.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.