ibapi 2.11.1

A Rust implementation of the Interactive Brokers TWS API, providing a reliable and user friendly interface for TWS and IB Gateway. Designed with a focus on simplicity and performance.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# API Patterns

## Builder Patterns

The library provides unified builder patterns to simplify common operations in both sync and async modes.

### Contract Builder

The V2 contract builder API uses type-state patterns to ensure compile-time safety:

```rust
use ibapi::contracts::{Contract, ContractMonth};

// Stock builder - simple with defaults
let stock = Contract::stock("AAPL").build();

// Stock with customization
let intl_stock = Contract::stock("7203")
    .on_exchange("TSEJ")
    .in_currency("JPY")
    .build();

// Option builder - enforces required fields at compile time
let option = Contract::call("AAPL")
    .strike(150.0)  // Required - validates positive value
    .expires_on(2024, 12, 20)  // Required
    .build();  // Only available when all required fields are set

// This won't compile - missing required fields:
// let invalid = Contract::call("AAPL").build();  // Error: build() not available

// Futures with smart defaults
let futures = Contract::futures("ES")
    .expires_in(ContractMonth::new(2024, 3))
    .build();
```

The contract builder pattern provides:
- **Type-state tracking**: Required fields enforced at compile time
- **Smart defaults**: Sensible defaults for common use cases
- **Strong typing**: Type-safe wrappers for exchanges, currencies, and option rights
- **Zero invalid states**: Can't build incomplete contracts

For comprehensive documentation, see the [Contract Builder Guide](contract-builder.md).

### Request Builder

For client methods with request IDs:

```rust
// Sync mode
pub fn pnl(client: &Client, account: &str) -> Result<Subscription<PnL>, Error> {
    let builder = client
        .request()
        .check_version(server_versions::PNL, "PnL not supported")?;
    
    let request = encode_request_pnl(builder.request_id(), account)?;
    builder.send(request)
}

// Async mode - identical except for .await
pub async fn pnl(client: &Client, account: &str) -> Result<Subscription<PnL>, Error> {
    let builder = client
        .request()
        .check_version(server_versions::PNL, "PnL not supported")
        .await?;
    
    let request = encode_request_pnl(builder.request_id(), account)?;
    builder.send(request).await
}
```

### Shared Request Builder

For requests using shared channels (no request ID):

```rust
// Sync mode
pub fn positions(client: &Client) -> Result<Subscription<PositionUpdate>, Error> {
    let request = encode_request_positions()?;
    
    client
        .shared_request(OutgoingMessages::RequestPositions)
        .send(request)
}

// Async mode
pub async fn positions(client: &Client) -> Result<Subscription<PositionUpdate>, Error> {
    let request = encode_request_positions()?;
    
    client
        .shared_request(OutgoingMessages::RequestPositions)
        .send(request)
        .await
}
```

### Order Request Builder

For order operations:

```rust
pub fn place_order(client: &Client, contract: &Contract, order: &Order) -> Result<(), Error> {
    let builder = client.order_request();
    let request = encode_order(builder.order_id(), contract, order)?;
    builder.send(request)?;  // .await for async
    Ok(())
}
```

### Subscription Builder

Create subscriptions with additional context:

```rust
pub fn market_depth(client: &Client, contract: &Contract, num_rows: i32) 
    -> Result<Subscription<MarketDepth>, Error> 
{
    let request_id = client.next_request_id();
    let request = encode_market_depth(request_id, contract, num_rows)?;
    
    client
        .subscription::<MarketDepth>()
        .with_smart_depth(true)
        .send_with_request_id(request_id, request)
        // .await for async version
}
```

### Conditional Order Builder Pattern

The library provides a fluent API for building conditional orders with type-safe condition builders and ergonomic helper functions.

#### Helper Functions

Helper functions provide a concise way to create condition builders:

```rust
use ibapi::orders::builder::{price, time, margin, volume, execution, percent_change};
use ibapi::orders::order_builder;

// Helper functions return partially-built condition builders
let price_cond = price(265598, "SMART").greater_than(150.0);
let time_cond = time().greater_than("20251230 14:30:00 US/Eastern");
let margin_cond = margin().less_than(30);
let volume_cond = volume(76792991, "SMART").greater_than(50_000_000);
let pct_change_cond = percent_change(756733, "SMART").greater_than(2.0);

// Execution condition returns OrderCondition directly (no threshold)
let exec_cond = execution("TSLA", "STK", "SMART");

// Create order with single condition
let order = order_builder::market_order(Action::Buy, 100.0)
    .condition(price_cond)
    .build();
```

