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
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
//! Builder types for order conditions.
//!
//! This module provides fluent builder APIs for constructing order conditions
//! with type safety and validation.

use serde::{Deserialize, Serialize};

/// Price evaluation method for price conditions.
///
/// Determines which price feed to use when evaluating price conditions.
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum TriggerMethod {
    /// Default method (last for most securities, double bid/ask for OTC and options)
    #[default]
    Default = 0,
    /// Two consecutive bid or ask prices
    DoubleBidAsk = 1,
    /// Last traded price
    Last = 2,
    /// Two consecutive last prices
    DoubleLast = 3,
    /// Current bid or ask price
    BidAsk = 4,
    /// Last price or bid/ask if no last price available
    LastOrBidAsk = 7,
    /// Mid-point between bid and ask
    Midpoint = 8,
}

impl From<TriggerMethod> for i32 {
    fn from(method: TriggerMethod) -> i32 {
        method as i32
    }
}

impl From<i32> for TriggerMethod {
    fn from(value: i32) -> Self {
        match value {
            0 => TriggerMethod::Default,
            1 => TriggerMethod::DoubleBidAsk,
            2 => TriggerMethod::Last,
            3 => TriggerMethod::DoubleLast,
            4 => TriggerMethod::BidAsk,
            7 => TriggerMethod::LastOrBidAsk,
            8 => TriggerMethod::Midpoint,
            _ => TriggerMethod::Default,
        }
    }
}

impl crate::ToField for TriggerMethod {
    fn to_field(&self) -> String {
        i32::from(*self).to_string()
    }
}

// ============================================================================
// Condition Structs (to be created by Unit 1.1)
// ============================================================================

/// Price-based condition that activates an order when a contract reaches a specified price.
///
/// This condition monitors the price of a specific contract and triggers when the price
/// crosses the specified threshold. The trigger method determines which price feed to use
/// (last, bid/ask, mid-point, etc.).
///
/// # TWS Behavior
///
/// - The contract must be specified by its contract ID, which can be obtained via
///   `contract_details()` API call
/// - Different exchanges may have different price feeds available
/// - The condition continuously monitors the price during market hours
/// - When `conditions_ignore_rth` is true on the order, monitoring extends to
///   after-hours trading
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::{PriceCondition, TriggerMethod};
/// use ibapi::orders::OrderCondition;
///
/// // Trigger when AAPL (contract ID 265598) goes above $150 on SMART
/// let condition = PriceCondition::builder(265598, "SMART")
///     .greater_than(150.0)
///     .trigger_method(TriggerMethod::Last)
///     .build();
///
/// let order_condition = OrderCondition::Price(condition);
/// ```
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PriceCondition {
    /// Contract identifier for the instrument to monitor.
    /// Use contract_details() to obtain the contract_id for a symbol.
    pub contract_id: i32,
    /// Exchange where the price is monitored (e.g., "SMART", "NASDAQ", "NYSE").
    pub exchange: String,
    /// Trigger price threshold.
    pub price: f64,
    /// Method for price evaluation.
    #[serde(serialize_with = "serialize_trigger_method", deserialize_with = "deserialize_trigger_method")]
    pub trigger_method: TriggerMethod,
    /// True to trigger when price goes above threshold, false for below.
    pub is_more: bool,
    /// True for AND condition (all conditions must be met), false for OR condition (any condition triggers).
    pub is_conjunction: bool,
}

impl Default for PriceCondition {
    fn default() -> Self {
        Self {
            contract_id: 0,
            exchange: String::new(),
            price: 0.0,
            trigger_method: TriggerMethod::Default,
            is_more: true,
            is_conjunction: true,
        }
    }
}

// Serde helpers for TriggerMethod
fn serialize_trigger_method<S>(method: &TriggerMethod, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_i32((*method).into())
}

fn deserialize_trigger_method<'de, D>(deserializer: D) -> Result<TriggerMethod, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = i32::deserialize(deserializer)?;
    Ok(value.into())
}

