ibapi 3.0.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! Builders for contracts-domain request and response messages.

use super::{RequestEncoder, ResponseProtoEncoder};
use crate::common::test_utils::helpers::constants::TEST_REQ_ID_FIRST;
use crate::contracts::{Contract, SecurityType};
use crate::messages::OutgoingMessages;
use crate::proto;
use crate::proto::encoders::{encode_contract, some_str};

// =============================================================================
// Request builders
// =============================================================================

#[derive(Clone, Debug)]
pub struct ContractDataRequestBuilder {
    pub request_id: i32,
    pub contract: Contract,
}

impl Default for ContractDataRequestBuilder {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            contract: Contract::default(),
        }
    }
}

impl ContractDataRequestBuilder {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn contract(mut self, contract: &Contract) -> Self {
        self.contract = contract.clone();
        self
    }
}

impl RequestEncoder for ContractDataRequestBuilder {
    type Proto = proto::ContractDataRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::RequestContractData;

    fn to_proto(&self) -> Self::Proto {
        proto::ContractDataRequest {
            req_id: Some(self.request_id),
            contract: Some(encode_contract(&self.contract)),
        }
    }
}

#[derive(Clone, Debug)]
pub struct MatchingSymbolsRequestBuilder {
    pub request_id: i32,
    pub pattern: String,
}

impl Default for MatchingSymbolsRequestBuilder {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            pattern: String::new(),
        }
    }
}

impl MatchingSymbolsRequestBuilder {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn pattern(mut self, v: impl Into<String>) -> Self {
        self.pattern = v.into();
        self
    }
}

impl RequestEncoder for MatchingSymbolsRequestBuilder {
    type Proto = proto::MatchingSymbolsRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::RequestMatchingSymbols;

    fn to_proto(&self) -> Self::Proto {
        proto::MatchingSymbolsRequest {
            req_id: Some(self.request_id),
            pattern: Some(self.pattern.clone()),
        }
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub struct MarketRuleRequestBuilder {
    pub market_rule_id: i32,
}

impl MarketRuleRequestBuilder {
    pub fn market_rule_id(mut self, v: i32) -> Self {
        self.market_rule_id = v;
        self
    }
}

impl RequestEncoder for MarketRuleRequestBuilder {
    type Proto = proto::MarketRuleRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::RequestMarketRule;

    fn to_proto(&self) -> Self::Proto {
        proto::MarketRuleRequest {
            market_rule_id: Some(self.market_rule_id),
        }
    }
}

#[derive(Clone, Debug)]
pub struct SmartComponentsRequestBuilder {
    pub request_id: i32,
    pub bbo_exchange: String,
}

impl Default for SmartComponentsRequestBuilder {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            bbo_exchange: String::new(),
        }
    }
}

impl SmartComponentsRequestBuilder {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn bbo_exchange(mut self, v: impl Into<String>) -> Self {
        self.bbo_exchange = v.into();
        self
    }
}

impl RequestEncoder for SmartComponentsRequestBuilder {
    type Proto = proto::SmartComponentsRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::RequestSmartComponents;

    fn to_proto(&self) -> Self::Proto {
        proto::SmartComponentsRequest {
            req_id: Some(self.request_id),
            bbo_exchange: some_str(&self.bbo_exchange),
        }
    }
}

#[derive(Clone, Debug)]
pub struct CalculateOptionPriceRequestBuilder {
    pub request_id: i32,
    pub contract: Contract,
    pub volatility: f64,
    pub underlying_price: f64,
}

impl Default for CalculateOptionPriceRequestBuilder {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            contract: Contract::default(),
            volatility: 0.0,
            underlying_price: 0.0,
        }
    }
}

impl CalculateOptionPriceRequestBuilder {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn contract(mut self, contract: &Contract) -> Self {
        self.contract = contract.clone();
        self
    }
    pub fn volatility(mut self, v: f64) -> Self {
        self.volatility = v;
        self
    }
    pub fn underlying_price(mut self, v: f64) -> Self {
        self.underlying_price = v;
        self
    }
}

impl RequestEncoder for CalculateOptionPriceRequestBuilder {
    type Proto = proto::CalculateOptionPriceRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::ReqCalcOptionPrice;

