ibapi 4.0.0

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
use crate::common::test_utils::helpers::assert_decimal_parse_error;

use super::*;
use prost::Message;
use time::macros::{date, datetime};

use crate::common::test_utils::helpers::assert_rejects_text_framing;
use crate::market_data::historical::BarTimestamp;
use crate::messages::IncomingMessages;
use crate::testdata::builders::market_data::{
    histogram_data_response, histogram_entry, historical_data_daily_bar, historical_data_response, historical_tick_bid_ask, historical_tick_last,
    historical_tick_mid, historical_ticks_bid_ask_response, historical_ticks_last_response, historical_ticks_response,
};
use crate::testdata::builders::ResponseProtoEncoder;

// ---------------------------------------------------------------------------
// Happy-path proto decoders. Each test drives bytes through the `*_proto`
// helper directly (scanner precedent: src/scanner/common/decoders_tests.rs).
// ---------------------------------------------------------------------------

#[test]
fn test_decode_historical_data_proto() {
    let proto_msg = crate::proto::HistoricalData {
        req_id: Some(1),
        historical_data_bars: vec![
            crate::proto::HistoricalDataBar {
                date: Some("1681133400".into()),
                open: Some(185.50),
                high: Some(186.00),
                low: Some(185.00),
                close: Some(185.75),
                volume: Some("1000".into()),
                wap: Some("185.625".into()),
                bar_count: Some(150),
            },
            crate::proto::HistoricalDataBar {
                date: Some("20230411".into()),
                open: Some(186.00),
                high: Some(187.00),
                low: Some(185.50),
                close: Some(186.50),
                volume: Some("2000".into()),
                wap: Some("186.25".into()),
                bar_count: Some(300),
            },
        ],
    };

    let bars = decode_historical_data_proto(&proto_msg.encode_to_vec()).unwrap();
    assert_eq!(bars.len(), 2);

    assert_eq!(bars[0].date, BarTimestamp::DateTime(datetime!(2023-04-10 13:30:00 UTC)));
    assert_eq!(bars[0].open, 185.50);
    assert_eq!(bars[0].high, 186.00);
    assert_eq!(bars[0].low, 185.00);
    assert_eq!(bars[0].close, 185.75);
    assert_eq!(bars[0].volume, 1000.0);
    assert_eq!(bars[0].wap, 185.625);
    assert_eq!(bars[0].count, 150);

    assert_eq!(bars[1].open, 186.00);
    assert_eq!(bars[1].date, BarTimestamp::Date(date!(2023 - 04 - 11)));
    assert_eq!(bars[1].count, 300);
}

#[test]
fn test_decode_historical_ticks_last_proto() {
    let proto_msg = crate::proto::HistoricalTicksLast {
        req_id: Some(1),
        historical_ticks_last: vec![
            crate::proto::HistoricalTickLast {
                time: Some(1681133400),
                tick_attrib_last: Some(crate::proto::TickAttribLast {
                    past_limit: Some(true),
                    unreported: Some(false),
                }),
                price: Some(11.63),
                size: Some("100".into()),
                exchange: Some("ISLAND".into()),
                special_conditions: Some("O X".into()),
            },
            crate::proto::HistoricalTickLast {
                time: Some(1681133401),
                tick_attrib_last: Some(crate::proto::TickAttribLast {
                    past_limit: Some(false),
                    unreported: Some(true),
                }),
                price: Some(11.73),
                size: Some("50".into()),
                exchange: Some("FINRA".into()),
                special_conditions: Some("I".into()),
            },
        ],
        is_done: Some(true),
    };

    let (ticks, done) = decode_historical_ticks_last_proto(&proto_msg.encode_to_vec()).unwrap();
    assert!(done);
    assert_eq!(ticks.len(), 2);

    assert_eq!(ticks[0].timestamp, datetime!(2023-04-10 13:30:00 UTC));
    assert!(ticks[0].tick_attribute_last.past_limit);
    assert!(!ticks[0].tick_attribute_last.unreported);
    assert_eq!(ticks[0].price, 11.63);
    assert_eq!(ticks[0].size, Some(100.0));
    assert_eq!(ticks[0].exchange, "ISLAND");
    assert_eq!(ticks[0].special_conditions, "O X");

    assert_eq!(ticks[1].timestamp, datetime!(2023-04-10 13:30:01 UTC));
    assert!(!ticks[1].tick_attribute_last.past_limit);
    assert!(ticks[1].tick_attribute_last.unreported);
    assert_eq!(ticks[1].size, Some(50.0));
    assert_eq!(ticks[1].exchange, "FINRA");
}