/// Time-based condition that activates an order at a specific date and time.
///
/// This condition triggers when the current time passes (or is before) the specified
/// time threshold. Useful for scheduling orders to activate at specific times.
///
/// # TWS Behavior
///
/// - Time is evaluated based on the timezone specified in the time string
/// - The condition checks continuously and triggers once the time threshold is crossed
/// - Common use case: activate orders at market open, before close, or at specific times
/// - Unlike `good_after_time`/`good_till_date` on the order itself, this can be combined
///   with other conditions using AND/OR logic
///
/// # Time Format
///
/// Format: "YYYYMMDD HH:MM:SS TZ"
/// - YYYYMMDD: Year, month, day (e.g., 20251230)
/// - HH:MM:SS: Hour, minute, second in 24-hour format
/// - TZ: Timezone (e.g., "UTC", "US/Eastern", "America/New_York")
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::TimeCondition;
/// use ibapi::orders::OrderCondition;
///
/// // Trigger after 2:30 PM Eastern Time on December 30, 2025
/// let condition = TimeCondition::builder()
///     .greater_than("20251230 14:30:00 US/Eastern")
///     .build();
///
/// let order_condition = OrderCondition::Time(condition);
/// ```
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct TimeCondition {
    /// Time in format "YYYYMMDD HH:MM:SS TZ".
    /// Example: "20251230 14:30:00 US/Eastern"
    pub time: String,
    /// True to trigger after the time, false for before.
    pub is_more: bool,
    /// True for AND condition (all conditions must be met), false for OR condition (any condition triggers).
    pub is_conjunction: bool,
}

/// Margin cushion condition that activates an order based on account margin levels.
///
/// The margin cushion is a measure of account health, calculated as:
/// (Equity with Loan Value - Maintenance Margin) / Net Liquidation Value
///
/// This condition monitors your account's margin cushion and triggers when it crosses
/// the specified percentage threshold. Useful for risk management and protecting against
/// margin calls.
///
/// # TWS Behavior
///
/// - Margin cushion is updated in real-time as positions and prices change
/// - The percentage is specified as an integer (e.g., 30 for 30%)
/// - Only applies to margin accounts; cash accounts will not trigger this condition
/// - Common use: Submit protective orders when margin cushion falls below safe levels
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::MarginCondition;
/// use ibapi::orders::OrderCondition;
///
/// // Trigger when margin cushion falls below 30%
/// let condition = MarginCondition::builder()
///     .less_than(30)
///     .build();
///
/// let order_condition = OrderCondition::Margin(condition);
/// ```
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct MarginCondition {
    /// Margin cushion percentage threshold (0-100).
    /// Example: 30 represents 30% margin cushion.
    pub percent: i32,
    /// True to trigger when margin cushion goes above threshold, false for below.
    pub is_more: bool,
    /// True for AND condition (all conditions must be met), false for OR condition (any condition triggers).
    pub is_conjunction: bool,
}

/// Execution-based condition that activates an order when a trade of a specific security executes.
///
/// This condition monitors executions in your account and triggers when any trade of the
/// specified contract executes. The condition checks for executions matching the symbol,
/// security type, and exchange.
///
/// # TWS Behavior
///
/// - The condition triggers on ANY execution of the specified contract, regardless of side or quantity
/// - Only monitors executions in the current account
/// - The execution can be from any order type (market, limit, stop, etc.)
/// - Common use case: Place a hedge order immediately after an initial position is filled
/// - The symbol must match exactly (case-sensitive in most cases)
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::ExecutionCondition;
/// use ibapi::orders::OrderCondition;
///
/// // Trigger when MSFT stock executes on SMART exchange
/// let condition = ExecutionCondition::builder("MSFT", "STK", "SMART")
///     .build();
///
/// let order_condition = OrderCondition::Execution(condition);
/// ```
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ExecutionCondition {
    /// Symbol of the contract to monitor for executions.
    pub symbol: String,
    /// Security type: "STK" (stock), "OPT" (option), "FUT" (future), "FOP" (future option), etc.
    pub security_type: String,
    /// Exchange where execution is monitored (e.g., "SMART", "NASDAQ", "NYSE").
    pub exchange: String,
    /// True for AND condition (all conditions must be met), false for OR condition (any condition triggers).
    pub is_conjunction: bool,
}