    fn to_proto(&self) -> Self::Proto {
        proto::CalculateOptionPriceRequest {
            req_id: Some(self.request_id),
            contract: Some(encode_contract(&self.contract)),
            volatility: Some(self.volatility),
            under_price: Some(self.underlying_price),
            option_price_options: Default::default(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct CalculateImpliedVolatilityRequestBuilder {
    pub request_id: i32,
    pub contract: Contract,
    pub option_price: f64,
    pub underlying_price: f64,
}

impl Default for CalculateImpliedVolatilityRequestBuilder {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            contract: Contract::default(),
            option_price: 0.0,
            underlying_price: 0.0,
        }
    }
}

impl CalculateImpliedVolatilityRequestBuilder {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn contract(mut self, contract: &Contract) -> Self {
        self.contract = contract.clone();
        self
    }
    pub fn option_price(mut self, v: f64) -> Self {
        self.option_price = v;
        self
    }
    pub fn underlying_price(mut self, v: f64) -> Self {
        self.underlying_price = v;
        self
    }
}

impl RequestEncoder for CalculateImpliedVolatilityRequestBuilder {
    type Proto = proto::CalculateImpliedVolatilityRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::ReqCalcImpliedVolat;

    fn to_proto(&self) -> Self::Proto {
        proto::CalculateImpliedVolatilityRequest {
            req_id: Some(self.request_id),
            contract: Some(encode_contract(&self.contract)),
            option_price: Some(self.option_price),
            under_price: Some(self.underlying_price),
            implied_volatility_options: Default::default(),
        }
    }
}

// CancelOptionPrice / CancelImpliedVolatility builders intentionally omitted:
// the production cancel path goes through `OptionComputation::cancel_message`
// in `stream_decoders`, which is exercised end-to-end by `test_cancel_messages`.
single_req_id_request_builder!(CancelContractDataRequestBuilder, CancelContractData, OutgoingMessages::CancelContractData);

#[derive(Clone, Debug)]
pub struct OptionChainRequestBuilder {
    pub request_id: i32,
    pub symbol: String,
    pub exchange: String,
    pub security_type: SecurityType,
    pub contract_id: i32,
}

impl Default for OptionChainRequestBuilder {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            symbol: String::new(),
            exchange: String::new(),
            security_type: SecurityType::Stock,
            contract_id: 0,
        }
    }
}

impl OptionChainRequestBuilder {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = v.into();
        self
    }
    pub fn exchange(mut self, v: impl Into<String>) -> Self {
        self.exchange = v.into();
        self
    }
    pub fn security_type(mut self, v: SecurityType) -> Self {
        self.security_type = v;
        self
    }
    pub fn contract_id(mut self, v: i32) -> Self {
        self.contract_id = v;
        self
    }
}

impl RequestEncoder for OptionChainRequestBuilder {
    type Proto = proto::SecDefOptParamsRequest;
    const MSG_ID: OutgoingMessages = OutgoingMessages::RequestSecurityDefinitionOptionalParameters;

    fn to_proto(&self) -> Self::Proto {
        proto::SecDefOptParamsRequest {
            req_id: Some(self.request_id),
            underlying_symbol: Some(self.symbol.clone()),
            fut_fop_exchange: Some(self.exchange.clone()),
            underlying_sec_type: Some(self.security_type.to_string()),
            underlying_con_id: Some(self.contract_id),
        }
    }
}

// =============================================================================
// Response builders
// =============================================================================

/// Builder for `ContractData` (msg 10) responses. Mirrors `proto::ContractData`
/// (req_id + Contract + ContractDetails). Only the fields exercised by tests
/// have setters; everything else stays at the proto default.
#[derive(Clone, Debug)]
pub struct ContractDataResponse {
    pub request_id: i32,
    pub contract_id: i32,
    pub symbol: String,
    pub security_type: String,
    pub last_trade_date_or_contract_month: String,
    pub multiplier: String,
    pub exchange: String,
    pub primary_exchange: String,
    pub currency: String,
    pub local_symbol: String,
    pub trading_class: String,
    pub market_name: String,
    pub min_tick: String,
    pub order_types: String,
    pub valid_exchanges: String,
    pub long_name: String,
    pub industry: String,
    pub category: String,
    pub subcategory: String,
    pub time_zone_id: String,
    pub stock_type: String,
    /// Default `"1"` is load-bearing — `test_contract_details` validators assert `min_size == 1.0`.
    pub min_size: String,
    /// Default `"1"` is load-bearing — see `min_size`.
    pub size_increment: String,
    /// Default `"100"` is load-bearing — see `min_size`.
    pub suggested_size_increment: String,
}

