| Crates.io | ib-flex |
| lib.rs | ib-flex |
| version | 0.1.6 |
| created_at | 2026-01-14 22:33:57.487468+00 |
| updated_at | 2026-01-15 06:02:32.105387+00 |
| description | Pure Rust parser for Interactive Brokers FLEX XML statements |
| homepage | https://github.com/clifton/ib-flex |
| repository | https://github.com/clifton/ib-flex |
| max_upload_size | |
| id | 2044041 |
| size | 886,621 |
Type-safe Rust parser for Interactive Brokers FLEX XML statements with comprehensive coverage and financial precision.
Add this to your Cargo.toml:
[dependencies]
ib-flex = "0.1"
use ib_flex::{parse_activity_flex, detect_statement_type, StatementType};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let xml = std::fs::read_to_string("flex_statement.xml")?;
// Automatically detect statement type
match detect_statement_type(&xml)? {
StatementType::Activity => {
let statement = parse_activity_flex(&xml)?;
println!("Account: {}", statement.account_id);
println!("Trades: {}", statement.trades.items.len());
}
StatementType::TradeConfirmation => {
let statement = ib_flex::parse_trade_confirmation(&xml)?;
println!("Account: {}", statement.account_id);
println!("Trades: {}", statement.trades.items.len());
}
}
Ok(())
}
Interactive Brokers FLEX queries must be configured in the IB Client Portal:
yyyy-MM-dd) or compact (yyyyMMdd)Important: European date formats (dd/MM/yyyy) are NOT supported by the IB FLEX API.
๐ For comprehensive setup instructions, see FLEX_SETUP.md which covers all 21 recommended sections, field selections, and configuration options.
The api-client feature provides programmatic access to fetch FLEX statements directly from Interactive Brokers without manual downloads.
[dependencies]
ib-flex = { version = "0.1", features = ["api-client"] }
use ib_flex::api::FlexApiClient;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create client with your token
let client = FlexApiClient::new("YOUR_TOKEN");
// Step 1: Send request with your query ID
let reference_code = client.send_request("123456").await?;
// Step 2: Get statement with automatic retry
let xml = client.get_statement_with_retry(
&reference_code,
10, // max retries
Duration::from_secs(2) // delay between retries
).await?;
// Step 3: Parse the statement
let statement = ib_flex::parse_activity_flex(&xml)?;
println!("Trades: {}", statement.trades.items.len());
Ok(())
}
Run the API examples (requires IB credentials):
export IB_FLEX_TOKEN="your_token"
export IB_FLEX_QUERY_ID="your_query_id"
cargo run --example fetch_flex_statement --features api-client
cargo run --example api_simple_usage --features api-client
cargo run --example api_with_retry --features api-client
Benchmarked on M1 MacBook Pro:
| Statement Type | Transactions | Parse Time |
|---|---|---|
| Minimal | 1 trade | ~6.5 ยตs |
| Options | 4 trades | ~65 ยตs |
| Cash | 15 transactions | ~71 ยตs |
Memory efficient with approximately 200 bytes per trade. Parsing 10,000 trades uses ~2MB of memory.
All financial values use rust_decimal::Decimal for precise calculations without floating-point errors. The library includes 15 comprehensive enums with 100+ variants covering:
The repository includes several complete example programs:
Run parsing examples:
cargo run --example parse_activity_statement
cargo run --example parse_trade_confirmation
cargo run --example filter_trades
cargo run --example calculate_commissions
To use this example:
cargo run --example backfill_summary -- path/to/your/backfill.xml
Output includes:
api-client feature)Run API examples:
export IB_FLEX_TOKEN="your_token"
export IB_FLEX_QUERY_ID="your_query_id"
cargo run --example fetch_flex_statement --features api-client
cargo build
cargo test
cargo bench
cargo fmt
cargo clippy -- -D warnings
The library has comprehensive test coverage including:
The library includes comprehensive reliability tests using:
Run tests with:
cargo test # All tests
cargo test --doc # Documentation tests only
Contributions welcome! This is an open-source project designed to benefit the Rust trading community.
Before submitting a PR:
cargo testcargo clippy -- -D warningscargo fmtBug reports and feature requests are appreciated. When reporting bugs, please include:
Licensed under either of:
at your option.
See PLAN.md for detailed implementation statistics and EDGE_CASES_SUMMARY.md for edge case coverage.