/// Volume-based condition that activates an order when cumulative volume reaches a threshold.
///
/// This condition monitors the cumulative trading volume for a specific contract throughout
/// the trading day and triggers when the volume crosses the specified threshold.
///
/// # TWS Behavior
///
/// - Volume is cumulative from market open (resets daily)
/// - The contract must be specified by its contract ID
/// - Volume tracking is exchange-specific (different exchanges may show different volumes)
/// - When `conditions_ignore_rth` is true on the order, includes after-hours volume
/// - Common use case: Enter positions after sufficient liquidity is established
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::VolumeCondition;
/// use ibapi::orders::OrderCondition;
///
/// // Trigger when TSLA volume exceeds 50 million shares
/// let condition = VolumeCondition::builder(76792991, "SMART")
///     .greater_than(50_000_000)
///     .build();
///
/// let order_condition = OrderCondition::Volume(condition);
/// ```
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct VolumeCondition {
    /// Contract identifier for the instrument to monitor.
    /// Use contract_details() to obtain the contract_id for a symbol.
    pub contract_id: i32,
    /// Exchange where volume is monitored (e.g., "SMART", "NASDAQ", "NYSE").
    pub exchange: String,
    /// Volume threshold (number of shares/contracts traded).
    pub volume: i32,
    /// True to trigger when volume goes above threshold, false for below.
    pub is_more: bool,
    /// True for AND condition (all conditions must be met), false for OR condition (any condition triggers).
    pub is_conjunction: bool,
}

/// Percent change condition that activates an order based on price movement percentage.
///
/// This condition monitors the percentage change in a contract's price from its value at
/// the start of the trading day and triggers when the change crosses the specified threshold.
/// The percentage can be positive (gain) or negative (loss).
///
/// # TWS Behavior
///
/// - Percent change is calculated from the session's opening price
/// - The contract must be specified by its contract ID
/// - The percentage is specified as a decimal (e.g., 2.0 for 2%, not 0.02)
/// - When `is_more` is true, triggers on upward moves; when false, on downward moves
/// - Resets at the start of each trading session
/// - Common use case: Momentum trading or volatility-based order activation
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::PercentChangeCondition;
/// use ibapi::orders::OrderCondition;
///
/// // Trigger when SPY moves more than 2% upward from open
/// let condition = PercentChangeCondition::builder(756733, "SMART")
///     .greater_than(2.0)
///     .build();
///
/// let order_condition = OrderCondition::PercentChange(condition);
/// ```
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct PercentChangeCondition {
    /// Contract identifier for the instrument to monitor.
    /// Use contract_details() to obtain the contract_id for a symbol.
    pub contract_id: i32,
    /// Exchange where price change is monitored (e.g., "SMART", "NASDAQ", "NYSE").
    pub exchange: String,
    /// Percentage change threshold (e.g., 2.0 for 2%, 5.5 for 5.5%).
    pub percent: f64,
    /// True to trigger when percent change goes above threshold (gains), false for below (losses).
    pub is_more: bool,
    /// True for AND condition (all conditions must be met), false for OR condition (any condition triggers).
    pub is_conjunction: bool,
}

// ============================================================================
// Builder Types
// ============================================================================

/// Builder for [`PriceCondition`].
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::{PriceCondition, TriggerMethod};
///
/// let condition = PriceCondition::builder(12345, "NASDAQ")
///     .greater_than(150.0)
///     .trigger_method(TriggerMethod::Last)
///     .conjunction(false)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct PriceConditionBuilder {
    contract_id: i32,
    exchange: String,
    price: Option<f64>,
    trigger_method: TriggerMethod,
    is_more: bool,
    is_conjunction: bool,
}

impl PriceCondition {
    /// Create a builder for a price condition.
    ///
    /// # Parameters
    ///
    /// - `contract_id`: Contract identifier for the instrument to monitor
    /// - `exchange`: Exchange where the price is monitored
    pub fn builder(contract_id: i32, exchange: impl Into<String>) -> PriceConditionBuilder {
        PriceConditionBuilder::new(contract_id, exchange)
    }
}

impl PriceConditionBuilder {
    /// Create a new price condition builder.
    ///
    /// # Parameters
    ///
    /// - `contract_id`: Contract identifier for the instrument to monitor
    /// - `exchange`: Exchange where the price is monitored
    pub fn new(contract_id: i32, exchange: impl Into<String>) -> Self {
        Self {
            contract_id,
            exchange: exchange.into(),
            price: None,                            // Must be set by greater_than/less_than
            trigger_method: TriggerMethod::Default, // Default trigger method
            is_more: true,                          // Default: trigger when price goes above
            is_conjunction: true,                   // Default: AND condition
        }
    }

