ibapi 3.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
use super::*;
use crate::contracts::Symbol;
use crate::messages::ResponseMessage;
use crate::orders::{Action, OrderStatusKind};
use crate::server_versions;

#[test]
fn test_decode_open_order_proto() {
    use prost::Message;

    let proto_msg = crate::proto::OpenOrder {
        order_id: Some(42),
        contract: Some(crate::proto::Contract {
            con_id: Some(265598),
            symbol: Some("AAPL".into()),
            sec_type: Some("STK".into()),
            exchange: Some("SMART".into()),
            currency: Some("USD".into()),
            ..Default::default()
        }),
        order: Some(crate::proto::Order {
            order_id: Some(42),
            action: Some("BUY".into()),
            total_quantity: Some("100".into()),
            order_type: Some("LMT".into()),
            lmt_price: Some(150.0),
            ..Default::default()
        }),
        order_state: Some(crate::proto::OrderState {
            status: Some("Submitted".into()),
            ..Default::default()
        }),
    };

    let mut bytes = Vec::new();
    proto_msg.encode(&mut bytes).unwrap();

    let result = decode_open_order_proto(&bytes).unwrap();
    assert_eq!(result.order_id, 42);
    assert_eq!(result.contract.contract_id, 265598);
    assert_eq!(result.contract.symbol.to_string(), "AAPL");
    assert_eq!(result.order.order_id, 42);
    assert_eq!(result.order.action, Action::Buy);
    assert_eq!(result.order.total_quantity, 100.0);
    assert_eq!(result.order.order_type, "LMT");
    assert_eq!(result.order.limit_price, Some(150.0));
    assert_eq!(result.order_state.status, OrderStatusKind::Submitted);
}

#[test]
fn test_decode_order_status_proto() {
    use prost::Message;

    let proto_msg = crate::proto::OrderStatus {
        order_id: Some(99),
        status: Some("Filled".into()),
        filled: Some("50".into()),
        remaining: Some("0".into()),
        avg_fill_price: Some(152.5),
        perm_id: Some(123456),
        parent_id: Some(10),
        last_fill_price: Some(152.75),
        client_id: Some(7),
        why_held: Some("locate".into()),
        mkt_cap_price: Some(1.23),
    };

    let mut bytes = Vec::new();
    proto_msg.encode(&mut bytes).unwrap();

    let result = decode_order_status_proto(&bytes).unwrap();
    assert_eq!(result.order_id, 99);
    assert_eq!(result.status, OrderStatusKind::Filled);
    assert_eq!(result.filled, 50.0);
    assert_eq!(result.remaining, 0.0);
    assert_eq!(result.average_fill_price, Some(152.5));
    assert_eq!(result.perm_id, 123456);
    assert_eq!(result.parent_id, 10);
    assert_eq!(result.last_fill_price, Some(152.75));
    assert_eq!(result.client_id, 7);
    assert_eq!(result.why_held, "locate");
    assert_eq!(result.market_cap_price, Some(1.23));
}

#[test]
fn test_decode_order_status_proto_missing_doubles() {
    // Regression: previously decoded to Some(0.0) via unwrap_or_default().
    use prost::Message;

    let proto_msg = crate::proto::OrderStatus {
        order_id: Some(99),
        status: Some("Submitted".into()),
        filled: Some("0".into()),
        remaining: Some("100".into()),
        avg_fill_price: None,
        perm_id: Some(123456),
        parent_id: Some(0),
        last_fill_price: None,
        client_id: Some(7),
        why_held: None,
        mkt_cap_price: None,
    };

    let mut bytes = Vec::new();
    proto_msg.encode(&mut bytes).unwrap();

    let result = decode_order_status_proto(&bytes).unwrap();
    assert_eq!(result.average_fill_price, None);
    assert_eq!(result.last_fill_price, None);
    assert_eq!(result.market_cap_price, None);
}