#[test]
fn test_decode_head_timestamp_proto() {
    let proto_msg = crate::proto::HeadTimestamp {
        req_id: Some(1),
        head_timestamp: Some("1609459200".into()),
    };

    let result = decode_head_timestamp_proto(proto_msg).unwrap();
    assert_eq!(result, OffsetDateTime::from_unix_timestamp(1609459200).unwrap());
}

#[test]
fn test_decode_historical_ticks_proto() {
    let proto_msg = crate::proto::HistoricalTicks {
        req_id: Some(1),
        historical_ticks: vec![
            crate::proto::HistoricalTick {
                time: Some(1681133400),
                price: Some(150.0),
                size: Some("100".into()),
            },
            crate::proto::HistoricalTick {
                time: Some(1681133401),
                price: Some(150.5),
                size: Some("200".into()),
            },
        ],
        is_done: Some(false),
    };

    let (ticks, done) = decode_historical_ticks_proto(&proto_msg.encode_to_vec()).unwrap();
    assert_eq!(ticks.len(), 2);
    assert!(!done);
    assert_eq!(ticks[0].timestamp, datetime!(2023-04-10 13:30:00 UTC));
    assert_eq!(ticks[0].price, 150.0);
    assert_eq!(ticks[0].size, Some(100.0));
}

#[test]
fn test_decode_historical_ticks_bid_ask_proto() {
    let proto_msg = crate::proto::HistoricalTicksBidAsk {
        req_id: Some(1),
        historical_ticks_bid_ask: vec![crate::proto::HistoricalTickBidAsk {
            time: Some(1681133400),
            tick_attrib_bid_ask: Some(crate::proto::TickAttribBidAsk {
                bid_past_low: Some(true),
                ask_past_high: Some(false),
            }),
            price_bid: Some(149.0),
            price_ask: Some(151.0),
            size_bid: Some("100".into()),
            size_ask: Some("200".into()),
        }],
        is_done: Some(true),
    };

    let (ticks, done) = decode_historical_ticks_bid_ask_proto(&proto_msg.encode_to_vec()).unwrap();
    assert_eq!(ticks.len(), 1);
    assert!(done);
    assert_eq!(ticks[0].timestamp, datetime!(2023-04-10 13:30:00 UTC));
    assert!(ticks[0].tick_attribute_bid_ask.bid_past_low);
    assert!(!ticks[0].tick_attribute_bid_ask.ask_past_high);
    assert_eq!(ticks[0].price_bid, 149.0);
    assert_eq!(ticks[0].price_ask, 151.0);
    assert_eq!(ticks[0].size_bid, Some(100.0));
    assert_eq!(ticks[0].size_ask, Some(200.0));
}

#[test]
fn test_decode_histogram_data_proto() {
    let proto_msg = crate::proto::HistogramData {
        req_id: Some(1),
        histogram_data_entries: vec![
            crate::proto::HistogramDataEntry {
                price: Some(100.5),
                size: Some("50".into()),
            },
            crate::proto::HistogramDataEntry {
                price: Some(101.0),
                size: Some("75".into()),
            },
        ],
    };

    let result = decode_histogram_data_proto(proto_msg).unwrap();
    assert_eq!(result.len(), 2);
    assert_eq!(result[0].price, 100.5);
    assert_eq!(result[0].size, Some(50.0));
    assert_eq!(result[1].price, 101.0);
    assert_eq!(result[1].size, Some(75.0));
}

#[test]
fn test_decode_historical_data_end_proto() {
    // Wire format for start/end uses "YYYYMMDD HH:MM:SS TZ".
    let proto_msg = crate::proto::HistoricalDataEnd {
        req_id: Some(1),
        start_date_str: Some("20260101 09:30:00 US/Eastern".into()),
        end_date_str: Some("20260105 16:00:00 US/Eastern".into()),
    };

    let (start, end) = decode_historical_data_end_proto(&proto_msg.encode_to_vec()).unwrap();
    assert!(start < end);
    assert_eq!(start.year(), 2026);
    assert_eq!(end.year(), 2026);
}

