ibapi 2.11.2

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
591
592
593
594
595
596
597
598
599
600
601
602
//! Synchronous builder implementations

use std::marker::PhantomData;
use std::sync::Arc;

use crate::client::sync::Client;
use crate::client::StreamDecoder;
use crate::errors::Error;
use crate::messages::{OutgoingMessages, RequestMessage};
use crate::subscriptions::sync::Subscription;
use crate::subscriptions::DecoderContext;
use crate::transport::InternalSubscription;

/// Builder for creating requests with IDs
#[allow(dead_code)]
pub(crate) struct RequestBuilder<'a> {
    client: &'a Client,
    request_id: i32,
}

#[allow(dead_code)]
impl<'a> RequestBuilder<'a> {
    /// Create a new request builder with an auto-generated request ID
    pub fn new(client: &'a Client) -> Self {
        Self {
            client,
            request_id: client.next_request_id(),
        }
    }

    /// Create a new request builder with a specific request ID
    pub fn with_id(client: &'a Client, request_id: i32) -> Self {
        Self { client, request_id }
    }

    /// Get the request ID
    pub fn request_id(&self) -> i32 {
        self.request_id
    }

    /// Check server version requirement
    pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
        self.client.check_server_version(required_version, feature)?;
        Ok(self)
    }

    /// Send the request and create a subscription
    pub fn send<T>(self, message: RequestMessage) -> Result<Subscription<T>, Error>
    where
        T: StreamDecoder<T>,
    {
        SubscriptionBuilder::new(self.client).send_with_request_id(self.request_id, message)
    }

    /// Send the request and create a subscription with context
    pub fn send_with_context<T>(self, message: RequestMessage, context: DecoderContext) -> Result<Subscription<T>, Error>
    where
        T: StreamDecoder<T>,
    {
        SubscriptionBuilder::new(self.client)
            .with_context(context)
            .send_with_request_id(self.request_id, message)
    }

    /// Send the request without creating a subscription
    pub fn send_raw(self, message: RequestMessage) -> Result<InternalSubscription, Error> {
        self.client.send_request(self.request_id, message)
    }
}

/// Builder for creating shared channel requests (without request IDs)
#[allow(dead_code)]
pub(crate) struct SharedRequestBuilder<'a> {
    client: &'a Client,
    message_type: OutgoingMessages,
}

#[allow(dead_code)]
impl<'a> SharedRequestBuilder<'a> {
    /// Create a new shared request builder
    pub fn new(client: &'a Client, message_type: OutgoingMessages) -> Self {
        Self { client, message_type }
    }

    /// Check server version requirement
    pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
        self.client.check_server_version(required_version, feature)?;
        Ok(self)
    }

    /// Send the request and create a subscription
    pub fn send<T>(self, message: RequestMessage) -> Result<Subscription<T>, Error>
    where
        T: StreamDecoder<T>,
    {
        SubscriptionBuilder::new(self.client).send_shared(self.message_type, message)
    }

    /// Send the request and create a subscription with context
    pub fn send_with_context<T>(self, message: RequestMessage, context: DecoderContext) -> Result<Subscription<T>, Error>
    where
        T: StreamDecoder<T>,
    {
        SubscriptionBuilder::new(self.client)
            .with_context(context)
            .send_shared(self.message_type, message)
    }

    /// Send the request without creating a subscription
    pub fn send_raw(self, message: RequestMessage) -> Result<InternalSubscription, Error> {
        self.client.send_shared_request(self.message_type, message)
    }
}

/// Builder for creating order requests
#[allow(dead_code)]
pub(crate) struct OrderRequestBuilder<'a> {
    client: &'a Client,
    order_id: i32,
}

#[allow(dead_code)]
impl<'a> OrderRequestBuilder<'a> {
    /// Create a new order request builder with an auto-generated order ID
    pub fn new(client: &'a Client) -> Self {
        Self {
            client,
            order_id: client.next_order_id(),
        }
    }

    /// Create a new order request builder with a specific order ID
    pub fn with_id(client: &'a Client, order_id: i32) -> Self {
        Self { client, order_id }
    }

    /// Get the order ID
    pub fn order_id(&self) -> i32 {
        self.order_id
    }

    /// Check server version requirement
    pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
        self.client.check_server_version(required_version, feature)?;
        Ok(self)
    }

    /// Send the order request
    pub fn send(self, message: RequestMessage) -> Result<InternalSubscription, Error> {
        self.client.send_order(self.order_id, message)
    }
}

/// Builder for simple message sends (no response expected)
#[allow(dead_code)]
pub(crate) struct MessageBuilder<'a> {
    client: &'a Client,
}