impl Default for ContractDataResponse {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            contract_id: 0,
            symbol: String::new(),
            security_type: String::new(),
            last_trade_date_or_contract_month: String::new(),
            multiplier: String::new(),
            exchange: String::new(),
            primary_exchange: String::new(),
            currency: String::new(),
            local_symbol: String::new(),
            trading_class: String::new(),
            market_name: String::new(),
            min_tick: "0.01".to_string(),
            order_types: String::new(),
            valid_exchanges: String::new(),
            long_name: String::new(),
            industry: String::new(),
            category: String::new(),
            subcategory: String::new(),
            time_zone_id: String::new(),
            stock_type: String::new(),
            min_size: "1".to_string(),
            size_increment: "1".to_string(),
            suggested_size_increment: "100".to_string(),
        }
    }
}

impl ContractDataResponse {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn contract_id(mut self, v: i32) -> Self {
        self.contract_id = v;
        self
    }
    pub fn symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = v.into();
        self
    }
    pub fn security_type(mut self, v: impl Into<String>) -> Self {
        self.security_type = v.into();
        self
    }
    pub fn last_trade_date_or_contract_month(mut self, v: impl Into<String>) -> Self {
        self.last_trade_date_or_contract_month = v.into();
        self
    }
    pub fn multiplier(mut self, v: impl Into<String>) -> Self {
        self.multiplier = v.into();
        self
    }
    pub fn exchange(mut self, v: impl Into<String>) -> Self {
        self.exchange = v.into();
        self
    }
    pub fn primary_exchange(mut self, v: impl Into<String>) -> Self {
        self.primary_exchange = v.into();
        self
    }
    pub fn currency(mut self, v: impl Into<String>) -> Self {
        self.currency = v.into();
        self
    }
    pub fn local_symbol(mut self, v: impl Into<String>) -> Self {
        self.local_symbol = v.into();
        self
    }
    pub fn trading_class(mut self, v: impl Into<String>) -> Self {
        self.trading_class = v.into();
        self
    }
    pub fn market_name(mut self, v: impl Into<String>) -> Self {
        self.market_name = v.into();
        self
    }
    pub fn min_tick(mut self, v: impl Into<String>) -> Self {
        self.min_tick = v.into();
        self
    }
    pub fn order_types(mut self, v: impl Into<String>) -> Self {
        self.order_types = v.into();
        self
    }
    pub fn valid_exchanges(mut self, v: impl Into<String>) -> Self {
        self.valid_exchanges = v.into();
        self
    }
    pub fn long_name(mut self, v: impl Into<String>) -> Self {
        self.long_name = v.into();
        self
    }
    pub fn industry(mut self, v: impl Into<String>) -> Self {
        self.industry = v.into();
        self
    }
    pub fn category(mut self, v: impl Into<String>) -> Self {
        self.category = v.into();
        self
    }
    pub fn subcategory(mut self, v: impl Into<String>) -> Self {
        self.subcategory = v.into();
        self
    }
    pub fn time_zone_id(mut self, v: impl Into<String>) -> Self {
        self.time_zone_id = v.into();
        self
    }
    pub fn stock_type(mut self, v: impl Into<String>) -> Self {
        self.stock_type = v.into();
        self
    }
}

impl ResponseProtoEncoder for ContractDataResponse {
    type Proto = proto::ContractData;

    fn to_proto(&self) -> Self::Proto {
        proto::ContractData {
            req_id: Some(self.request_id),
            contract: Some(proto::Contract {
                con_id: Some(self.contract_id),
                symbol: some_str(&self.symbol),
                sec_type: some_str(&self.security_type),
                last_trade_date_or_contract_month: some_str(&self.last_trade_date_or_contract_month),
                multiplier: if self.multiplier.is_empty() {
                    None
                } else {
                    self.multiplier.parse().ok()
                },
                exchange: some_str(&self.exchange),
                primary_exch: some_str(&self.primary_exchange),
                currency: some_str(&self.currency),
                local_symbol: some_str(&self.local_symbol),
                trading_class: some_str(&self.trading_class),
                ..Default::default()
            }),
            contract_details: Some(proto::ContractDetails {
                market_name: some_str(&self.market_name),
                min_tick: some_str(&self.min_tick),
                order_types: some_str(&self.order_types),
                valid_exchanges: some_str(&self.valid_exchanges),
                long_name: some_str(&self.long_name),
                industry: some_str(&self.industry),
                category: some_str(&self.category),
                subcategory: some_str(&self.subcategory),
                time_zone_id: some_str(&self.time_zone_id),
                stock_type: some_str(&self.stock_type),
                min_size: some_str(&self.min_size),
                size_increment: some_str(&self.size_increment),
                suggested_size_increment: some_str(&self.suggested_size_increment),
                ..Default::default()
            }),
        }
    }
}