#[test]
fn test_decode_historical_schedule_proto() {
    let proto_msg = crate::proto::HistoricalSchedule {
        req_id: Some(1),
        start_date_time: Some("20260101-09:30:00".into()),
        end_date_time: Some("20260105-16:00:00".into()),
        time_zone: Some("US/Eastern".into()),
        historical_sessions: vec![crate::proto::HistoricalSession {
            start_date_time: Some("20260102-09:30:00".into()),
            end_date_time: Some("20260102-16:00:00".into()),
            ref_date: Some("20260102".into()),
        }],
    };

    let result = decode_historical_schedule_proto(proto_msg).unwrap();
    assert_eq!(result.time_zone, "US/Eastern");
    assert_eq!(result.sessions.len(), 1);
    assert_eq!(result.sessions[0].reference, date!(2026 - 01 - 02));
}

#[test]
fn test_decode_historical_schedule_unknown_timezone_errors() {
    // Gateway sends an unmappable timezone — must surface as Error::UnsupportedTimeZone.
    let proto_msg = crate::proto::HistoricalSchedule {
        req_id: Some(1),
        start_date_time: Some("20230414-09:30:00".into()),
        end_date_time: Some("20230414-16:00:00".into()),
        time_zone: Some("Bogus Standard Time".into()),
        historical_sessions: vec![],
    };

    let err = decode_historical_schedule_proto(proto_msg).expect_err("unknown tz must error");
    assert!(matches!(err, Error::UnsupportedTimeZone(ref name) if name == "Bogus Standard Time"));
    let rendered = err.to_string();
    assert!(rendered.contains("Bogus Standard Time"), "missing tz name: {rendered}");
    assert!(
        rendered.contains("register_timezone_alias"),
        "missing programmatic-fix pointer: {rendered}"
    );
    assert!(rendered.contains("IBAPI_TIMEZONE_ALIASES"), "missing env-var pointer: {rendered}");
}

#[test]
fn test_decode_historical_data_update_proto() {
    let proto_msg = crate::proto::HistoricalDataUpdate {
        req_id: Some(1),
        historical_data_bar: Some(crate::proto::HistoricalDataBar {
            date: Some("1681133400".into()), // unix timestamp
            open: Some(150.0),
            high: Some(151.0),
            low: Some(149.5),
            close: Some(150.75),
            volume: Some("1000".into()),
            wap: Some("150.5".into()),
            bar_count: Some(42),
        }),
    };

    let result = decode_historical_data_update_proto(&proto_msg.encode_to_vec()).unwrap();
    assert_eq!(result.open, 150.0);
    assert_eq!(result.high, 151.0);
    assert_eq!(result.low, 149.5);
    assert_eq!(result.close, 150.75);
    assert_eq!(result.volume, 1000.0);
    assert_eq!(result.wap, 150.5);
    assert_eq!(result.count, 42);
    assert_eq!(result.date, BarTimestamp::DateTime(datetime!(2023-04-10 13:30:00 UTC)));
}

#[test]
fn test_decode_historical_data_update_proto_missing_bar_defaults() {
    let proto_msg = crate::proto::HistoricalDataUpdate {
        req_id: Some(1),
        historical_data_bar: None,
    };

    let result = decode_historical_data_update_proto(&proto_msg.encode_to_vec()).unwrap();
    assert_eq!(result.count, 0);
    assert_eq!(result.date, BarTimestamp::DateTime(OffsetDateTime::UNIX_EPOCH));
}

// ---------------------------------------------------------------------------
// Public-wrapper guards. Each `decode_X(message)` rejects text framing with
// `Error::UnexpectedWireFormat` via `require_proto()` — scanner precedent #532.
// ---------------------------------------------------------------------------

