Skip to main content

ibkr-flex-accounting

Strict, resumable Interactive Brokers Flex Web Service v3 accounting, reconciliation, and market-projection engine for Python.

Why this library exists

Thin XML parsers turn Flex statements into objects, but real portfolio accounting hits harder problems:

  • Strict arithmetic reconciliation: Every accepted snapshot must reconcile native cash (CashReport), positions (OpenPositions), accruals (InterestAccruals, OpenDividendAccruals), closing FX (ConversionRates), Statement of Funds running balances (StmtFunds), Forex balances (FxPositions), ChangeInNAV, and MTMPerformanceSummaryInBase / FIFOPerformanceSummaryInBase using Decimal arithmetic.
  • Resumable multi-lane acquisition: Independent daily, period (YTD/MTD), performance_history, and history lanes persist durable JSON journals so a slow backfill never blocks the daily close, references survive restarts, and tokens are never logged or redirected off *.interactivebrokers.com.
  • Immutable archives + safe compaction: Raw XML files are stored content-addressed (YYYY-MM-DD-ibkr-flex-<sha16>.xml), superseded period windows can be compacted in memory and safely pruned on disk while preserving every daily original.
  • Any base currency: Base currency (USD, EUR, GBP, CHF, etc.) is inferred from EquitySummaryInBase or enforced explicitly via base_currency=....
  • Pure market projection: value(report, when, quote=..., fx=...) projects confirmed broker quantities onto external closing prices/FX without mutating accounting facts, separating quote-source basis differences from market movement.

Installation

pip install ibkr-flex-accounting

Or with uv:

uv add ibkr-flex-accounting

Quick start

Python API

from pathlib import Path
from ibkr_flex_accounting import account, store

cfg = {
    "account": "U1234567",
    "query_id": "123456",
    "query_name": "Portfolio Accounting",
    "anchor": "2025-12-31",
}

# Bootstrap a normalized store from an archive of Flex XML files
acct_store, rejected = store.bootstrap(Path("./archive"), cfg)
store.compact(acct_store)

latest = account.latest(acct_store, "2026-09-08")
print(latest["base_currency"], latest["nav"]["total"])

Multi-lane collection (resumable)

import os
from pathlib import Path
from ibkr_flex_accounting import account, reporting, store

acct_store = account.load("store.json")
acct_store, acks, findings = store.collect(
    acct_store,
    cfg,
    state_dir=Path("./state"),
    archive=Path("./archive"),
    when="2026-09-08",
    token=os.environ["IBKR_FLEX_TOKEN"],
)
store.compact(acct_store)
store.save("store.json", acct_store)
for state_path, state, _ in acks:
    reporting.acknowledge(state_path, state)

CLI

# Build a store from archived XMLs
ibkr-flex bootstrap --archive ./archive --config ibkr-flex.yaml --output store.json --compact

# Run one resumable collection cycle (reads IBKR_FLEX_TOKEN from env)
ibkr-flex collect --archive ./archive --config ibkr-flex.yaml --state-dir ./state --store store.json

# Inspect latest NAV, controls, and issues
ibkr-flex inspect --store store.json

# Safely prune superseded period XMLs (all daily originals are preserved)
ibkr-flex prune --archive ./archive --store store.json

See docs/flex-query-setup.md for the required IBKR Activity Flex Query sections and configuration fields.

Security & safety invariants

  • XXE-safe: Rejects XML payloads containing <!DOCTYPE or <!ENTITY.
  • No token leakage: GetStatement only follows https://*.interactivebrokers.com/.../GetStatement URLs, disables HTTP redirects (NoRedirect), redacts tokens and URLs from error messages, and caps responses at 32 MiB.
  • Rate-limit friendly: Enforces a 1.1s cooldown after every Flex Web Service call (including failures).
  • Atomic writes: Journals, archives, and JSON stores use same-directory temp file + fsync + os.replace.

License

MIT — see LICENSE.

Metadata

Release files for ibkr-flex-accounting 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ibkr-flex-accounting 0.1.0
File Size Uploaded Provenance Actions
ibkr_flex_accounting-0.1.0.tar.gz 66.0 kB Without provenance Details for ibkr_flex_accounting-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ibkr-flex-accounting 0.1.0
File Interpreter ABI Platform Actions
ibkr_flex_accounting-0.1.0-py3-none-any.whl Python 3 none any Details for ibkr_flex_accounting-0.1.0-py3-none-any.whl

Total release size: 100.0 kB

Release files / ibkr_flex_accounting-0.1.0.tar.gz

Download URL ibkr_flex_accounting-0.1.0.tar.gz
Size 66.0 kB
Tags Source
SHA-256 checksum
How to use checksums
85c317608fa12462bf7895b3b3ec7e1f49c944a0f02ac6f44a8bdac9b7ad7971
BLAKE2b-256 checksum
How to use checksums
9bcb80a204a50d091d83291d7e4f66424c671605bff913d6f7587da50ac96377
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ibkr_flex_accounting-0.1.0-py3-none-any.whl

Download URL ibkr_flex_accounting-0.1.0-py3-none-any.whl
Size 34.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8d2c12f5c5bcb532a708902ae24b317263abdeb5e7413aa3f0104f79bb9b7da
BLAKE2b-256 checksum
How to use checksums
a7147daa93c98c0b9f7fa2833b94a8433fa61bec47cb2ac1ebe5309e9bc94f31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for ibkr-flex-accounting 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page