/// Builder for `SymbolSamples` (msg 79) responses.
#[derive(Clone, Debug)]
pub struct SymbolSamplesEntry {
    pub contract_id: i32,
    pub symbol: String,
    pub security_type: String,
    pub primary_exchange: String,
    pub currency: String,
    pub description: String,
    pub derivative_security_types: Vec<String>,
}

impl SymbolSamplesEntry {
    pub fn primary_exchange(mut self, v: impl Into<String>) -> Self {
        self.primary_exchange = v.into();
        self
    }
    pub fn description(mut self, v: impl Into<String>) -> Self {
        self.description = v.into();
        self
    }
    pub fn derivative_security_types(mut self, v: Vec<String>) -> Self {
        self.derivative_security_types = v;
        self
    }
}

#[derive(Clone, Debug, Default)]
pub struct SymbolSamplesResponse {
    pub request_id: i32,
    pub entries: Vec<SymbolSamplesEntry>,
}

impl SymbolSamplesResponse {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn entry(mut self, e: SymbolSamplesEntry) -> Self {
        self.entries.push(e);
        self
    }
}

impl ResponseProtoEncoder for SymbolSamplesResponse {
    type Proto = proto::SymbolSamples;

    fn to_proto(&self) -> Self::Proto {
        proto::SymbolSamples {
            req_id: Some(self.request_id),
            contract_descriptions: self
                .entries
                .iter()
                .map(|e| proto::ContractDescription {
                    contract: Some(proto::Contract {
                        con_id: Some(e.contract_id),
                        symbol: some_str(&e.symbol),
                        sec_type: some_str(&e.security_type),
                        primary_exch: some_str(&e.primary_exchange),
                        currency: some_str(&e.currency),
                        description: some_str(&e.description),
                        ..Default::default()
                    }),
                    derivative_sec_types: e.derivative_security_types.clone(),
                })
                .collect(),
        }
    }
}

/// Builder for `MarketRule` (msg 87) responses.
#[derive(Clone, Debug)]
pub struct MarketRuleResponse {
    pub market_rule_id: i32,
    pub price_increments: Vec<(f64, f64)>,
}

impl MarketRuleResponse {
    pub fn increment(mut self, low_edge: f64, increment: f64) -> Self {
        self.price_increments.push((low_edge, increment));
        self
    }
}

impl ResponseProtoEncoder for MarketRuleResponse {
    type Proto = proto::MarketRule;

    fn to_proto(&self) -> Self::Proto {
        proto::MarketRule {
            market_rule_id: Some(self.market_rule_id),
            price_increments: self
                .price_increments
                .iter()
                .map(|(low_edge, increment)| proto::PriceIncrement {
                    low_edge: Some(*low_edge),
                    increment: Some(*increment),
                })
                .collect(),
        }
    }
}

/// Builder for `SmartComponents` (msg 82) responses.
#[derive(Clone, Debug, Default)]
pub struct SmartComponentsResponse {
    pub components: Vec<(i32, String, String)>,
}

impl SmartComponentsResponse {
    pub fn component(mut self, bit_number: i32, exchange: impl Into<String>, exchange_letter: impl Into<String>) -> Self {
        self.components.push((bit_number, exchange.into(), exchange_letter.into()));
        self
    }
}

impl ResponseProtoEncoder for SmartComponentsResponse {
    type Proto = proto::SmartComponents;

    fn to_proto(&self) -> Self::Proto {
        proto::SmartComponents {
            req_id: None,
            smart_components: self
                .components
                .iter()
                .map(|(bit_number, exchange, exchange_letter)| proto::SmartComponent {
                    bit_number: Some(*bit_number),
                    exchange: some_str(exchange),
                    exchange_letter: some_str(exchange_letter),
                })
                .collect(),
        }
    }
}