#[test]
fn test_decode_historical_data_rejects_text_framing() {
    assert_rejects_text_framing(
        IncomingMessages::HistoricalData,
        "17\09000\01\020230413\0182.94\0186.50\0180.94\0185.90\0948837.22\0184.869\0324891\0",
        decode_historical_data,
    );
}

#[test]
fn test_decode_historical_data_end_rejects_text_framing() {
    assert_rejects_text_framing(
        IncomingMessages::HistoricalDataEnd,
        "108\09000\020230315 09:30:00 UTC\020230315 10:30:00 UTC\0",
        decode_historical_data_end,
    );
}

#[test]
fn test_decode_historical_data_update_rejects_text_framing() {
    assert_rejects_text_framing(
        IncomingMessages::HistoricalDataUpdate,
        "90\09000\0-1\01681133400\0185.50\0186.00\0185.00\0185.75\01000.5\0185.625\0150\0",
        decode_historical_data_update,
    );
}

#[test]
fn test_decode_historical_ticks_mid_point_rejects_text_framing() {
    assert_rejects_text_framing(
        IncomingMessages::HistoricalTick,
        "96\09000\01\01681133398\00\091.36\00\01\0",
        decode_historical_ticks_mid_point,
    );
}

#[test]
fn test_decode_historical_ticks_bid_ask_rejects_text_framing() {
    assert_rejects_text_framing(
        IncomingMessages::HistoricalTickBidAsk,
        "97\09000\01\01681133399\00\011.63\011.83\02800\0100\01\0",
        decode_historical_ticks_bid_ask,
    );
}

#[test]
fn test_decode_historical_ticks_last_rejects_text_framing() {
    assert_rejects_text_framing(
        IncomingMessages::HistoricalTickLast,
        "98\09000\01\01681133400\00\011.63\024547\0ISLAND\0 O X\01\0",
        decode_historical_ticks_last,
    );
}

// === decimal wire fields are routed through parse_optional_decimal (issue #716) ===
//
// Bar volume and wap are still plain f64; the tick and histogram sizes below
// are Option<f64>.

/// The builder stringifies an f64, so a malformed wire value needs raw proto.
fn historical_data_bytes(volume: &str) -> Vec<u8> {
    crate::proto::HistoricalData {
        req_id: Some(9000),
        historical_data_bars: vec![crate::proto::HistoricalDataBar {
            date: Some("20230101".into()),
            open: Some(100.0),
            high: Some(101.0),
            low: Some(99.0),
            close: Some(100.5),
            volume: Some(volume.into()),
            wap: Some("100.25".into()),
            bar_count: Some(10),
        }],
    }
    .encode_to_vec()
}

#[test]
fn test_decode_historical_data_proto_rejects_malformed_volume() {
    assert_decimal_parse_error(super::decode_historical_data_proto(&historical_data_bytes("abc")), "abc");
}

#[test]
fn test_decode_historical_data_proto_preserves_fractional_volume() {
    let bytes = historical_data_response()
        .bar(
            historical_data_daily_bar("20230101")
                .ohlc(100.0, 101.0, 99.0, 100.5)
                .volume(0.5)
                .wap(100.25)
                .count(10),
        )
        .encode_proto();

    let bars = super::decode_historical_data_proto(&bytes).unwrap();
    assert_eq!(bars[0].volume, 0.5);
}

#[test]
fn test_decode_historical_data_update_proto_rejects_malformed_volume() {
    let bytes = crate::proto::HistoricalDataUpdate {
        req_id: Some(9000),
        historical_data_bar: Some(crate::proto::HistoricalDataBar {
            date: Some("20230101".into()),
            volume: Some("abc".into()),
            ..Default::default()
        }),
    }
    .encode_to_vec();

    assert_decimal_parse_error(super::decode_historical_data_update_proto(&bytes), "abc");
}

// === tick and histogram sizes preserve fractional wire values (issue #716) ===
//
// These are the regression the issue reports: under the old `i32` typing a wire
// value of "0.5" failed `parse::<i32>()` and silently decoded as 0.

#[test]
fn test_decode_historical_ticks_proto_preserves_fractional_size() {
    let bytes = historical_ticks_response()
        .tick(historical_tick_mid(1_681_133_400, 150.0, 0.5))
        .encode_proto();

    let (ticks, _) = super::decode_historical_ticks_proto(&bytes).unwrap();
    assert_eq!(ticks[0].size, Some(0.5));
}

