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
//! Domain types for the accounts module

use std::fmt;
use std::ops::Deref;

/// Account identifier
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AccountId(pub String);

impl Deref for AccountId {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for AccountId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for AccountId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for AccountId {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

/// Model code identifier
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ModelCode(pub String);

impl Deref for ModelCode {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for ModelCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for ModelCode {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for ModelCode {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

/// Contract identifier
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ContractId(pub i32);

impl ContractId {
    /// Get the inner value
    pub fn value(&self) -> i32 {
        self.0
    }
}

impl fmt::Display for ContractId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<i32> for ContractId {
    fn from(id: i32) -> Self {
        Self(id)
    }
}

// pub struct ModelCode(pub String);

/// Account group for filtering
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AccountGroup(pub String);

impl AccountGroup {
    /// Convert to string representation for API calls
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for AccountGroup {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl From<&str> for AccountGroup {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl From<String> for AccountGroup {
    fn from(s: String) -> Self {
        Self(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod account_id {
        use super::*;

        #[test]
        fn test_new() {
            let id = AccountId("U123456".to_string());
            assert_eq!(id.0, "U123456");
        }

        #[test]
        fn test_deref() {
            let id = AccountId("U123456".to_string());
            assert_eq!(&*id, "U123456");
            assert_eq!(id.len(), 7);
            assert!(id.starts_with("U"));
        }

        #[test]
        fn test_display() {
            let id = AccountId("U123456".to_string());
            assert_eq!(format!("{}", id), "U123456");
        }

        #[test]
        fn test_from_string() {
            let id = AccountId::from("U123456".to_string());
            assert_eq!(id.0, "U123456");
        }

        #[test]
        fn test_from_str() {
            let id = AccountId::from("U123456");
            assert_eq!(id.0, "U123456");
        }

        #[test]
        fn test_empty_account_id() {
            let id = AccountId::from("");
            assert_eq!(id.0, "");
            assert_eq!(id.len(), 0);
        }

        #[test]
        fn test_special_characters() {
            let id = AccountId::from("U-123_456.789");
            assert_eq!(id.0, "U-123_456.789");
        }

        #[test]
        fn test_equality() {
            let id1 = AccountId::from("U123456");
            let id2 = AccountId::from("U123456");
            let id3 = AccountId::from("U654321");

            assert_eq!(id1, id2);
            assert_ne!(id1, id3);
        }

        #[test]
        fn test_hash() {
            use std::collections::HashSet;

            let mut set = HashSet::new();
            set.insert(AccountId::from("U123456"));
            set.insert(AccountId::from("U123456"));
            set.insert(AccountId::from("U654321"));

            assert_eq!(set.len(), 2);
            assert!(set.contains(&AccountId::from("U123456")));
            assert!(set.contains(&AccountId::from("U654321")));
        }
    }

    mod model_code {
        use super::*;

        #[test]
        fn test_new() {
            let code = ModelCode("MODEL1".to_string());
            assert_eq!(code.0, "MODEL1");
        }

        #[test]
        fn test_deref() {
            let code = ModelCode("MODEL1".to_string());
            assert_eq!(&*code, "MODEL1");
            assert_eq!(code.len(), 6);
            assert!(code.contains("MODEL"));
        }

        #[test]
        fn test_display() {
            let code = ModelCode("MODEL1".to_string());
            assert_eq!(format!("{}", code), "MODEL1");
        }

        #[test]
        fn test_from_string() {
            let code = ModelCode::from("MODEL1".to_string());
            assert_eq!(code.0, "MODEL1");
        }

        #[test]
        fn test_from_str() {
            let code = ModelCode::from("MODEL1");
            assert_eq!(code.0, "MODEL1");
        }

        #[test]
        fn test_empty_model_code() {
            let code = ModelCode::from("");
            assert_eq!(code.0, "");
            assert!(code.is_empty());
        }

        #[test]
        fn test_equality() {
            let code1 = ModelCode::from("MODEL1");
            let code2 = ModelCode::from("MODEL1");
            let code3 = ModelCode::from("MODEL2");

            assert_eq!(code1, code2);
            assert_ne!(code1, code3);
        }

        #[test]
        fn test_hash() {
            use std::collections::HashSet;

            let mut set = HashSet::new();
            set.insert(ModelCode::from("MODEL1"));
            set.insert(ModelCode::from("MODEL1"));
            set.insert(ModelCode::from("MODEL2"));

            assert_eq!(set.len(), 2);
        }
    }

    mod contract_id {
        use super::*;

        #[test]
        fn test_new() {
            let id = ContractId(12345);
            assert_eq!(id.0, 12345);
        }

        #[test]
        fn test_value() {
            let id = ContractId(12345);
            assert_eq!(id.value(), 12345);
        }

        #[test]
        fn test_display() {
            let id = ContractId(12345);
            assert_eq!(format!("{}", id), "12345");
        }

        #[test]
        fn test_from_i32() {
            let id = ContractId::from(12345);
            assert_eq!(id.0, 12345);
        }

        #[test]
        fn test_zero_contract_id() {
            let id = ContractId(0);
            assert_eq!(id.value(), 0);
            assert_eq!(format!("{}", id), "0");
        }

        #[test]
        fn test_negative_contract_id() {
            let id = ContractId(-1);
            assert_eq!(id.value(), -1);
            assert_eq!(format!("{}", id), "-1");
        }

        #[test]
        fn test_max_contract_id() {
            let id = ContractId(i32::MAX);
            assert_eq!(id.value(), i32::MAX);
            assert_eq!(format!("{}", id), i32::MAX.to_string());
        }

        #[test]
        fn test_equality() {
            let id1 = ContractId(12345);
            let id2 = ContractId(12345);
            let id3 = ContractId(54321);

            assert_eq!(id1, id2);
            assert_ne!(id1, id3);
        }

        #[test]
        fn test_copy() {
            let id1 = ContractId(12345);
            let id2 = id1; // Copy
            assert_eq!(id1, id2);
            assert_eq!(id1.value(), 12345);
            assert_eq!(id2.value(), 12345);
        }

        #[test]
        fn test_hash() {
            use std::collections::HashSet;

            let mut set = HashSet::new();
            set.insert(ContractId(12345));
            set.insert(ContractId(12345));
            set.insert(ContractId(54321));

            assert_eq!(set.len(), 2);
        }
    }

    mod account_group {
        use super::*;

        #[test]
        fn test_new() {
            let group = AccountGroup("All".to_string());
            assert_eq!(group.0, "All");
        }

        #[test]
        fn test_as_str() {
            let group = AccountGroup("All".to_string());
            assert_eq!(group.as_str(), "All");
        }

        #[test]
        fn test_display() {
            let group = AccountGroup("All".to_string());
            assert_eq!(format!("{}", group), "All");
        }

        #[test]
        fn test_from_str() {
            let group = AccountGroup::from("All");
            assert_eq!(group.0, "All");
        }

        #[test]
        fn test_from_string() {
            let owned_string = "All".to_string();
            let group = AccountGroup::from(owned_string.clone());
            assert_eq!(group.0, "All");
            // Verify we're not unnecessarily cloning
            assert_eq!(group.0, owned_string);
        }

        #[test]
        fn test_empty_group() {
            let group = AccountGroup::from("");
            assert_eq!(group.as_str(), "");
        }

        #[test]
        fn test_special_group_names() {
            let group = AccountGroup::from("Group-1_Test.2024");
            assert_eq!(group.as_str(), "Group-1_Test.2024");
        }

        #[test]
        fn test_equality() {
            let group1 = AccountGroup::from("All");
            let group2 = AccountGroup::from("All");
            let group3 = AccountGroup::from("Group1");

            assert_eq!(group1, group2);
            assert_ne!(group1, group3);
        }
    }

    #[test]
    fn test_types_are_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}

        assert_send_sync::<AccountId>();
        assert_send_sync::<ModelCode>();
        assert_send_sync::<ContractId>();
        assert_send_sync::<AccountGroup>();
    }

    #[test]
    fn test_types_are_clone() {
        let account_id = AccountId("U123".to_string());
        let _ = account_id.clone();

        let model_code = ModelCode("MODEL".to_string());
        let _ = model_code.clone();

        let contract_id = ContractId(123);
        let _ = contract_id;

        let account_group = AccountGroup("All".to_string());
        let _ = account_group.clone();
    }

    #[test]
    fn test_types_are_debug() {
        let account_id = AccountId("U123".to_string());
        let debug_str = format!("{:?}", account_id);
        assert!(debug_str.contains("AccountId"));
        assert!(debug_str.contains("U123"));

        let model_code = ModelCode("MODEL".to_string());
        let debug_str = format!("{:?}", model_code);
        assert!(debug_str.contains("ModelCode"));
        assert!(debug_str.contains("MODEL"));

        let contract_id = ContractId(123);
        let debug_str = format!("{:?}", contract_id);
        assert!(debug_str.contains("ContractId"));
        assert!(debug_str.contains("123"));

        let account_group = AccountGroup("All".to_string());
        let debug_str = format!("{:?}", account_group);
        assert!(debug_str.contains("AccountGroup"));
        assert!(debug_str.contains("All"));
    }

    #[test]
    fn test_account_id_string_operations() {
        let id = AccountId::from("U123456");

        // Test various string operations via Deref
        assert_eq!(id.to_uppercase(), "U123456");
        assert_eq!(id.chars().count(), 7);
        assert!(id.contains("123"));
        assert_eq!(id.replace("U", "D"), "D123456");
    }

    #[test]
    fn test_model_code_string_operations() {
        let code = ModelCode::from("model_test");

        // Test various string operations via Deref
        assert_eq!(code.to_uppercase(), "MODEL_TEST");
        assert!(code.starts_with("model"));
        assert!(code.ends_with("test"));
        assert_eq!(code.split('_').count(), 2);
    }
}