    /// Set trigger when price is greater than the specified value.
    pub fn greater_than(mut self, price: f64) -> Self {
        self.price = Some(price);
        self.is_more = true;
        self
    }

    /// Set trigger when price is less than the specified value.
    pub fn less_than(mut self, price: f64) -> Self {
        self.price = Some(price);
        self.is_more = false;
        self
    }

    /// Set the trigger method for price evaluation.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ibapi::orders::conditions::{PriceCondition, TriggerMethod};
    ///
    /// let condition = PriceCondition::builder(12345, "NASDAQ")
    ///     .greater_than(150.0)
    ///     .trigger_method(TriggerMethod::Last)
    ///     .build();
    /// ```
    pub fn trigger_method(mut self, method: TriggerMethod) -> Self {
        self.trigger_method = method;
        self
    }

    /// Set whether this is an AND (conjunction) or OR (disjunction) condition.
    ///
    /// Default is `true` (AND).
    pub fn conjunction(mut self, is_conjunction: bool) -> Self {
        self.is_conjunction = is_conjunction;
        self
    }

    /// Build the price condition.
    pub fn build(self) -> PriceCondition {
        PriceCondition {
            contract_id: self.contract_id,
            exchange: self.exchange,
            price: self
                .price
                .expect("PriceConditionBuilder requires a price threshold; call greater_than() or less_than() before build()"),
            trigger_method: self.trigger_method,
            is_more: self.is_more,
            is_conjunction: self.is_conjunction,
        }
    }
}

/// Builder for [`TimeCondition`].
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::TimeCondition;
///
/// let condition = TimeCondition::builder()
///     .greater_than("20251230 23:59:59 UTC")
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct TimeConditionBuilder {
    time: Option<String>,
    is_more: bool,
    is_conjunction: bool,
}

impl TimeCondition {
    /// Create a builder for a time condition.
    pub fn builder() -> TimeConditionBuilder {
        TimeConditionBuilder::new()
    }
}

impl TimeConditionBuilder {
    /// Create a new time condition builder.
    pub fn new() -> Self {
        Self {
            time: None,           // Must be set by greater_than/less_than
            is_more: true,        // Default: trigger after time
            is_conjunction: true, // Default: AND condition
        }
    }

    /// Set trigger when time is greater than (after) the specified time.
    ///
    /// # Parameters
    ///
    /// - `time`: Time in format "YYYYMMDD HH:MM:SS TZ"
    pub fn greater_than(mut self, time: impl Into<String>) -> Self {
        self.time = Some(time.into());
        self.is_more = true;
        self
    }

    /// Set trigger when time is less than (before) the specified time.
    ///
    /// # Parameters
    ///
    /// - `time`: Time in format "YYYYMMDD HH:MM:SS TZ"
    pub fn less_than(mut self, time: impl Into<String>) -> Self {
        self.time = Some(time.into());
        self.is_more = false;
        self
    }

    /// Set whether this is an AND (conjunction) or OR (disjunction) condition.
    ///
    /// Default is `true` (AND).
    pub fn conjunction(mut self, is_conjunction: bool) -> Self {
        self.is_conjunction = is_conjunction;
        self
    }

    /// Build the time condition.
    pub fn build(self) -> TimeCondition {
        TimeCondition {
            time: self
                .time
                .expect("TimeConditionBuilder requires a time value; call greater_than() or less_than() before build()"),
            is_more: self.is_more,
            is_conjunction: self.is_conjunction,
        }
    }
}

impl Default for TimeConditionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for [`MarginCondition`].
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::MarginCondition;
///
/// let condition = MarginCondition::builder()
///     .less_than(30)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct MarginConditionBuilder {
    percent: Option<i32>,
    is_more: bool,
    is_conjunction: bool,
}

impl MarginCondition {
    /// Create a builder for a margin cushion condition.
    pub fn builder() -> MarginConditionBuilder {
        MarginConditionBuilder::new()
    }
}

impl MarginConditionBuilder {
    /// Create a new margin condition builder.
    pub fn new() -> Self {
        Self {
            percent: None,        // Must be set by greater_than/less_than
            is_more: true,        // Default: trigger when above threshold
            is_conjunction: true, // Default: AND condition
        }
    }