#### Fluent Condition Chaining

The `OrderBuilder` provides methods for chaining conditions with AND/OR logic:

```rust
use ibapi::orders::builder::{price, time, volume};
use ibapi::orders::order_builder;

// Multiple conditions with AND logic (all must be true)
let order = order_builder::limit_order(Action::Buy, 100.0, 151.0)
    .condition(price(265598, "SMART").greater_than(150.0))
    .and_condition(volume(265598, "SMART").greater_than(80_000_000))
    .and_condition(time().greater_than("20251230 10:00:00 US/Eastern"))
    .build();

// Multiple conditions with OR logic (any can trigger)
let order = order_builder::market_order(Action::Sell, 100.0)
    .condition(margin().less_than(25))
    .or_condition(price(265598, "SMART").less_than(140.0))
    .or_condition(time().greater_than("20251230 15:55:00 US/Eastern"))
    .build();

// Mixed AND/OR logic
let order = order_builder::limit_order(Action::Buy, 50.0, 452.0)
    .condition(price(265598, "SMART").greater_than(150.0))
    .and_condition(volume(265598, "SMART").greater_than(50_000_000))  // Price AND Volume
    .or_condition(time().greater_than("20251230 14:00:00 US/Eastern")) // OR Time
    .build();
```

#### Type-State Pattern for Conditions

Each condition builder uses the type-state pattern to ensure valid configuration:

```rust
use ibapi::orders::conditions::{PriceCondition, TriggerMethod};

// Builder starts with required fields only
let builder = PriceCondition::builder(265598, "SMART");

// Threshold and direction set together (type-safe)
let condition = builder.greater_than(150.0);  // Sets price + direction
// or
let condition = builder.less_than(140.0);     // Sets price + direction

// Optional configuration
let condition = condition
    .trigger_method(TriggerMethod::Last)  // Use last price
    .conjunction(true);                   // AND with next condition

// Convert to OrderCondition
let condition = condition.build();
```

All condition builders follow this pattern:
- **Required parameters** in the constructor (contract ID, exchange, etc.)
- **Threshold and direction** set together via `greater_than()` or `less_than()`
- **Optional parameters** via chainable methods
- **Type conversion** via `.build()` or automatic `Into<OrderCondition>`

#### Old vs New API Comparison

**Before (v0.x):**
```rust
// Threshold in constructor, separate trigger direction method
let price_cond = PriceCondition::builder(265598, "SMART", 150.0)
    .trigger_above()
    .build();

let time_cond = TimeCondition::builder("20251230 14:30:00 US/Eastern")
    .trigger_after()
    .build();

// Manual condition assignment
let mut order = order_builder::market_order(Action::Buy, 100.0);
order.conditions = vec![
    OrderCondition::Price(price_cond),
    OrderCondition::Time(time_cond),
];
```

**After (v1.0+):**
```rust
// Threshold and direction combined, fluent chaining
let order = order_builder::market_order(Action::Buy, 100.0)
    .condition(price(265598, "SMART").greater_than(150.0))
    .and_condition(time().greater_than("20251230 14:30:00 US/Eastern"))
    .build();

// Or using builders directly
let price_cond = PriceCondition::builder(265598, "SMART")
    .greater_than(150.0)
    .build();

let time_cond = TimeCondition::builder()
    .greater_than("20251230 14:30:00 US/Eastern")
    .build();
```

Key improvements:
- **More ergonomic**: Helper functions reduce boilerplate
- **Type-safe**: Threshold and direction set atomically
- **Fluent**: Method chaining for AND/OR logic
- **Explicit**: `greater_than()` vs `less_than()` is clearer than `trigger_above()` vs `trigger_below()`
- **Consistent**: All condition types follow the same pattern

#### Sync and Async Usage

Conditional orders work identically in both sync and async modes:

**Sync Mode:**
```rust
use ibapi::client::blocking::Client;
use ibapi::contracts::Contract;
use ibapi::orders::builder::{price, order_builder};
use ibapi::orders::Action;

let client = Client::connect("127.0.0.1:7497", 100)?;
let contract = Contract::stock("AAPL").build();

let order = order_builder::market_order(Action::Buy, 100.0)
    .condition(price(265598, "SMART").greater_than(150.0))
    .build();

let order_id = client.next_valid_order_id()?;
client.submit_order(order_id, &contract, &order)?;
```

**Async Mode:**
```rust
use ibapi::client::Client;
use ibapi::contracts::Contract;
use ibapi::orders::builder::{price, order_builder};
use ibapi::orders::Action;

let client = Client::connect("127.0.0.1:4002", 100).await?;
let contract = Contract::stock("AAPL").build();

let order = order_builder::market_order(Action::Buy, 100.0)
    .condition(price(265598, "SMART").greater_than(150.0))
    .build();

let order_id = client.next_valid_order_id().await?;
client.submit_order(order_id, &contract, &order).await?;
```

The only difference is the `.await` calls on client methods. The order building logic is identical.

#### Advanced Pattern: Reusable Condition Components

Create reusable condition components for common trading scenarios:

```rust
use ibapi::orders::builder::{price, time, volume, margin};
use ibapi::orders::conditions::OrderCondition;

// Reusable condition builders
fn liquidity_check(contract_id: i32, min_volume: i32) -> impl Into<OrderCondition> {
    volume(contract_id, "SMART").greater_than(min_volume)
}

fn trading_hours_only() -> impl Into<OrderCondition> {
    time()
        .greater_than("20251230 09:30:00 US/Eastern")
        .conjunction(true)
}

fn risk_guard() -> impl Into<OrderCondition> {
    margin().less_than(30)
}

// Compose conditions
let order = order_builder::market_order(Action::Buy, 100.0)
    .condition(liquidity_check(265598, 50_000_000))
    .and_condition(trading_hours_only())
    .and_condition(risk_guard())
    .build();
```