#[test]
fn test_decode_historical_ticks_last_proto_preserves_fractional_size() {
    let bytes = historical_ticks_last_response()
        .tick(historical_tick_last(1_681_133_400, 150.0, 0.25, "NYSE"))
        .encode_proto();

    let (ticks, _) = super::decode_historical_ticks_last_proto(&bytes).unwrap();
    assert_eq!(ticks[0].size, Some(0.25));
}

#[test]
fn test_decode_historical_ticks_bid_ask_proto_preserves_fractional_sizes() {
    let bytes = historical_ticks_bid_ask_response()
        .tick(historical_tick_bid_ask(1_681_133_400, 149.0, 151.0, 0.5, 0.75))
        .encode_proto();

    let (ticks, _) = super::decode_historical_ticks_bid_ask_proto(&bytes).unwrap();
    assert_eq!(ticks[0].size_bid, Some(0.5));
    assert_eq!(ticks[0].size_ask, Some(0.75));
}

#[test]
fn test_decode_histogram_data_proto_preserves_fractional_size() {
    let bytes = histogram_data_response().entry(histogram_entry(125.5, 0.5)).encode_proto();

    let entries = super::decode_histogram_data_proto(prost::Message::decode(&bytes[..]).expect("fixture must decode")).unwrap();
    assert_eq!(entries[0].size, Some(0.5));
}

// --- malformed sizes ---

#[test]
fn test_decode_histogram_data_proto_rejects_malformed_size() {
    let bytes = histogram_data_response()
        .entry(histogram_entry(125.5, 0.0).size_wire(Some("abc")))
        .encode_proto();

    assert_decimal_parse_error(
        super::decode_histogram_data_proto(prost::Message::decode(&bytes[..]).expect("fixture must decode")),
        "abc",
    );
}

#[test]
fn test_decode_historical_ticks_proto_rejects_malformed_size() {
    let bytes = historical_ticks_response()
        .tick(historical_tick_mid(1_681_133_400, 150.0, 0.0).size_wire(Some("abc")))
        .encode_proto();

    assert_decimal_parse_error(super::decode_historical_ticks_proto(&bytes), "abc");
}

/// `HistoricalDataEnd` echoes the requested window, and the schedule decoder
/// carries the same wall-clock shape; an edge inside a DST fold or gap used to
/// panic in `OffsetResult::unwrap`. The fold/gap policy itself is pinned in
/// `common::timezone` tests; this checks both entry points reach it.
#[test]
fn test_window_edge_in_dst_fold_or_gap_decodes() {
    // The live input from #790 (2026-08-29 04:40Z, NVDA daily bars, 300 days,
    // keepUpToDate): IB echoed the window start 300 days back, inside the
    // 2025-11-02 fold hour.
    let proto_msg = crate::proto::HistoricalDataEnd {
        req_id: Some(1),
        start_date_str: Some("20251102 01:40:26 US/Eastern".into()),
        end_date_str: Some("20260829 04:33:35 US/Eastern".into()),
    };
    let (start, end) = decode_historical_data_end_proto(&proto_msg.encode_to_vec()).expect("fold resolves");
    assert_eq!(start, datetime!(2025-11-02 05:40:26 UTC), "fold takes the earlier (EDT) offset");
    assert_eq!(end, datetime!(2026-08-29 08:33:35 UTC));

    // A gap reading is pushed forward, not rejected — rejecting it discarded the bars.
    let gap = super::parse_date_with_tz("20260308 02:30:00 US/Eastern").expect("gap resolves");
    assert_eq!(gap, datetime!(2026-03-08 07:30:00 UTC));

    let tz = time_tz::timezones::get_by_name("US/Eastern").expect("tz");
    let fold = super::parse_schedule_date_time("20251102-01:30:00", tz).expect("fold resolves");
    assert_eq!(fold, datetime!(2025-11-02 05:30:00 UTC));
    let gap = super::parse_schedule_date_time("20260308-02:30:00", tz).expect("gap resolves");
    assert_eq!(gap, datetime!(2026-03-08 07:30:00 UTC));
}