#[test]
fn test_decode_order_status_proto_rejects_empty_status() {
    // Missing or empty status must error rather than silently defaulting to
    // Submitted; matches the text decoder which fails on empty status fields.
    use prost::Message;

    for status in [None, Some(String::new())] {
        let proto_msg = crate::proto::OrderStatus {
            order_id: Some(99),
            status,
            filled: Some("0".into()),
            remaining: Some("100".into()),
            avg_fill_price: None,
            perm_id: Some(1),
            parent_id: Some(0),
            last_fill_price: None,
            client_id: Some(0),
            why_held: None,
            mkt_cap_price: None,
        };

        let mut bytes = Vec::new();
        proto_msg.encode(&mut bytes).unwrap();

        assert!(matches!(decode_order_status_proto(&bytes), Err(crate::Error::Parse(..))));
    }
}

#[test]
fn test_decode_commission_report_proto() {
    use prost::Message;

    let proto_msg = crate::proto::CommissionAndFeesReport {
        exec_id: Some("exec123".into()),
        commission_and_fees: Some(1.25),
        currency: Some("USD".into()),
        realized_pnl: Some(500.0),
        bond_yield: Some(f64::MAX),
        yield_redemption_date: Some("20260101".into()),
    };

    let mut bytes = Vec::new();
    proto_msg.encode(&mut bytes).unwrap();

    let result = decode_commission_report_proto(&bytes).unwrap();
    assert_eq!(result.execution_id, "exec123");
    assert_eq!(result.commission, 1.25);
    assert_eq!(result.currency, "USD");
    assert_eq!(result.realized_pnl, Some(500.0));
    assert_eq!(result.yields, None); // f64::MAX filtered out
    assert_eq!(result.yield_redemption_date, "20260101");
}

#[test]
fn test_decode_execution_data_proto() {
    use prost::Message;

    let proto_msg = crate::proto::ExecutionDetails {
        req_id: Some(42),
        contract: Some(crate::proto::Contract {
            con_id: Some(265598),
            symbol: Some("AAPL".into()),
            sec_type: Some("STK".into()),
            ..Default::default()
        }),
        execution: Some(crate::proto::Execution {
            order_id: Some(100),
            exec_id: Some("exec001".into()),
            time: Some("20260101 12:00:00".into()),
            acct_number: Some("DU1234".into()),
            side: Some("BOT".into()),
            shares: Some("50".into()),
            price: Some(152.5),
            perm_id: Some(99999),
            ..Default::default()
        }),
    };

    let mut bytes = Vec::new();
    proto_msg.encode(&mut bytes).unwrap();

    let result = decode_execution_data_proto(&bytes).unwrap();
    assert_eq!(result.request_id, 42);
    assert_eq!(result.contract.contract_id, 265598);
    assert_eq!(result.execution.execution_id, "exec001");
    assert_eq!(result.execution.shares, 50.0);
    assert_eq!(result.execution.price, 152.5);
    assert_eq!(result.execution.perm_id, 99999);
    assert_eq!(result.execution.side, crate::orders::ExecutionSide::Bought);
}

#[test]
fn test_decode_completed_order_proto() {
    use prost::Message;

    let proto_msg = crate::proto::CompletedOrder {
        contract: Some(crate::proto::Contract {
            con_id: Some(265598),
            symbol: Some("AAPL".into()),
            sec_type: Some("STK".into()),
            ..Default::default()
        }),
        order: Some(crate::proto::Order {
            order_id: Some(200),
            action: Some("SELL".into()),
            total_quantity: Some("200".into()),
            order_type: Some("MKT".into()),
            ..Default::default()
        }),
        order_state: Some(crate::proto::OrderState {
            status: Some("Filled".into()),
            completed_time: Some("20260101 12:00:00".into()),
            completed_status: Some("Filled".into()),
            ..Default::default()
        }),
    };

    let mut bytes = Vec::new();
    proto_msg.encode(&mut bytes).unwrap();

    let result = decode_completed_order_proto(&bytes).unwrap();
    // Completed orders always report `order_id = -1` (legacy text-decoder sentinel).
    assert_eq!(result.order_id, -1);
    assert_eq!(result.order.order_id, 200);
    assert_eq!(result.contract.contract_id, 265598);
    assert_eq!(result.contract.symbol, Symbol::from("AAPL"));
    assert_eq!(result.order.action, Action::Sell);
    assert_eq!(result.order_state.completed_time, "20260101 12:00:00");
}