For comprehensive conditional order documentation, see [Order Types - Conditional Orders](order-types.md#conditional-orders-with-conditions).

## Protocol Version Checking

Use the protocol module for version-specific features:

```rust
use crate::protocol::{check_version, Features, is_supported};

// Check if feature is supported
pub fn tick_by_tick_trades(&self, contract: &Contract) 
    -> Result<Subscription<Trade>, Error> 
{
    check_version(self.server_version, Features::TICK_BY_TICK)?;
    // ... implementation
}

// Conditional field encoding
pub fn encode_order(order: &Order, server_version: i32) -> RequestMessage {
    let mut message = RequestMessage::new();
    
    // Always included
    message.push_field(&order.order_id);
    
    // Conditionally included based on server version
    if is_supported(server_version, Features::DECISION_MAKER) {
        message.push_field(&order.decision_maker);
    }
    
    message
}
```

## Subscription Patterns

### Sync Mode (Iterator)
```rust
let positions = client.positions()?;

// Iterate until completion
for position in positions {
    match position? {
        PositionUpdate::Position(p) => println!("Position: {:?}", p),
        PositionUpdate::End => break,
    }
}
```

### Async Mode (Stream)
```rust
use futures::StreamExt;

let mut positions = client.positions().await?;

// Stream until completion
while let Some(position) = positions.next().await {
    match position? {
        PositionUpdate::Position(p) => println!("Position: {:?}", p),
        PositionUpdate::End => break,
    }
}
```

## Trading Hours Support

The `TradingHours` enum controls whether data includes extended hours (pre-market and after-hours). However, not all market data APIs support this parameter at the TWS protocol level.

| API | TradingHours Support | Notes |
|-----|---------------------|-------|
| `realtime_bars()` | ✓ Yes | Server-side filtering via `use_rth` |
| `historical_data()` | ✓ Yes | Server-side filtering via `use_rth` |
| `market_data()` | ✗ No | TWS protocol doesn't support filtering |

### Realtime Bars (Extended Hours Supported)

```rust
use ibapi::market_data::TradingHours;

// Regular trading hours only
let bars = client.realtime_bars(
    &contract,
    BarSize::Sec5,
    WhatToShow::Trades,
    TradingHours::Regular,  // Excludes pre/post-market
).await?;

// Include extended hours
let bars = client.realtime_bars(
    &contract,
    BarSize::Sec5,
    WhatToShow::Trades,
    TradingHours::Extended,  // Includes pre/post-market
).await?;
```

### Market Data Subscriptions (No Extended Hours Filtering)

The TWS API's `reqMktData` does not support a `useRth` parameter. Streaming tick data automatically includes all available data, including pre-market and after-hours quotes when the exchange reports them.

```rust
// Market data subscriptions receive ALL available data
// including extended hours - no filtering option exists
let ticks = client.market_data(&contract)
    .subscribe()
    .await?;
```

To filter for regular trading hours only, you must filter client-side based on timestamp and the trading session times for your specific exchange.

## Error Handling Patterns

### Connection Errors
```rust
match client.market_data(contract).subscribe() {
    Ok(subscription) => process_data(subscription),
    Err(Error::NotConnected) => {
        // Wait for reconnection
        while !client.is_connected() {
            thread::sleep(Duration::from_secs(1));
        }
        // Retry
    },
    Err(e) => return Err(e),
}
```

### Subscription Errors
```rust
for result in subscription {
    match result {
        Ok(data) => process(data),
        Err(Error::ConnectionReset) => {
            // Resubscribe after reconnection
            break;
        },
        Err(e) => log::error!("Error: {}", e),
    }
}
```

## Common Patterns

### Concurrent Subscriptions
```rust
use std::sync::Arc;
use std::thread;

let client = Arc::new(client);

let handles: Vec<_> = contracts
    .into_iter()
    .map(|contract| {
        let client = Arc::clone(&client);
        thread::spawn(move || {
            let data = client.market_data(&contract).subscribe()?;
            process_market_data(data)
        })
    })
    .collect();
```

### Rate Limiting
```rust
use std::time::{Duration, Instant};

struct RateLimiter {
    last_request: Instant,
    min_interval: Duration,
}

impl RateLimiter {
    fn wait_if_needed(&mut self) {
        let elapsed = self.last_request.elapsed();
        if elapsed < self.min_interval {
            thread::sleep(self.min_interval - elapsed);
        }
        self.last_request = Instant::now();
    }
}
```

### Reconnection Handling
```rust
loop {
    if !client.is_connected() {
        log::info!("Waiting for reconnection...");
        thread::sleep(Duration::from_secs(5));
        continue;
    }

    match perform_operation(&client) {
        Ok(result) => return Ok(result),
        Err(Error::NotConnected) => continue,
        Err(e) => return Err(e),
    }
}
```

## Trait Composition Patterns

### Domain Traits for Shared Behavior
```rust
// Use domain traits when:
// - 2+ types need the same operation (encode, decode, validate)
// - You want to write generic functions over those types
pub trait Encodable {
    fn encode(&self, message: &mut RequestMessage) -> Result<(), Error>;
}

pub trait Decodable: Sized {
    fn decode(fields: &mut FieldIter) -> Result<Self, Error>;
}

// Implement for types that need this behavior
impl Encodable for Order { /* ... */ }
impl Encodable for Contract { /* ... */ }
```

### Extension via Composition
```rust
// Use composition when:
// - A type needs capabilities from multiple sources
// - Behavior should be added without modifying the original type
pub struct Subscription<T> {
    receiver: Receiver<T>,
    cancel_fn: Box<dyn Fn() + Send>,
}

// Add behavior via trait impls
impl<T> Iterator for Subscription<T> { /* ... */ }
impl<T> Drop for Subscription<T> { /* ... */ }
```

### Newtype Wrappers for Domain Constraints
```rust
// Bad: raw i32 allows invalid IDs and type confusion
fn lookup(contract_id: i32, order_id: i32) -> Contract { /* ... */ }  // easy to swap args

// Good: newtype wrappers prevent mistakes
// Use newtype wrappers when:
// - A primitive has domain constraints (non-zero, positive, etc.)
// - Type confusion is possible (ContractId vs OrderId)
pub struct ContractId(i32);

impl ContractId {
    pub fn new(id: i32) -> Result<Self, Error> {
        if id <= 0 { return Err(Error::InvalidContractId); }
        Ok(Self(id))
    }
}

// Type system prevents invalid states
fn lookup(id: ContractId) -> Contract { /* ... */ }  // Can't pass raw i32
```