webull_unofficial 1.1.1

The unofficial Rust interface for the WeBull API
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
// Builder patterns with client references for fluent async execution

use crate::{
    error::{Result, WebullError},
    models::*,
    WebullClient,
};
use std::future::Future;
use std::pin::Pin;

/// Login request builder that can be executed directly
pub struct LoginBuilderWithClient<'a> {
    client: &'a mut WebullClient,
    username: Option<String>,
    password: Option<String>,
    device_name: Option<String>,
    mfa_code: Option<String>,
    question_id: Option<String>,
    question_answer: Option<String>,
}

impl<'a> LoginBuilderWithClient<'a> {
    pub fn new(client: &'a mut WebullClient) -> Self {
        Self {
            client,
            username: None,
            password: None,
            device_name: None,
            mfa_code: None,
            question_id: None,
            question_answer: None,
        }
    }

    pub fn username(mut self, username: impl Into<String>) -> Self {
        self.username = Some(username.into());
        self
    }

    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self
    }

    pub fn device_name(mut self, device_name: impl Into<String>) -> Self {
        self.device_name = Some(device_name.into());
        self
    }

    pub fn mfa(mut self, code: impl Into<String>) -> Self {
        self.mfa_code = Some(code.into());
        self
    }

    pub fn security_question(
        mut self,
        question_id: impl Into<String>,
        answer: impl Into<String>,
    ) -> Self {
        self.question_id = Some(question_id.into());
        self.question_answer = Some(answer.into());
        self
    }
}

impl<'a> std::future::IntoFuture for LoginBuilderWithClient<'a> {
    type Output = Result<LoginResponse>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let username = self
                .username
                .ok_or_else(|| WebullError::InvalidRequest("username is required".to_string()))?;
            let password = self
                .password
                .ok_or_else(|| WebullError::InvalidRequest("password is required".to_string()))?;

            self.client
                .login(
                    &username,
                    &password,
                    self.device_name.as_deref(),
                    self.mfa_code.as_deref(),
                    self.question_id.as_deref(),
                    self.question_answer.as_deref(),
                )
                .await
        })
    }
}

/// Bars request builder that can be executed directly
pub struct BarsRequestBuilderWithClient<'a> {
    client: &'a WebullClient,
    ticker_id: Option<String>,
    interval: Option<String>,
    count: Option<i32>,
    timestamp: Option<i64>,
}

impl<'a> BarsRequestBuilderWithClient<'a> {
    pub fn new(client: &'a WebullClient) -> Self {
        Self {
            client,
            ticker_id: None,
            interval: None,
            count: Some(100),
            timestamp: None,
        }
    }

    pub fn ticker_id(mut self, ticker_id: impl Into<String>) -> Self {
        self.ticker_id = Some(ticker_id.into());
        self
    }

    pub fn interval(mut self, interval: impl Into<String>) -> Self {
        self.interval = Some(interval.into());
        self
    }

    pub fn count(mut self, count: i32) -> Self {
        self.count = Some(count);
        self
    }

    pub fn timestamp(mut self, timestamp: i64) -> Self {
        self.timestamp = Some(timestamp);
        self
    }

    pub fn from_date(mut self, date: chrono::DateTime<chrono::Utc>) -> Self {
        self.timestamp = Some(date.timestamp());
        self
    }
}

impl<'a> std::future::IntoFuture for BarsRequestBuilderWithClient<'a> {
    type Output = Result<Vec<Bar>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let ticker_id = self
                .ticker_id
                .ok_or_else(|| WebullError::InvalidRequest("ticker_id is required".to_string()))?;
            let interval = self
                .interval
                .ok_or_else(|| WebullError::InvalidRequest("interval is required".to_string()))?;
            let count = self.count.unwrap_or(100);

            self.client
                .get_bars(&ticker_id, &interval, count, self.timestamp)
                .await
        })
    }
}

/// News request builder that can be executed directly
pub struct NewsRequestBuilderWithClient<'a> {
    client: &'a WebullClient,
    ticker: Option<String>,
    last_id: Option<i64>,
    count: Option<i32>,
}

impl<'a> NewsRequestBuilderWithClient<'a> {
    pub fn new(client: &'a WebullClient) -> Self {
        Self {
            client,
            ticker: None,
            last_id: Some(0),
            count: Some(20),
        }
    }