    /// Set trigger when margin cushion is greater than the specified percentage.
    pub fn greater_than(mut self, percent: i32) -> Self {
        self.percent = Some(percent);
        self.is_more = true;
        self
    }

    /// Set trigger when margin cushion is less than the specified percentage.
    pub fn less_than(mut self, percent: i32) -> Self {
        self.percent = Some(percent);
        self.is_more = false;
        self
    }

    /// Set whether this is an AND (conjunction) or OR (disjunction) condition.
    ///
    /// Default is `true` (AND).
    pub fn conjunction(mut self, is_conjunction: bool) -> Self {
        self.is_conjunction = is_conjunction;
        self
    }

    /// Build the margin condition.
    pub fn build(self) -> MarginCondition {
        MarginCondition {
            percent: self
                .percent
                .expect("MarginConditionBuilder requires a percentage threshold; call greater_than() or less_than() before build()"),
            is_more: self.is_more,
            is_conjunction: self.is_conjunction,
        }
    }
}

impl Default for MarginConditionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for [`ExecutionCondition`].
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::ExecutionCondition;
///
/// let condition = ExecutionCondition::builder("AAPL", "STK", "SMART")
///     .conjunction(false)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct ExecutionConditionBuilder {
    symbol: String,
    security_type: String,
    exchange: String,
    is_conjunction: bool,
}

impl ExecutionCondition {
    /// Create a builder for an execution condition.
    ///
    /// # Parameters
    ///
    /// - `symbol`: Symbol of the contract
    /// - `security_type`: Security type (e.g., "STK", "OPT")
    /// - `exchange`: Exchange where execution is monitored
    pub fn builder(symbol: impl Into<String>, security_type: impl Into<String>, exchange: impl Into<String>) -> ExecutionConditionBuilder {
        ExecutionConditionBuilder::new(symbol, security_type, exchange)
    }
}

impl ExecutionConditionBuilder {
    /// Create a new execution condition builder.
    ///
    /// # Parameters
    ///
    /// - `symbol`: Symbol of the contract
    /// - `security_type`: Security type (e.g., "STK", "OPT")
    /// - `exchange`: Exchange where execution is monitored
    pub fn new(symbol: impl Into<String>, security_type: impl Into<String>, exchange: impl Into<String>) -> Self {
        Self {
            symbol: symbol.into(),
            security_type: security_type.into(),
            exchange: exchange.into(),
            is_conjunction: true, // Default: AND condition
        }
    }

    /// Set whether this is an AND (conjunction) or OR (disjunction) condition.
    ///
    /// Default is `true` (AND).
    pub fn conjunction(mut self, is_conjunction: bool) -> Self {
        self.is_conjunction = is_conjunction;
        self
    }

    /// Build the execution condition.
    pub fn build(self) -> ExecutionCondition {
        ExecutionCondition {
            symbol: self.symbol,
            security_type: self.security_type,
            exchange: self.exchange,
            is_conjunction: self.is_conjunction,
        }
    }
}

/// Builder for [`VolumeCondition`].
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::VolumeCondition;
///
/// let condition = VolumeCondition::builder(12345, "NASDAQ")
///     .greater_than(1000000)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct VolumeConditionBuilder {
    contract_id: i32,
    exchange: String,
    volume: Option<i32>,
    is_more: bool,
    is_conjunction: bool,
}

impl VolumeCondition {
    /// Create a builder for a volume condition.
    ///
    /// # Parameters
    ///
    /// - `contract_id`: Contract identifier for the instrument to monitor
    /// - `exchange`: Exchange where volume is monitored
    pub fn builder(contract_id: i32, exchange: impl Into<String>) -> VolumeConditionBuilder {
        VolumeConditionBuilder::new(contract_id, exchange)
    }
}

impl VolumeConditionBuilder {
    /// Create a new volume condition builder.
    ///
    /// # Parameters
    ///
    /// - `contract_id`: Contract identifier for the instrument to monitor
    /// - `exchange`: Exchange where volume is monitored
    pub fn new(contract_id: i32, exchange: impl Into<String>) -> Self {
        Self {
            contract_id,
            exchange: exchange.into(),
            volume: None,         // Must be set by greater_than/less_than
            is_more: true,        // Default: trigger when above threshold
            is_conjunction: true, // Default: AND condition
        }
    }