// =============================================================================
// Builder → production-decoder integration tests
// =============================================================================

#[test]
fn test_decode_open_order_proto_round_trips_via_builder() {
    use crate::testdata::builders::orders::open_order;
    use crate::testdata::builders::ResponseProtoEncoder;

    let bytes = open_order()
        .order_id(42)
        .contract_id(265598)
        .symbol("AAPL")
        .security_type("STK")
        .order_type("LMT")
        .limit_price(Some(150.0))
        .status(OrderStatusKind::Submitted)
        .encode_proto();

    let result = super::decode_open_order_proto(&bytes).unwrap();
    assert_eq!(result.order_id, 42);
    assert_eq!(result.contract.contract_id, 265598);
    assert_eq!(result.contract.symbol, Symbol::from("AAPL"));
    assert_eq!(result.order.action, Action::Buy);
    assert_eq!(result.order.order_type, "LMT");
    assert_eq!(result.order.limit_price, Some(150.0));
    assert_eq!(result.order_state.status, OrderStatusKind::Submitted);
}

#[test]
fn test_decode_completed_order_proto_round_trips_via_builder() {
    use crate::testdata::builders::orders::completed_order;
    use crate::testdata::builders::ResponseProtoEncoder;

    let bytes = completed_order()
        .symbol("AAPL")
        .security_type("STK")
        .total_quantity(100.0)
        .completed_time("20260101 12:00:00")
        .completed_status("Filled by Trader")
        .encode_proto();

    let result = super::decode_completed_order_proto(&bytes).unwrap();
    assert_eq!(result.contract.symbol, Symbol::from("AAPL"));
    assert_eq!(result.order.total_quantity, 100.0);
    assert_eq!(result.order_state.status, OrderStatusKind::Filled);
    assert_eq!(result.order_state.completed_time, "20260101 12:00:00");
    assert_eq!(result.order_state.completed_status, "Filled by Trader");
}

#[test]
fn test_decode_order_status_proto_round_trips_via_builder() {
    use crate::testdata::builders::orders::order_status;
    use crate::testdata::builders::ResponseProtoEncoder;

    let bytes = order_status()
        .order_id(99)
        .status(OrderStatusKind::Filled)
        .filled(50.0)
        .remaining(0.0)
        .average_fill_price(Some(152.5))
        .perm_id(123456)
        .last_fill_price(Some(152.75))
        .client_id(7)
        .market_cap_price(Some(1.23))
        .encode_proto();

    let result = super::decode_order_status_proto(&bytes).unwrap();
    assert_eq!(result.order_id, 99);
    assert_eq!(result.status, OrderStatusKind::Filled);
    assert_eq!(result.filled, 50.0);
    assert_eq!(result.remaining, 0.0);
    assert_eq!(result.average_fill_price, Some(152.5));
    assert_eq!(result.perm_id, 123456);
    assert_eq!(result.last_fill_price, Some(152.75));
    assert_eq!(result.client_id, 7);
    assert_eq!(result.market_cap_price, Some(1.23));
}