    pub fn ticker(mut self, ticker: impl Into<String>) -> Self {
        self.ticker = Some(ticker.into());
        self
    }

    pub fn last_id(mut self, last_id: i64) -> Self {
        self.last_id = Some(last_id);
        self
    }

    pub fn after(mut self, news_id: i64) -> Self {
        self.last_id = Some(news_id);
        self
    }

    pub fn count(mut self, count: i32) -> Self {
        self.count = Some(count);
        self
    }

    pub fn latest(mut self, count: i32) -> Self {
        self.last_id = Some(0);
        self.count = Some(count);
        self
    }
}

impl<'a> std::future::IntoFuture for NewsRequestBuilderWithClient<'a> {
    type Output = Result<Vec<News>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let ticker = self
                .ticker
                .ok_or_else(|| WebullError::InvalidRequest("ticker is required".to_string()))?;
            let last_id = self.last_id.unwrap_or(0);
            let count = self.count.unwrap_or(20);

            self.client.get_news(&ticker, last_id, count).await
        })
    }
}

/// Options request builder that can be executed directly
pub struct OptionsRequestBuilderWithClient<'a> {
    client: &'a WebullClient,
    ticker: Option<String>,
    expiration_date: Option<String>,
    option_type: Option<String>,
    min_strike: Option<f64>,
    max_strike: Option<f64>,
}

impl<'a> OptionsRequestBuilderWithClient<'a> {
    pub fn new(client: &'a WebullClient) -> Self {
        Self {
            client,
            ticker: None,
            expiration_date: None,
            option_type: None,
            min_strike: None,
            max_strike: None,
        }
    }

    pub fn ticker(mut self, ticker: impl Into<String>) -> Self {
        self.ticker = Some(ticker.into());
        self
    }

    pub fn expiration(mut self, date: impl Into<String>) -> Self {
        self.expiration_date = Some(date.into());
        self
    }

    pub fn calls_only(mut self) -> Self {
        self.option_type = Some("CALL".to_string());
        self
    }

    pub fn puts_only(mut self) -> Self {
        self.option_type = Some("PUT".to_string());
        self
    }

    pub fn min_strike(mut self, price: f64) -> Self {
        self.min_strike = Some(price);
        self
    }

    pub fn max_strike(mut self, price: f64) -> Self {
        self.max_strike = Some(price);
        self
    }

    pub fn strike_range(mut self, min: f64, max: f64) -> Self {
        self.min_strike = Some(min);
        self.max_strike = Some(max);
        self
    }

    pub fn near_the_money(mut self, current_price: f64, percent_range: f64) -> Self {
        let range = current_price * (percent_range / 100.0);
        self.min_strike = Some(current_price - range);
        self.max_strike = Some(current_price + range);
        self
    }
}

impl<'a> std::future::IntoFuture for OptionsRequestBuilderWithClient<'a> {
    type Output = Result<Vec<OptionContract>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let ticker = self
                .ticker
                .ok_or_else(|| WebullError::InvalidRequest("ticker is required".to_string()))?;

            self.client.get_options(&ticker).await
        })
    }
}

/// Order builder that can be executed directly
pub struct PlaceOrderBuilderWithClient<'a> {
    client: &'a WebullClient,
    ticker_id: Option<i64>,
    action: Option<OrderAction>,
    order_type: Option<OrderType>, // Made optional for auto-detection
    time_in_force: TimeInForce,
    quantity: Option<f64>,
    limit_price: Option<f64>,
    stop_price: Option<f64>,
    outside_regular_trading_hour: bool,
    serial_id: Option<String>,
    combo_type: Option<String>,
}

impl<'a> PlaceOrderBuilderWithClient<'a> {
    /// Create a new order builder with automatic type detection
    pub fn new(client: &'a WebullClient) -> Self {
        Self {
            client,
            ticker_id: None,
            action: None,
            order_type: None, // Will be auto-detected
            time_in_force: TimeInForce::Day,
            quantity: None,
            limit_price: None,
            stop_price: None,
            outside_regular_trading_hour: false,
            serial_id: None,
            combo_type: None,
        }
    }