/// Builder for `SecurityDefinitionOptionParameter` (msg 75) responses.
#[derive(Clone, Debug)]
pub struct OptionChainResponse {
    pub request_id: i32,
    pub exchange: String,
    pub underlying_contract_id: i32,
    pub trading_class: String,
    pub multiplier: String,
    pub expirations: Vec<String>,
    pub strikes: Vec<f64>,
}

impl Default for OptionChainResponse {
    fn default() -> Self {
        Self {
            request_id: TEST_REQ_ID_FIRST,
            exchange: String::new(),
            underlying_contract_id: 0,
            trading_class: String::new(),
            multiplier: "100".to_string(),
            expirations: Vec::new(),
            strikes: Vec::new(),
        }
    }
}

impl OptionChainResponse {
    pub fn request_id(mut self, v: i32) -> Self {
        self.request_id = v;
        self
    }
    pub fn exchange(mut self, v: impl Into<String>) -> Self {
        self.exchange = v.into();
        self
    }
    pub fn underlying_contract_id(mut self, v: i32) -> Self {
        self.underlying_contract_id = v;
        self
    }
    pub fn trading_class(mut self, v: impl Into<String>) -> Self {
        self.trading_class = v.into();
        self
    }
    pub fn multiplier(mut self, v: impl Into<String>) -> Self {
        self.multiplier = v.into();
        self
    }
    pub fn expirations(mut self, v: Vec<String>) -> Self {
        self.expirations = v;
        self
    }
    pub fn strikes(mut self, v: Vec<f64>) -> Self {
        self.strikes = v;
        self
    }
}

impl ResponseProtoEncoder for OptionChainResponse {
    type Proto = proto::SecDefOptParameter;

    fn to_proto(&self) -> Self::Proto {
        proto::SecDefOptParameter {
            req_id: Some(self.request_id),
            exchange: some_str(&self.exchange),
            underlying_con_id: Some(self.underlying_contract_id),
            trading_class: some_str(&self.trading_class),
            multiplier: some_str(&self.multiplier),
            expirations: self.expirations.clone(),
            strikes: self.strikes.clone(),
        }
    }
}

// =============================================================================
// Entry-point functions
// =============================================================================

pub fn contract_data_request() -> ContractDataRequestBuilder {
    ContractDataRequestBuilder::default()
}

pub fn matching_symbols_request() -> MatchingSymbolsRequestBuilder {
    MatchingSymbolsRequestBuilder::default()
}

pub fn market_rule_request() -> MarketRuleRequestBuilder {
    MarketRuleRequestBuilder::default()
}

pub fn smart_components_request() -> SmartComponentsRequestBuilder {
    SmartComponentsRequestBuilder::default()
}

pub fn calculate_option_price_request() -> CalculateOptionPriceRequestBuilder {
    CalculateOptionPriceRequestBuilder::default()
}

pub fn calculate_implied_volatility_request() -> CalculateImpliedVolatilityRequestBuilder {
    CalculateImpliedVolatilityRequestBuilder::default()
}

pub fn cancel_contract_data_request() -> CancelContractDataRequestBuilder {
    CancelContractDataRequestBuilder::default()
}

pub fn option_chain_request() -> OptionChainRequestBuilder {
    OptionChainRequestBuilder::default()
}

pub fn contract_data() -> ContractDataResponse {
    ContractDataResponse::default()
}

pub fn symbol_samples() -> SymbolSamplesResponse {
    SymbolSamplesResponse::default()
}

pub fn symbol_samples_entry(contract_id: i32, symbol: impl Into<String>) -> SymbolSamplesEntry {
    SymbolSamplesEntry {
        contract_id,
        symbol: symbol.into(),
        security_type: "STK".to_string(),
        primary_exchange: "NASDAQ".to_string(),
        currency: "USD".to_string(),
        description: String::new(),
        derivative_security_types: Vec::new(),
    }
}

pub fn market_rule(market_rule_id: i32) -> MarketRuleResponse {
    MarketRuleResponse {
        market_rule_id,
        price_increments: Vec::new(),
    }
}

pub fn smart_components() -> SmartComponentsResponse {
    SmartComponentsResponse::default()
}

pub fn option_chain() -> OptionChainResponse {
    OptionChainResponse::default()
}