#[test]
fn test_decode_commission_report_proto_round_trips_via_builder() {
    use crate::testdata::builders::orders::commission_report;
    use crate::testdata::builders::ResponseProtoEncoder;

    let bytes = commission_report()
        .execution_id("exec123")
        .commission(1.25)
        .currency("USD")
        .realized_pnl(Some(500.0))
        .yields(Some(f64::MAX))
        .encode_proto();

    let result = super::decode_commission_report_proto(&bytes).unwrap();
    assert_eq!(result.execution_id, "exec123");
    assert_eq!(result.commission, 1.25);
    assert_eq!(result.currency, "USD");
    assert_eq!(result.realized_pnl, Some(500.0));
    assert_eq!(result.yields, None); // f64::MAX is the IBKR sentinel for "unset"
}

#[test]
fn test_decode_execution_data_proto_round_trips_via_builder() {
    use crate::testdata::builders::orders::execution_data;
    use crate::testdata::builders::ResponseProtoEncoder;

    let bytes = execution_data()
        .request_id(42)
        .order_id(100)
        .contract_id(265598)
        .symbol("AAPL")
        .security_type("STK")
        .execution_id("exec001")
        .side("BOT")
        .shares(50.0)
        .price(152.5)
        .perm_id(99999)
        .encode_proto();

    let result = super::decode_execution_data_proto(&bytes).unwrap();
    assert_eq!(result.request_id, 42);
    assert_eq!(result.contract.contract_id, 265598);
    assert_eq!(result.execution.execution_id, "exec001");
    assert_eq!(result.execution.shares, 50.0);
    assert_eq!(result.execution.price, 152.5);
    assert_eq!(result.execution.perm_id, 99999);
    assert_eq!(result.execution.side, crate::orders::ExecutionSide::Bought);
}

// =============================================================================
// Text-framing rejection (rule 20)
// =============================================================================
//
// Servers ≥ the connection floor always emit these messages in
// proto framing. Text-framed arrival skip-classifies via UnexpectedResponse
// rather than terminating the subscription.

#[test]
fn test_decode_open_order_rejects_text_framing() {
    let _ = server_versions::PROTOBUF_REST_MESSAGES_3;
    let mut message = ResponseMessage::from("5\013\076792991\0AAPL\0STK\0");
    let err = decode_open_order(&mut message).expect_err("text framing must be rejected");
    assert!(
        matches!(err, Error::UnexpectedResponse(_)),
        "expected Error::UnexpectedResponse, got {err:?}"
    );
}

#[test]
fn test_decode_completed_order_rejects_text_framing() {
    let mut message = ResponseMessage::from("101\0265598\0AAPL\0STK\0");
    let err = decode_completed_order(&mut message).expect_err("text framing must be rejected");
    assert!(
        matches!(err, Error::UnexpectedResponse(_)),
        "expected Error::UnexpectedResponse, got {err:?}"
    );
}

#[test]
fn test_decode_execution_data_rejects_text_framing() {
    let mut message = ResponseMessage::from("11\09000\042\0265598\0AAPL\0STK\0");
    let err = decode_execution_data(&mut message).expect_err("text framing must be rejected");
    assert!(
        matches!(err, Error::UnexpectedResponse(_)),
        "expected Error::UnexpectedResponse, got {err:?}"
    );
}

#[test]
fn test_decode_commission_report_rejects_text_framing() {
    let mut message = ResponseMessage::from("59\01\0exec001\02.5\0USD\0");
    let err = decode_commission_report(&mut message).expect_err("text framing must be rejected");
    assert!(
        matches!(err, Error::UnexpectedResponse(_)),
        "expected Error::UnexpectedResponse, got {err:?}"
    );
}

#[test]
fn test_decode_order_status_rejects_text_framing() {
    let mut message = ResponseMessage::from("3\013\0PreSubmitted\00\0100\00.0\01376327563\00\00.0\0100\0\00.0\0");
    let err = decode_order_status(&mut message).expect_err("text framing must be rejected");
    assert!(
        matches!(err, Error::UnexpectedResponse(_)),
        "expected Error::UnexpectedResponse, got {err:?}"
    );
}