    pub fn market(client: &'a WebullClient) -> Self {
        Self::new_with_type(client, OrderType::Market)
    }

    pub fn limit_order(client: &'a WebullClient, price: f64) -> Self {
        Self::new_with_type(client, OrderType::Limit).limit(price)
    }

    pub fn stop_order(client: &'a WebullClient, price: f64) -> Self {
        Self::new_with_type(client, OrderType::Stop).stop(price)
    }

    pub fn stop_limit_order(client: &'a WebullClient, stop_price: f64, limit_price: f64) -> Self {
        Self::new_with_type(client, OrderType::StopLimit)
            .stop(stop_price)
            .limit(limit_price)
    }

    fn new_with_type(client: &'a WebullClient, order_type: OrderType) -> Self {
        Self {
            client,
            ticker_id: None,
            action: None,
            order_type: Some(order_type),
            time_in_force: TimeInForce::Day,
            quantity: None,
            limit_price: None,
            stop_price: None,
            outside_regular_trading_hour: false,
            serial_id: None,
            combo_type: None,
        }
    }

    pub fn ticker_id(mut self, ticker_id: i64) -> Self {
        self.ticker_id = Some(ticker_id);
        self
    }

    pub fn action(mut self, action: OrderAction) -> Self {
        self.action = Some(action);
        self
    }

    pub fn buy(mut self) -> Self {
        self.action = Some(OrderAction::Buy);
        self
    }

    pub fn sell(mut self) -> Self {
        self.action = Some(OrderAction::Sell);
        self
    }

    pub fn quantity(mut self, quantity: f64) -> Self {
        self.quantity = Some(quantity);
        self
    }

    pub fn time_in_force(mut self, tif: TimeInForce) -> Self {
        self.time_in_force = tif;
        self
    }

    /// Set limit price (for limit and stop-limit orders)
    pub fn limit(mut self, price: f64) -> Self {
        self.limit_price = Some(price);
        self
    }

    /// Set stop price (for stop and stop-limit orders)
    pub fn stop(mut self, price: f64) -> Self {
        self.stop_price = Some(price);
        self
    }

    /// Alias for limit() - for backwards compatibility
    pub fn limit_price(self, price: f64) -> Self {
        self.limit(price)
    }

    /// Alias for stop() - for backwards compatibility
    pub fn stop_price(self, price: f64) -> Self {
        self.stop(price)
    }

    pub fn extended_hours(mut self) -> Self {
        self.outside_regular_trading_hour = true;
        self
    }

    pub fn serial_id(mut self, id: String) -> Self {
        self.serial_id = Some(id);
        self
    }

    pub fn combo_type(mut self, combo_type: String) -> Self {
        self.combo_type = Some(combo_type);
        self
    }
}

impl<'a> std::future::IntoFuture for PlaceOrderBuilderWithClient<'a> {
    type Output = Result<String>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let ticker_id = self
                .ticker_id
                .ok_or_else(|| WebullError::InvalidRequest("ticker_id is required".to_string()))?;
            let action = self
                .action
                .ok_or_else(|| WebullError::InvalidRequest("action is required".to_string()))?;
            let quantity = self
                .quantity
                .ok_or_else(|| WebullError::InvalidRequest("quantity is required".to_string()))?;

            // Auto-detect order type if not explicitly set
            let order_type = if let Some(order_type) = self.order_type {
                order_type
            } else {
                // Detect based on which prices are set
                match (self.limit_price.is_some(), self.stop_price.is_some()) {
                    (true, true) => OrderType::StopLimit,
                    (true, false) => OrderType::Limit,
                    (false, true) => OrderType::Stop,
                    (false, false) => OrderType::Market,
                }
            };

            // Validate order type specific requirements
            match order_type {
                OrderType::Limit => {
                    if self.limit_price.is_none() {
                        return Err(WebullError::InvalidRequest(format!(
                            "{:?} order requires limit_price",
                            order_type
                        )));
                    }
                }
                OrderType::Stop => {
                    if self.stop_price.is_none() {
                        return Err(WebullError::InvalidRequest(
                            "Stop order requires stop_price".to_string(),
                        ));
                    }
                }
                OrderType::StopLimit => {
                    if self.limit_price.is_none() {
                        return Err(WebullError::InvalidRequest(
                            "StopLimit order requires limit_price".to_string(),
                        ));
                    }
                    if self.stop_price.is_none() {
                        return Err(WebullError::InvalidRequest(
                            "StopLimit order requires stop_price".to_string(),
                        ));
                    }
                }
                _ => {}
            }