#[allow(dead_code)]
impl<'a> MessageBuilder<'a> {
    /// Create a new message builder
    pub fn new(client: &'a Client) -> Self {
        Self { client }
    }

    /// Check server version requirement
    pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
        self.client.check_server_version(required_version, feature)?;
        Ok(self)
    }

    /// Send the message
    pub fn send(self, message: RequestMessage) -> Result<(), Error> {
        self.client.send_message(message)
    }
}

/// Builder for creating subscriptions with consistent patterns
#[allow(dead_code)]
pub(crate) struct SubscriptionBuilder<'a, T> {
    client: &'a Client,
    context: DecoderContext,
    _phantom: PhantomData<T>,
}

#[allow(dead_code)]
impl<'a, T> SubscriptionBuilder<'a, T>
where
    T: StreamDecoder<T>,
{
    /// Creates a new subscription builder
    pub fn new(client: &'a Client) -> Self {
        Self {
            client,
            context: client.decoder_context(),
            _phantom: PhantomData,
        }
    }

    /// Sets the response context for special handling
    pub fn with_context(mut self, context: DecoderContext) -> Self {
        self.context = context;
        self
    }

    /// Sets smart depth flag in the context
    pub fn with_smart_depth(mut self, is_smart_depth: bool) -> Self {
        self.context.is_smart_depth = is_smart_depth;
        self
    }

    /// Builds a subscription from an internal subscription (already sent)
    pub fn build(self, subscription: InternalSubscription) -> Subscription<T> {
        Subscription::new(Arc::clone(&self.client.message_bus), subscription, self.context)
    }

    /// Sends a request with a specific request ID and builds the subscription
    pub fn send_with_request_id(self, request_id: i32, message: RequestMessage) -> Result<Subscription<T>, Error> {
        let subscription = self.client.send_request(request_id, message)?;
        Ok(self.build(subscription))
    }

    /// Sends a shared request (no ID) and builds the subscription
    pub fn send_shared(self, message_type: OutgoingMessages, message: RequestMessage) -> Result<Subscription<T>, Error> {
        let subscription = self.client.send_shared_request(message_type, message)?;
        Ok(self.build(subscription))
    }

    /// Sends an order request and builds the subscription
    pub fn send_order(self, order_id: i32, message: RequestMessage) -> Result<Subscription<T>, Error> {
        let subscription = self.client.send_order(order_id, message)?;
        Ok(self.build(subscription))
    }
}

/// Extension trait to add builder methods to Client
#[allow(dead_code)]
pub(crate) trait ClientRequestBuilders {
    /// Create a request builder with an auto-generated request ID
    fn request(&self) -> RequestBuilder<'_>;

    /// Create a request builder with a specific request ID
    fn request_with_id(&self, request_id: i32) -> RequestBuilder<'_>;

    /// Create a shared request builder
    fn shared_request(&self, message_type: OutgoingMessages) -> SharedRequestBuilder<'_>;

    /// Create an order request builder
    fn order_request(&self) -> OrderRequestBuilder<'_>;

    /// Create an order request builder with a specific order ID
    fn order_request_with_id(&self, order_id: i32) -> OrderRequestBuilder<'_>;

    /// Create a simple message builder
    fn message(&self) -> MessageBuilder<'_>;
}

#[allow(dead_code)]
impl ClientRequestBuilders for Client {
    fn request(&self) -> RequestBuilder<'_> {
        RequestBuilder::new(self)
    }

    fn request_with_id(&self, request_id: i32) -> RequestBuilder<'_> {
        RequestBuilder::with_id(self, request_id)
    }

    fn shared_request(&self, message_type: OutgoingMessages) -> SharedRequestBuilder<'_> {
        SharedRequestBuilder::new(self, message_type)
    }

    fn order_request(&self) -> OrderRequestBuilder<'_> {
        OrderRequestBuilder::new(self)
    }

    fn order_request_with_id(&self, order_id: i32) -> OrderRequestBuilder<'_> {
        OrderRequestBuilder::with_id(self, order_id)
    }

    fn message(&self) -> MessageBuilder<'_> {
        MessageBuilder::new(self)
    }
}

/// Extension trait to add subscription builder to Client
pub(crate) trait SubscriptionBuilderExt {
    /// Creates a new subscription builder
    fn subscription<T>(&self) -> SubscriptionBuilder<'_, T>
    where
        T: StreamDecoder<T>;
}