    /// Set trigger when volume is greater than the specified value.
    pub fn greater_than(mut self, volume: i32) -> Self {
        self.volume = Some(volume);
        self.is_more = true;
        self
    }

    /// Set trigger when volume is less than the specified value.
    pub fn less_than(mut self, volume: i32) -> Self {
        self.volume = Some(volume);
        self.is_more = false;
        self
    }

    /// Set whether this is an AND (conjunction) or OR (disjunction) condition.
    ///
    /// Default is `true` (AND).
    pub fn conjunction(mut self, is_conjunction: bool) -> Self {
        self.is_conjunction = is_conjunction;
        self
    }

    /// Build the volume condition.
    pub fn build(self) -> VolumeCondition {
        VolumeCondition {
            contract_id: self.contract_id,
            exchange: self.exchange,
            volume: self
                .volume
                .expect("VolumeConditionBuilder requires a volume threshold; call greater_than() or less_than() before build()"),
            is_more: self.is_more,
            is_conjunction: self.is_conjunction,
        }
    }
}

/// Builder for [`PercentChangeCondition`].
///
/// # Example
///
/// ```no_run
/// use ibapi::orders::conditions::PercentChangeCondition;
///
/// let condition = PercentChangeCondition::builder(12345, "NASDAQ")
///     .greater_than(5.0)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct PercentChangeConditionBuilder {
    contract_id: i32,
    exchange: String,
    percent: Option<f64>,
    is_more: bool,
    is_conjunction: bool,
}

impl PercentChangeCondition {
    /// Create a builder for a percent change condition.
    ///
    /// # Parameters
    ///
    /// - `contract_id`: Contract identifier for the instrument to monitor
    /// - `exchange`: Exchange where price change is monitored
    pub fn builder(contract_id: i32, exchange: impl Into<String>) -> PercentChangeConditionBuilder {
        PercentChangeConditionBuilder::new(contract_id, exchange)
    }
}

impl PercentChangeConditionBuilder {
    /// Create a new percent change condition builder.
    ///
    /// # Parameters
    ///
    /// - `contract_id`: Contract identifier for the instrument to monitor
    /// - `exchange`: Exchange where price change is monitored
    pub fn new(contract_id: i32, exchange: impl Into<String>) -> Self {
        Self {
            contract_id,
            exchange: exchange.into(),
            percent: None,        // Must be set by greater_than/less_than
            is_more: true,        // Default: trigger when above threshold
            is_conjunction: true, // Default: AND condition
        }
    }

    /// Set trigger when percent change is greater than the specified value.
    pub fn greater_than(mut self, percent: f64) -> Self {
        self.percent = Some(percent);
        self.is_more = true;
        self
    }

    /// Set trigger when percent change is less than the specified value.
    pub fn less_than(mut self, percent: f64) -> Self {
        self.percent = Some(percent);
        self.is_more = false;
        self
    }

    /// Set whether this is an AND (conjunction) or OR (disjunction) condition.
    ///
    /// Default is `true` (AND).
    pub fn conjunction(mut self, is_conjunction: bool) -> Self {
        self.is_conjunction = is_conjunction;
        self
    }

    /// Build the percent change condition.
    pub fn build(self) -> PercentChangeCondition {
        PercentChangeCondition {
            contract_id: self.contract_id,
            exchange: self.exchange,
            percent: self
                .percent
                .expect("PercentChangeConditionBuilder requires a threshold; call greater_than() or less_than() before build()"),
            is_more: self.is_more,
            is_conjunction: self.is_conjunction,
        }
    }
}

// From implementations to convert builders to OrderCondition
impl From<PriceConditionBuilder> for crate::orders::OrderCondition {
    fn from(builder: PriceConditionBuilder) -> Self {
        crate::orders::OrderCondition::Price(builder.build())
    }
}

impl From<TimeConditionBuilder> for crate::orders::OrderCondition {
    fn from(builder: TimeConditionBuilder) -> Self {
        crate::orders::OrderCondition::Time(builder.build())
    }
}

impl From<MarginConditionBuilder> for crate::orders::OrderCondition {
    fn from(builder: MarginConditionBuilder) -> Self {
        crate::orders::OrderCondition::Margin(builder.build())
    }
}

impl From<VolumeConditionBuilder> for crate::orders::OrderCondition {
    fn from(builder: VolumeConditionBuilder) -> Self {
        crate::orders::OrderCondition::Volume(builder.build())
    }
}