            let order = PlaceOrderRequest {
                ticker_id,
                action,
                order_type,
                time_in_force: self.time_in_force,
                quantity,
                limit_price: self.limit_price,
                stop_price: self.stop_price,
                outside_regular_trading_hour: self.outside_regular_trading_hour,
                serial_id: self.serial_id,
                combo_type: self.combo_type,
            };

            self.client.place_order(&order).await
        })
    }
}

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

    #[test]
    fn test_login_builder_with_client() {
        let mut client = WebullClient::new_paper(Some(6)).unwrap();
        let builder = LoginBuilderWithClient::new(&mut client);

        // Test builder pattern
        let builder = builder
            .username("test@example.com")
            .password("testpass")
            .device_name("test_device")
            .mfa("123456");

        // Verify fields are set
        assert_eq!(builder.username, Some("test@example.com".to_string()));
        assert_eq!(builder.password, Some("testpass".to_string()));
        assert_eq!(builder.device_name, Some("test_device".to_string()));
        assert_eq!(builder.mfa_code, Some("123456".to_string()));
    }

    #[test]
    fn test_bars_request_builder() {
        let client = WebullClient::new_paper(Some(6)).unwrap();
        let builder = BarsRequestBuilderWithClient::new(&client);

        // Test builder pattern
        let builder = builder
            .ticker_id("913256135")
            .interval("5m")
            .count(100)
            .timestamp(1234567890);

        // Verify fields are set
        assert_eq!(builder.ticker_id, Some("913256135".to_string()));
        assert_eq!(builder.interval, Some("5m".to_string()));
        assert_eq!(builder.count, Some(100));
        assert_eq!(builder.timestamp, Some(1234567890));
    }

    #[test]
    fn test_bars_builder_from_date() {
        let client = WebullClient::new_paper(Some(6)).unwrap();
        let builder = BarsRequestBuilderWithClient::new(&client);

        // Test from_date conversion
        let date = chrono::Utc::now();
        let builder = builder.from_date(date);

        // Verify timestamp is set
        assert!(builder.timestamp.is_some());
        assert_eq!(builder.timestamp.unwrap(), date.timestamp());
    }

    #[test]
    fn test_news_request_builder() {
        let client = WebullClient::new_paper(Some(6)).unwrap();
        let builder = NewsRequestBuilderWithClient::new(&client);

        // Test builder pattern
        let builder = builder.ticker("AAPL").latest(20).after(9876543210);

        // Verify fields are set
        assert_eq!(builder.ticker, Some("AAPL".to_string()));
        assert_eq!(builder.count, Some(20));
        assert_eq!(builder.last_id, Some(9876543210));
    }

    #[test]
    fn test_news_builder_convenience_methods() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test latest() convenience method
        let builder = NewsRequestBuilderWithClient::new(&client).latest(10);
        assert_eq!(builder.count, Some(10));
        assert_eq!(builder.last_id, Some(0));

        // Test after() method
        let builder = NewsRequestBuilderWithClient::new(&client).after(123);
        assert_eq!(builder.last_id, Some(123));
    }

    #[test]
    fn test_options_request_builder() {
        let client = WebullClient::new_paper(Some(6)).unwrap();
        let builder = OptionsRequestBuilderWithClient::new(&client);

        // Test builder pattern
        let builder = builder
            .ticker("SPY")
            .calls_only()
            .near_the_money(450.0, 5.0);

        // Verify fields are set
        assert_eq!(builder.ticker, Some("SPY".to_string()));
        // Options builder uses option_type field instead of filter_calls/puts
        assert_eq!(builder.option_type, Some("CALL".to_string()));
        assert_eq!(builder.min_strike, Some(427.5)); // 450 * 0.95
        assert_eq!(builder.max_strike, Some(472.5)); // 450 * 1.05
    }

    #[test]
    fn test_options_builder_filters() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test calls_only
        let builder = OptionsRequestBuilderWithClient::new(&client).calls_only();
        assert_eq!(builder.option_type, Some("CALL".to_string()));

        // Test puts_only
        let builder = OptionsRequestBuilderWithClient::new(&client).puts_only();
        assert_eq!(builder.option_type, Some("PUT".to_string()));

        // Test near_the_money calculation
        let builder = OptionsRequestBuilderWithClient::new(&client).near_the_money(100.0, 10.0);
        assert_eq!(builder.min_strike, Some(90.0));
        assert_eq!(builder.max_strike, Some(110.0));
    }

    #[test]
    fn test_place_order_builder_auto_detect() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test auto-detection with new()
        let builder = PlaceOrderBuilderWithClient::new(&client);
        assert_eq!(builder.order_type, None);

        // Market order (no prices)
        let builder = PlaceOrderBuilderWithClient::new(&client)
            .ticker_id(123)
            .buy()
            .quantity(10.0);
        assert_eq!(builder.limit_price, None);
        assert_eq!(builder.stop_price, None);

        // Limit order (limit price only)
        let builder = PlaceOrderBuilderWithClient::new(&client)
            .ticker_id(123)
            .limit(150.0)
            .buy()
            .quantity(10.0);
        assert_eq!(builder.limit_price, Some(150.0));
        assert_eq!(builder.stop_price, None);

        // Stop order (stop price only)
        let builder = PlaceOrderBuilderWithClient::new(&client)
            .ticker_id(123)
            .stop(145.0)
            .sell()
            .quantity(10.0);
        assert_eq!(builder.limit_price, None);
        assert_eq!(builder.stop_price, Some(145.0));

        // Stop-limit order (both prices)
        let builder = PlaceOrderBuilderWithClient::new(&client)
            .ticker_id(123)
            .limit(144.0)
            .stop(145.0)
            .sell()
            .quantity(10.0);
        assert_eq!(builder.limit_price, Some(144.0));
        assert_eq!(builder.stop_price, Some(145.0));
    }

    #[test]
    fn test_order_type_auto_detection_in_builder() {
        // This test verifies that the auto-detection logic correctly determines
        // the order type based on which prices are set
        
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test 1: Market order (no prices) should detect OrderType::Market
        {
            let builder = PlaceOrderBuilderWithClient::new(&client)
                .ticker_id(123)
                .buy()
                .quantity(10.0);
            
            // The order type should be None initially
            assert_eq!(builder.order_type, None);
            
            // Simulate what happens in IntoFuture
            let detected_type = match (builder.limit_price.is_some(), builder.stop_price.is_some()) {
                (true, true) => OrderType::StopLimit,
                (true, false) => OrderType::Limit,
                (false, true) => OrderType::Stop,
                (false, false) => OrderType::Market,
            };
            assert_eq!(detected_type, OrderType::Market);
        }

        // Test 2: Limit order (limit price only) should detect OrderType::Limit
        {
            let builder = PlaceOrderBuilderWithClient::new(&client)
                .ticker_id(123)
                .limit(150.0)
                .buy()
                .quantity(10.0);
            
            let detected_type = match (builder.limit_price.is_some(), builder.stop_price.is_some()) {
                (true, true) => OrderType::StopLimit,
                (true, false) => OrderType::Limit,
                (false, true) => OrderType::Stop,
                (false, false) => OrderType::Market,
            };
            assert_eq!(detected_type, OrderType::Limit);
        }

        // Test 3: Stop order (stop price only) should detect OrderType::Stop
        {
            let builder = PlaceOrderBuilderWithClient::new(&client)
                .ticker_id(123)
                .stop(145.0)
                .sell()
                .quantity(10.0);
            
            let detected_type = match (builder.limit_price.is_some(), builder.stop_price.is_some()) {
                (true, true) => OrderType::StopLimit,
                (true, false) => OrderType::Limit,
                (false, true) => OrderType::Stop,
                (false, false) => OrderType::Market,
            };
            assert_eq!(detected_type, OrderType::Stop);
        }

        // Test 4: Stop-Limit order (both prices) should detect OrderType::StopLimit
        {
            let builder = PlaceOrderBuilderWithClient::new(&client)
                .ticker_id(123)
                .limit(144.0)
                .stop(145.0)
                .sell()
                .quantity(10.0);
            
            let detected_type = match (builder.limit_price.is_some(), builder.stop_price.is_some()) {
                (true, true) => OrderType::StopLimit,
                (true, false) => OrderType::Limit,
                (false, true) => OrderType::Stop,
                (false, false) => OrderType::Market,
            };
            assert_eq!(detected_type, OrderType::StopLimit);
        }
    }

    #[test]
    fn test_place_order_builder_explicit_types() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test market order constructor
        let builder = PlaceOrderBuilderWithClient::market(&client);
        assert_eq!(builder.order_type, Some(OrderType::Market));

        // Test limit order constructor
        let builder = PlaceOrderBuilderWithClient::limit_order(&client, 150.0);
        assert_eq!(builder.order_type, Some(OrderType::Limit));
        assert_eq!(builder.limit_price, Some(150.0));

        // Test stop order constructor
        let builder = PlaceOrderBuilderWithClient::stop_order(&client, 145.0);
        assert_eq!(builder.order_type, Some(OrderType::Stop));
        assert_eq!(builder.stop_price, Some(145.0));

        // Test stop-limit order constructor
        let builder = PlaceOrderBuilderWithClient::stop_limit_order(&client, 145.0, 144.0);
        assert_eq!(builder.order_type, Some(OrderType::StopLimit));
        assert_eq!(builder.stop_price, Some(145.0));
        assert_eq!(builder.limit_price, Some(144.0));
    }

    #[test]
    fn test_place_order_builder_actions() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test buy() method
        let builder = PlaceOrderBuilderWithClient::new(&client).buy();
        assert_eq!(builder.action, Some(OrderAction::Buy));

        // Test sell() method
        let builder = PlaceOrderBuilderWithClient::new(&client).sell();
        assert_eq!(builder.action, Some(OrderAction::Sell));

        // Test action() method
        let builder = PlaceOrderBuilderWithClient::new(&client).action(OrderAction::Buy);
        assert_eq!(builder.action, Some(OrderAction::Buy));
    }

    #[test]
    fn test_place_order_builder_options() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        let builder = PlaceOrderBuilderWithClient::new(&client)
            .ticker_id(123)
            .quantity(10.0)
            .time_in_force(TimeInForce::GoodTillCancel)
            .extended_hours()
            .serial_id("test_serial".to_string())
            .combo_type("test_combo".to_string());

        assert_eq!(builder.ticker_id, Some(123));
        assert_eq!(builder.quantity, Some(10.0));
        assert_eq!(builder.time_in_force, TimeInForce::GoodTillCancel);
        assert_eq!(builder.outside_regular_trading_hour, true);
        assert_eq!(builder.serial_id, Some("test_serial".to_string()));
        assert_eq!(builder.combo_type, Some("test_combo".to_string()));
    }

    #[test]
    fn test_place_order_builder_aliases() {
        let client = WebullClient::new_paper(Some(6)).unwrap();

        // Test that limit_price and limit do the same thing
        let builder1 = PlaceOrderBuilderWithClient::new(&client).limit(150.0);
        let builder2 = PlaceOrderBuilderWithClient::new(&client).limit_price(150.0);
        assert_eq!(builder1.limit_price, builder2.limit_price);

        // Test that stop_price and stop do the same thing
        let builder1 = PlaceOrderBuilderWithClient::new(&client).stop(145.0);
        let builder2 = PlaceOrderBuilderWithClient::new(&client).stop_price(145.0);
        assert_eq!(builder1.stop_price, builder2.stop_price);
    }

    #[test]
    fn test_order_type_detection_logic() {
        // This tests the exact logic that would be used in IntoFuture
        fn detect_order_type(has_limit: bool, has_stop: bool) -> OrderType {
            match (has_limit, has_stop) {
                (true, true) => OrderType::StopLimit,
                (true, false) => OrderType::Limit,
                (false, true) => OrderType::Stop,
                (false, false) => OrderType::Market,
            }
        }

        assert_eq!(detect_order_type(false, false), OrderType::Market);
        assert_eq!(detect_order_type(true, false), OrderType::Limit);
        assert_eq!(detect_order_type(false, true), OrderType::Stop);
        assert_eq!(detect_order_type(true, true), OrderType::StopLimit);
    }
}