impl SubscriptionBuilderExt for Client {
    fn subscription<T>(&self) -> SubscriptionBuilder<'_, T>
    where
        T: StreamDecoder<T>,
    {
        SubscriptionBuilder::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::common::mocks::MockGateway;
    use crate::client::common::tests::setup_connect;
    use crate::market_data::realtime::Bar;
    use crate::messages::OutgoingMessages;
    use crate::subscriptions::DecoderContext;

    fn create_test_client() -> (Client, MockGateway) {
        let gateway = setup_connect();
        let address = gateway.address();
        let client = Client::connect(&address, 100).expect("Client connection should succeed");
        (client, gateway)
    }

    #[test]
    fn test_request_builder_new() {
        let (client, _gateway) = create_test_client();
        let builder = RequestBuilder::new(&client);
        assert!(builder.request_id > 0);
    }

    #[test]
    fn test_request_builder_with_id() {
        let (client, _gateway) = create_test_client();
        let request_id = 42;
        let builder = RequestBuilder::with_id(&client, request_id);
        assert_eq!(builder.request_id(), request_id);
    }

    #[test]
    fn test_request_builder_check_version_success() {
        let (client, _gateway) = create_test_client();
        let builder = RequestBuilder::new(&client);
        let result = builder.check_version(100, "test_feature");
        assert!(result.is_ok());
    }

    #[test]
    fn test_request_builder_check_version_failure() {
        let (client, _gateway) = create_test_client();
        let builder = RequestBuilder::new(&client);
        let result = builder.check_version(999999, "future_feature");
        assert!(result.is_err());
    }

    #[test]
    fn test_shared_request_builder_new() {
        let (client, _gateway) = create_test_client();
        let builder = SharedRequestBuilder::new(&client, OutgoingMessages::RequestMarketData);
        assert_eq!(builder.message_type, OutgoingMessages::RequestMarketData);
    }

    #[test]
    fn test_shared_request_builder_check_version() {
        let (client, _gateway) = create_test_client();
        let builder = SharedRequestBuilder::new(&client, OutgoingMessages::RequestMarketData);
        let result = builder.check_version(100, "test_feature");
        assert!(result.is_ok());
    }

    #[test]
    fn test_order_request_builder_new() {
        let (client, _gateway) = create_test_client();
        let builder = OrderRequestBuilder::new(&client);
        assert!(builder.order_id > 0);
    }

    #[test]
    fn test_order_request_builder_with_id() {
        let (client, _gateway) = create_test_client();
        let order_id = 12345;
        let builder = OrderRequestBuilder::with_id(&client, order_id);
        assert_eq!(builder.order_id(), order_id);
    }

    #[test]
    fn test_message_builder_new() {
        let (client, _gateway) = create_test_client();
        let builder = MessageBuilder::new(&client);
        // MessageBuilder doesn't have public fields to test, just ensure it creates
        let _ = builder;
    }

    #[test]
    fn test_message_builder_check_version() {
        let (client, _gateway) = create_test_client();
        let builder = MessageBuilder::new(&client);
        let result = builder.check_version(100, "test_feature");
        assert!(result.is_ok());
    }

    #[test]
    fn test_subscription_builder_new() {
        let (client, _gateway) = create_test_client();
        let builder: SubscriptionBuilder<Bar> = SubscriptionBuilder::new(&client);
        // Builder created successfully
        let _ = builder;
    }

    #[test]
    fn test_subscription_builder_with_context() {
        let (client, _gateway) = create_test_client();
        let context = client
            .decoder_context()
            .with_smart_depth(true)
            .with_request_type(OutgoingMessages::RequestMarketData);
        let builder: SubscriptionBuilder<Bar> = SubscriptionBuilder::new(&client).with_context(context.clone());
        assert_eq!(builder.context, context);
    }

    #[test]
    fn test_subscription_builder_with_smart_depth() {
        let (client, _gateway) = create_test_client();
        let builder: SubscriptionBuilder<Bar> = SubscriptionBuilder::new(&client).with_smart_depth(true);
        assert!(builder.context.is_smart_depth);
    }

    #[test]
    fn test_client_request_builders_trait() {
        let (client, _gateway) = create_test_client();

        // Test request()
        let request_builder = client.request();
        assert!(request_builder.request_id > 0);

        // Test request_with_id()
        let request_builder = client.request_with_id(99);
        assert_eq!(request_builder.request_id(), 99);

        // Test shared_request()
        let shared_builder = client.shared_request(OutgoingMessages::RequestMarketData);
        assert_eq!(shared_builder.message_type, OutgoingMessages::RequestMarketData);

        // Test order_request()
        let order_builder = client.order_request();
        assert!(order_builder.order_id > 0);

        // Test order_request_with_id()
        let order_builder = client.order_request_with_id(999);
        assert_eq!(order_builder.order_id(), 999);

        // Test message()
        let _message_builder = client.message();
    }

    #[test]
    fn test_subscription_builder_ext_trait() {
        let (client, _gateway) = create_test_client();
        let builder: SubscriptionBuilder<Bar> = client.subscription();
        // Builder created successfully through trait
        let _ = builder;
    }

    #[test]
    fn test_builder_patterns_table_driven() {
        struct TestCase {
            name: &'static str,
            request_id: Option<i32>,
            order_id: Option<i32>,
            message_type: Option<OutgoingMessages>,
            expected_id_min: i32,
        }

        let test_cases = vec![
            TestCase {
                name: "auto_request_id",
                request_id: None,
                order_id: None,
                message_type: None,
                expected_id_min: 1,
            },
            TestCase {
                name: "specific_request_id",
                request_id: Some(100),
                order_id: None,
                message_type: None,
                expected_id_min: 100,
            },
            TestCase {
                name: "specific_order_id",
                request_id: None,
                order_id: Some(500),
                message_type: None,
                expected_id_min: 500,
            },
            TestCase {
                name: "shared_request_type",
                request_id: None,
                order_id: None,
                message_type: Some(OutgoingMessages::RequestAccountData),
                expected_id_min: 0,
            },
        ];

        for tc in test_cases {
            let (client, _gateway) = create_test_client();

            if let Some(request_id) = tc.request_id {
                let builder = client.request_with_id(request_id);
                assert_eq!(builder.request_id(), request_id, "test case '{}' failed", tc.name);
            } else if let Some(order_id) = tc.order_id {
                let builder = client.order_request_with_id(order_id);
                assert_eq!(builder.order_id(), order_id, "test case '{}' failed", tc.name);
            } else if let Some(message_type) = tc.message_type {
                let builder = client.shared_request(message_type);
                assert_eq!(builder.message_type, message_type, "test case '{}' failed", tc.name);
            } else {
                let builder = client.request();
                assert!(builder.request_id() >= tc.expected_id_min, "test case '{}' failed", tc.name);
            }
        }
    }

    #[test]
    fn test_response_context_modifications() {
        struct TestCase {
            name: &'static str,
            initial_smart_depth: bool,
            initial_request_type: Option<OutgoingMessages>,
            set_smart_depth: Option<bool>,
            set_request_type: Option<OutgoingMessages>,
            expected_smart_depth: bool,
            expected_request_type: Option<OutgoingMessages>,
        }

        let test_cases = vec![
            TestCase {
                name: "default_context",
                initial_smart_depth: false,
                initial_request_type: None,
                set_smart_depth: None,
                set_request_type: None,
                expected_smart_depth: false,
                expected_request_type: None,
            },
            TestCase {
                name: "set_smart_depth_true",
                initial_smart_depth: false,
                initial_request_type: None,
                set_smart_depth: Some(true),
                set_request_type: None,
                expected_smart_depth: true,
                expected_request_type: None,
            },
            TestCase {
                name: "set_request_type",
                initial_smart_depth: false,
                initial_request_type: None,
                set_smart_depth: None,
                set_request_type: Some(OutgoingMessages::RequestMarketData),
                expected_smart_depth: false,
                expected_request_type: Some(OutgoingMessages::RequestMarketData),
            },
            TestCase {
                name: "set_both",
                initial_smart_depth: false,
                initial_request_type: None,
                set_smart_depth: Some(true),
                set_request_type: Some(OutgoingMessages::CancelMarketData),
                expected_smart_depth: true,
                expected_request_type: Some(OutgoingMessages::CancelMarketData),
            },
        ];

        for tc in test_cases {
            let (client, _gateway) = create_test_client();
            let mut builder: SubscriptionBuilder<Bar> = SubscriptionBuilder::new(&client);

            // Set initial context
            builder.context.is_smart_depth = tc.initial_smart_depth;
            builder.context.request_type = tc.initial_request_type;

            // Apply modifications
            if let Some(smart_depth) = tc.set_smart_depth {
                builder = builder.with_smart_depth(smart_depth);
            }

            if let Some(request_type) = tc.set_request_type {
                let context = DecoderContext::new(builder.context.server_version, builder.context.time_zone)
                    .with_smart_depth(builder.context.is_smart_depth)
                    .with_request_type(request_type);
                builder = builder.with_context(context);
            }

            // Verify expectations
            assert_eq!(
                builder.context.is_smart_depth, tc.expected_smart_depth,
                "test case '{}' failed: smart_depth mismatch",
                tc.name
            );
            assert_eq!(
                builder.context.request_type, tc.expected_request_type,
                "test case '{}' failed: request_type mismatch",
                tc.name
            );
        }
    }
}