impl From<PercentChangeConditionBuilder> for crate::orders::OrderCondition {
    fn from(builder: PercentChangeConditionBuilder) -> Self {
        crate::orders::OrderCondition::PercentChange(builder.build())
    }
}

impl From<ExecutionConditionBuilder> for crate::orders::OrderCondition {
    fn from(builder: ExecutionConditionBuilder) -> Self {
        crate::orders::OrderCondition::Execution(builder.build())
    }
}

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

    #[test]
    fn test_price_condition_builder() {
        let condition = PriceCondition::builder(12345, "NASDAQ")
            .greater_than(150.0)
            .trigger_method(TriggerMethod::DoubleBidAsk)
            .conjunction(false)
            .build();

        assert_eq!(condition.contract_id, 12345);
        assert_eq!(condition.exchange, "NASDAQ");
        assert_eq!(condition.price, 150.0);
        assert_eq!(condition.trigger_method, TriggerMethod::DoubleBidAsk);
        assert!(condition.is_more);
        assert!(!condition.is_conjunction);
    }

    #[test]
    fn test_time_condition_builder() {
        let condition = TimeCondition::builder().less_than("20251230 23:59:59 UTC").build();

        assert_eq!(condition.time, "20251230 23:59:59 UTC");
        assert!(!condition.is_more);
        assert!(condition.is_conjunction);
    }

    #[test]
    fn test_margin_condition_builder() {
        let condition = MarginCondition::builder().less_than(30).conjunction(false).build();

        assert_eq!(condition.percent, 30);
        assert!(!condition.is_more);
        assert!(!condition.is_conjunction);
    }

    #[test]
    fn test_execution_condition_builder() {
        let condition = ExecutionCondition::builder("AAPL", "STK", "SMART").conjunction(false).build();

        assert_eq!(condition.symbol, "AAPL");
        assert_eq!(condition.security_type, "STK");
        assert_eq!(condition.exchange, "SMART");
        assert!(!condition.is_conjunction);
    }

    #[test]
    fn test_volume_condition_builder() {
        let condition = VolumeCondition::builder(12345, "NASDAQ").less_than(1000000).build();

        assert_eq!(condition.contract_id, 12345);
        assert_eq!(condition.exchange, "NASDAQ");
        assert_eq!(condition.volume, 1000000);
        assert!(!condition.is_more);
        assert!(condition.is_conjunction);
    }

    #[test]
    fn test_percent_change_condition_builder() {
        let condition = PercentChangeCondition::builder(12345, "NASDAQ")
            .greater_than(5.0)
            .conjunction(false)
            .build();

        assert_eq!(condition.contract_id, 12345);
        assert_eq!(condition.exchange, "NASDAQ");
        assert_eq!(condition.percent, 5.0);
        assert!(condition.is_more);
        assert!(!condition.is_conjunction);
    }

    #[test]
    fn test_default_values() {
        let condition = PriceCondition::builder(12345, "NASDAQ").greater_than(150.0).build();

        assert_eq!(condition.trigger_method, TriggerMethod::Default);
        assert!(condition.is_more);
        assert!(condition.is_conjunction);
    }

    #[test]
    #[should_panic(expected = "PriceConditionBuilder requires a price threshold")]
    fn test_price_condition_builder_missing_threshold_panics() {
        let _ = PriceCondition::builder(12345, "NASDAQ").build();
    }

    #[test]
    #[should_panic(expected = "TimeConditionBuilder requires a time value")]
    fn test_time_condition_builder_missing_time_panics() {
        let _ = TimeCondition::builder().build();
    }

    #[test]
    #[should_panic(expected = "MarginConditionBuilder requires a percentage threshold")]
    fn test_margin_condition_builder_missing_threshold_panics() {
        let _ = MarginCondition::builder().build();
    }

    #[test]
    #[should_panic(expected = "VolumeConditionBuilder requires a volume threshold")]
    fn test_volume_condition_builder_missing_threshold_panics() {
        let _ = VolumeCondition::builder(12345, "NASDAQ").build();
    }

    #[test]
    #[should_panic(expected = "PercentChangeConditionBuilder requires a threshold")]
    fn test_percent_change_condition_builder_missing_threshold_panics() {
        let _ = PercentChangeCondition::builder(12345, "NASDAQ").build();
    }
}