schwab_api 0.0.4

An unofficial rust library for Schwab 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
use derive_builder::Builder;
use serde::Deserialize;
use serde::Serialize;
use serde_with::skip_serializing_none;

use super::accounts::AccountsInstrument;
use super::order::ComplexOrderStrategyType;
use super::order::Duration;
use super::order::Order;
use super::order::OrderActivity;
use super::order::OrderLegCollection;
use super::order::OrderStrategyType;
use super::order::OrderType;
use super::order::PriceLinkBasis;
use super::order::PriceLinkType;
use super::order::Session;
use super::order::SpecialInstruction;
use super::order::Status;
use super::order::StopPriceLinkBasis;
use super::order::StopPriceLinkType;
use super::order::StopType;
use super::order::TaxLotMethod;
use super::preview_order::Instruction;
use crate::Error;
use crate::model::InstrumentResponse;
use crate::model::market_data::instrument::InstrumentAssetType;

/// More Info in [Charles Schwab Developer Portal](https://developer.schwab.com/) : API Products -> Trader API - Individual -> Accounts and Trading Production -> Documentation -> Place Order Samples
#[skip_serializing_none]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[builder(setter(strip_option), default)]
#[serde(rename_all = "camelCase")]
pub struct OrderRequest {
    pub session: Option<Session>,
    pub duration: Option<Duration>,
    pub order_type: Option<OrderTypeRequest>,
    pub cancel_time: Option<chrono::DateTime<chrono::Utc>>,
    pub complex_order_strategy_type: Option<ComplexOrderStrategyType>,
    pub quantity: Option<f64>,
    pub filled_quantity: Option<f64>,
    pub remaining_quantity: Option<f64>,
    pub destination_link_name: Option<String>,
    pub release_time: Option<chrono::DateTime<chrono::Utc>>,
    pub stop_price: Option<f64>,
    pub stop_price_link_basis: Option<StopPriceLinkBasis>,
    pub stop_price_link_type: Option<StopPriceLinkType>,
    pub stop_price_offset: Option<f64>,
    pub stop_type: Option<StopType>,
    pub price_link_basis: Option<PriceLinkBasis>,
    pub price_link_type: Option<PriceLinkType>,
    pub price: Option<f64>,
    pub tax_lot_method: Option<TaxLotMethod>,
    /// xml: `OrderedMap` { "name": "orderLegCollection", "wrapped": true }
    pub order_leg_collection: Option<Vec<OrderLegCollectionRequest>>,
    pub activation_price: Option<f64>,
    pub special_instruction: Option<SpecialInstruction>,
    pub order_strategy_type: OrderStrategyType,
    pub order_id: Option<i64>,
    /// default: false
    pub cancelable: Option<bool>,
    /// default: false
    pub editable: Option<bool>,
    pub status: Option<Status>,
    pub entered_time: Option<chrono::DateTime<chrono::Utc>>,
    pub close_time: Option<chrono::DateTime<chrono::Utc>>,
    pub account_number: Option<i64>,
    /// xml: `OrderedMap` { "name": "orderActivity", "wrapped": true }
    pub order_activity_collection: Option<Vec<OrderActivity>>,
    /// xml: `OrderedMap` { "name": "replacingOrder", "wrapped": true }
    pub replacing_order_collection: Option<Vec<String>>,
    /// xml: `OrderedMap` { "name": "childOrder", "wrapped": true }
    pub child_order_strategies: Option<Vec<OrderRequest>>,
    pub status_description: Option<String>,
}

impl From<Order> for OrderRequest {
    fn from(value: Order) -> Self {
        Self {
            session: Some(value.session),
            duration: Some(value.duration),
            order_type: Some(value.order_type.into()),
            cancel_time: value.cancel_time,
            complex_order_strategy_type: Some(value.complex_order_strategy_type),
            quantity: Some(value.quantity),
            filled_quantity: Some(value.filled_quantity),
            remaining_quantity: Some(value.remaining_quantity),
            destination_link_name: Some(value.destination_link_name),
            release_time: value.release_time,
            stop_price: value.stop_price,
            stop_price_link_basis: value.stop_price_link_basis,
            stop_price_link_type: value.stop_price_link_type,
            stop_price_offset: value.stop_price_offset,
            stop_type: value.stop_type,
            price_link_basis: value.price_link_basis,
            price_link_type: value.price_link_type,
            price: Some(value.price),
            tax_lot_method: value.tax_lot_method,
            order_leg_collection: Some(
                value
                    .order_leg_collection
                    .into_iter()
                    .map(Into::into)
                    .collect(),
            ),
            activation_price: value.activation_price,
            special_instruction: value.special_instruction,
            order_strategy_type: value.order_strategy_type,
            order_id: Some(value.order_id),
            cancelable: Some(value.cancelable),
            editable: Some(value.editable),
            status: Some(value.status),
            entered_time: Some(value.entered_time),
            close_time: value.close_time,
            account_number: Some(value.account_number),
            order_activity_collection: value.order_activity_collection,
            replacing_order_collection: value.replacing_order_collection,
            child_order_strategies: value
                .child_order_strategies
                .map(|orders| orders.into_iter().map(Into::into).collect()),
            status_description: value.status_description,
        }
    }
}

impl OrderRequest {
    /// Create a market order.
    pub fn market(
        symbol: InstrumentRequest,
        instruction: Instruction,
        quantity: f64,
    ) -> Result<Self, Error> {
        let order_leg_collection = vec![OrderLegCollectionRequest {
            instruction,
            quantity,
            instrument: symbol,
        }];
        OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::Market)
            .session(Session::Normal)
            .duration(Duration::Day)
            .order_strategy_type(OrderStrategyType::Single)
            .order_leg_collection(order_leg_collection)
            .build()
            .map_err(Error::OrderRequestBuild)
    }

    /// Create a limit order.
    pub fn limit(
        symbol: InstrumentRequest,
        instruction: Instruction,
        quantity: f64,
        price: f64,
    ) -> Result<Self, Error> {
        let order_leg_collection = vec![OrderLegCollectionRequest {
            instruction,
            quantity,
            instrument: symbol,
        }];
        OrderRequestBuilder::default()
            .complex_order_strategy_type(ComplexOrderStrategyType::None)
            .order_type(OrderTypeRequest::Limit)
            .session(Session::Normal)
            .price(price)
            .duration(Duration::Day)
            .order_strategy_type(OrderStrategyType::Single)
            .order_leg_collection(order_leg_collection)
            .build()
            .map_err(Error::OrderRequestBuild)
    }
}

/// Same as `super::order::OrderType`, but does not have UNKNOWN since this type is not allowed as an input
/// Type of order to place.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderTypeRequest {
    /// Execute the order immediately at the best-available price.
    /// `More Info <https://www.investopedia.com/terms/m/marketorder.asp>`__.
    Market,
    #[default]
    /// Execute the order at your price or better.
    /// `More info <https://www.investopedia.com/terms/l/limitorder.asp>`__.
    Limit,
    /// Wait until the price reaches the stop price, and then immediately place a
    /// market order.
    /// `More Info <https://www.investopedia.com/terms/l/limitorder.asp>`__.
    Stop,
    /// Wait until the price reaches the stop price, and then immediately place a
    /// limit order at the specified price.
    /// `More Info <https://www.investopedia.com/terms/s/stop-limitorder.asp>`__.
    StopLimit,
    /// Similar to ``STOP``, except if the price moves in your favor, the stop
    /// price is adjusted in that direction. Places a market order if the stop
    /// condition is met.
    /// `More info <https://www.investopedia.com/terms/t/trailingstop.asp>`__.
    TrailingStop,
    Cabinet,
    NonMarketable,
    /// Place the order at the closing price immediately upon market close.
    /// `More info <https://www.investopedia.com/terms/m/marketonclose.asp>`__
    MarketOnClose,
    /// Exercise an option.
    Exercise,
    /// Similar to ``STOP_LIMIT``, except if the price moves in your favor, the
    /// stop price is adjusted in that direction. Places a limit order at the
    /// specified price if the stop condition is met.
    /// `More info <https://www.investopedia.com/terms/t/trailingstop.asp>`__.
    TrailingStopLimit,
    /// Place an order for an options spread resulting in a net debit.
    /// `More info <https://www.investopedia.com/ask/answers/042215/whats-difference-between-credit-spread-and-debt-spread.asp>`__
    NetDebit,
    /// Place an order for an options spread resulting in a net credit.
    /// `More info <https://www.investopedia.com/ask/answers/042215/whats-difference-between-credit-spread-and-debt-spread.asp>`__
    NetCredit,
    /// Place an order for an options spread resulting in neither a credit nor a
    /// debit.
    /// `More info <https://www.investopedia.com/ask/answers/042215/whats-difference-between-credit-spread-and-debt-spread.asp>`__
    NetZero,
    LimitOnClose,
}

impl From<OrderType> for OrderTypeRequest {
    fn from(value: OrderType) -> Self {
        match value {
            OrderType::Market => OrderTypeRequest::Market,
            OrderType::Limit => OrderTypeRequest::Limit,
            OrderType::Stop => OrderTypeRequest::Stop,
            OrderType::StopLimit => OrderTypeRequest::StopLimit,
            OrderType::TrailingStop => OrderTypeRequest::TrailingStop,
            OrderType::Cabinet => OrderTypeRequest::Cabinet,
            OrderType::NonMarketable => OrderTypeRequest::NonMarketable,
            OrderType::MarketOnClose => OrderTypeRequest::MarketOnClose,
            OrderType::Exercise => OrderTypeRequest::Exercise,
            OrderType::TrailingStopLimit => OrderTypeRequest::TrailingStopLimit,
            OrderType::NetDebit => OrderTypeRequest::NetDebit,
            OrderType::NetCredit => OrderTypeRequest::NetCredit,
            OrderType::NetZero => OrderTypeRequest::NetZero,
            OrderType::LimitOnClose => OrderTypeRequest::LimitOnClose,
            OrderType::Unknown => panic!("Unknown"),
        }
    }
}

/// Similar to `super::order::OrderLegCollection`, but more simple
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderLegCollectionRequest {
    pub instrument: InstrumentRequest,
    pub instruction: Instruction,
    pub quantity: f64,
}

impl From<OrderLegCollection> for OrderLegCollectionRequest {
    fn from(value: OrderLegCollection) -> Self {
        Self {
            instrument: value.instrument.into(),
            instruction: value.instruction,
            quantity: value.quantity,
        }
    }
}

/// Similar to `super::accounts::AccountsInstrument`, but more simple
/// only support Equity, Option now in schwab API
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "assetType", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum InstrumentRequest {
    Equity { symbol: String },
    Option { symbol: String },
}

impl From<AccountsInstrument> for InstrumentRequest {
    fn from(value: AccountsInstrument) -> Self {
        match value {
            AccountsInstrument::CashEquivalent(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::Equity(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::FixedIncome(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::MutualFund(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::Option(x) => Self::Option {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::Index(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::Currency(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
            AccountsInstrument::CollectiveInvestment(x) => Self::Equity {
                symbol: x.accounts_base_instrument.symbol,
            },
        }
    }
}

impl From<InstrumentResponse> for InstrumentRequest {
    fn from(value: InstrumentResponse) -> Self {
        match value.asset_type {
            InstrumentAssetType::Bond
            | InstrumentAssetType::Equity
            | InstrumentAssetType::Etf
            | InstrumentAssetType::Extended
            | InstrumentAssetType::Forex
            | InstrumentAssetType::Future
            | InstrumentAssetType::Fundamental
            | InstrumentAssetType::Index
            | InstrumentAssetType::Indicator
            | InstrumentAssetType::MutualFund
            | InstrumentAssetType::Unknown => Self::Equity {
                symbol: value.symbol,
            },
            InstrumentAssetType::FutureOption | InstrumentAssetType::Option => Self::Option {
                symbol: value.symbol,
            },
        }
    }
}

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

    use assert_json_diff::{CompareMode, Config, NumericMode, assert_json_matches};
    use serde_json::json;

    #[test]
    fn test_de() {
        let json = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/model/Trader/OrderRequest.json"
        ));

        let val = serde_json::from_str::<OrderRequest>(json);
        println!("{val:?}");
        assert!(val.is_ok());
    }

    #[test]
    fn test_market() {
        // Buy Market: Stock
        // Buy 15 shares of XYZ at the Market good for the Day.
        let expected = json!({
            "orderType": "MARKET",
            "session": "NORMAL",
            "duration": "DAY",
            "orderStrategyType": "SINGLE",
            "orderLegCollection": [
                {
                    "instruction": "BUY",
                    "quantity": 15,
                    "instrument": {
                        "symbol": "XYZ",
                        "assetType": "EQUITY"
                    }
                }
            ]
        });

        let symbol = InstrumentRequest::Equity {
            symbol: "XYZ".to_string(),
        };
        let order_req = OrderRequest::market(symbol, Instruction::Buy, 15.0).unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }

    #[test]
    fn test_limit() {
        // Buy Limit: Single Option
        // Buy to open 10 contracts of the XYZ March 15, 2024 $50 CALL at a Limit of $6.45 good for the Day.
        let expected = json!({
            "complexOrderStrategyType": "NONE",
            "orderType": "LIMIT",
            "session": "NORMAL",
            "price": 6.45,
            "duration": "DAY",
            "orderStrategyType": "SINGLE",
            "orderLegCollection": [
                {
                    "instruction": "BUY_TO_OPEN",
                    "quantity": 10,
                    "instrument": {
                        "symbol": "XYZ   240315C00500000",
                        "assetType": "OPTION"
                    }
                }
            ]
        });

        let symbol = InstrumentRequest::Option {
            symbol: "XYZ   240315C00500000".to_string(),
        };
        let order_req = OrderRequest::limit(symbol, Instruction::BuyToOpen, 10.0, 6.45).unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }

    #[test]
    fn test_vertical_call_spread() {
        // Buy Limit: Vertical Call Spread
        // Buy to open 2 contracts of the XYZ March 15, 2024 $45 Put and Sell to open 2 contract of the XYZ March 15, 2024 $43 Put at a LIMIT price of $0.10 good for the Day.
        let expected = json!({
            "orderType": "NET_DEBIT",
            "session": "NORMAL",
            "price": 0.1,
            "duration": "DAY",
            "orderStrategyType": "SINGLE",
            "orderLegCollection": [
                {
                    "instruction": "BUY_TO_OPEN",
                    "quantity": 2,
                    "instrument": {
                        "symbol": "XYZ   240315P00045000",
                        "assetType": "OPTION"
                    }
                },
                {
                    "instruction": "SELL_TO_OPEN",
                    "quantity": 2,
                    "instrument": {
                        "symbol": "XYZ   240315P00043000",
                        "assetType": "OPTION"
                    }
                }
            ]
        });

        let symbol1 = InstrumentRequest::Option {
            symbol: "XYZ   240315P00045000".to_string(),
        };
        let symbol2 = InstrumentRequest::Option {
            symbol: "XYZ   240315P00043000".to_string(),
        };
        let order_req = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::NetDebit)
            .session(Session::Normal)
            .duration(Duration::Day)
            .price(0.1)
            .order_leg_collection(vec![
                OrderLegCollectionRequest {
                    instruction: Instruction::BuyToOpen,
                    quantity: 2.0,
                    instrument: symbol1,
                },
                OrderLegCollectionRequest {
                    instruction: Instruction::SellToOpen,
                    quantity: 2.0,
                    instrument: symbol2,
                },
            ])
            .build()
            .unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }

    #[test]
    fn test_one_triggers_another() {
        // Conditional Order: One Triggers Another
        // Buy 10 shares of XYZ at a Limit price of $34.97 good for the Day. If filled, immediately submit an order to Sell 10 shares of XYZ with a Limit price of $42.03 good for the Day. Also known as 1st Trigger Sequence.
        let expected = json!({
            "orderType": "LIMIT",
            "session": "NORMAL",
            "price": 34.97,
            "duration": "DAY",
            "orderStrategyType": "TRIGGER",
            "orderLegCollection": [
                {
                    "instruction": "BUY",
                    "quantity": 10,
                    "instrument": {
                        "symbol": "XYZ",
                        "assetType": "EQUITY"
                    }
                }
            ],
            "childOrderStrategies": [
                {
                    "orderType": "LIMIT",
                    "session": "NORMAL",
                    "price": 42.03,
                    "duration": "DAY",
                    "orderStrategyType": "SINGLE",
                    "orderLegCollection": [
                        {
                            "instruction": "SELL",
                            "quantity": 10,
                            "instrument": {
                                "symbol": "XYZ",
                                "assetType": "EQUITY"
                            }
                        }
                    ]
                }
            ]
        });

        let symbol = InstrumentRequest::Equity {
            symbol: "XYZ".to_string(),
        };

        let child_order_req = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::Limit)
            .session(Session::Normal)
            .duration(Duration::Day)
            .price(42.03)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Sell,
                quantity: 10.0,
                instrument: symbol.clone(),
            }])
            .build()
            .unwrap();
        let order_req = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::Limit)
            .session(Session::Normal)
            .duration(Duration::Day)
            .price(34.97)
            .order_strategy_type(OrderStrategyType::Trigger)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Buy,
                quantity: 10.0,
                instrument: symbol,
            }])
            .child_order_strategies(vec![child_order_req])
            .build()
            .unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }

    #[test]
    fn test_one_cancels_another() {
        // Conditional Order: One Cancels Another
        // Sell 2 shares of XYZ at a Limit price of $45.97 and Sell 2 shares of XYZ with a Stop Limit order where the stop price is $37.03 and limit is $37.00. Both orders are sent at the same time. If one order fills, the other order is immediately cancelled. Both orders are good for the Day. Also known as an OCO order.
        let expected = json!({
            "orderStrategyType": "OCO",
            "childOrderStrategies": [
                {
                    "orderType": "LIMIT",
                    "session": "NORMAL",
                    "price": 45.97,
                    "duration": "DAY",
                    "orderStrategyType": "SINGLE",
                    "orderLegCollection": [
                        {
                            "instruction": "SELL",
                            "quantity": 2,
                            "instrument": {
                                "symbol": "XYZ",
                                "assetType": "EQUITY"
                            }
                        }
                    ]
                },
                {
                    "orderType": "STOP_LIMIT",
                    "session": "NORMAL",
                    "price": 37.0,
                    "stopPrice": 37.03,
                    "duration": "DAY",
                    "orderStrategyType": "SINGLE",
                    "orderLegCollection": [
                        {
                            "instruction": "SELL",
                            "quantity": 2,
                            "instrument": {
                                "symbol": "XYZ",
                                "assetType": "EQUITY"
                            }
                        }
                    ]
                }
            ]
        });

        let symbol = InstrumentRequest::Equity {
            symbol: "XYZ".to_string(),
        };

        let child_order_req1 = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::Limit)
            .session(Session::Normal)
            .duration(Duration::Day)
            .price(45.97)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Sell,
                quantity: 2.0,
                instrument: symbol.clone(),
            }])
            .build()
            .unwrap();
        let child_order_req2 = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::StopLimit)
            .session(Session::Normal)
            .duration(Duration::Day)
            .price(37.00)
            .stop_price(37.03)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Sell,
                quantity: 2.0,
                instrument: symbol.clone(),
            }])
            .build()
            .unwrap();
        let order_req = OrderRequestBuilder::default()
            .order_strategy_type(OrderStrategyType::Oco)
            .child_order_strategies(vec![child_order_req1, child_order_req2])
            .build()
            .unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn test_one_triggers_a_one_cancels_another() {
        // Conditional Order: One Triggers A One Cancels Another
        // Buy 5 shares of XYZ at a Limit price of $14.97 good for the Day. Once filled, 2 sell orders are immediately sent: Sell 5 shares of XYZ at a Limit price of $15.27 and Sell 5 shares of XYZ with a Stop order where the stop price is $11.27. If one of the sell orders fill, the other order is immediately cancelled. Both Sell orders are Good till Cancel. Also known as a 1st Trigger OCO order.
        let expected = json!({
            "orderStrategyType": "TRIGGER",
            "session": "NORMAL",
            "duration": "DAY",
            "orderType": "LIMIT",
            "price": 14.97,
            "orderLegCollection": [
                {
                    "instruction": "BUY",
                    "quantity": 5,
                    "instrument": {
                        "assetType": "EQUITY",
                        "symbol": "XYZ"
                    }
                }
            ],
            "childOrderStrategies": [
                {
                    "orderStrategyType": "OCO",
                    "childOrderStrategies": [
                        {
                            "orderStrategyType": "SINGLE",
                            "session": "NORMAL",
                            "duration": "GOOD_TILL_CANCEL",
                            "orderType": "LIMIT",
                            "price": 15.27,
                            "orderLegCollection": [
                                {
                                    "instruction": "SELL",
                                    "quantity": 5,
                                    "instrument": {
                                        "assetType": "EQUITY",
                                        "symbol": "XYZ"
                                    }
                                }
                            ]
                        },
                        {
                            "orderStrategyType": "SINGLE",
                            "session": "NORMAL",
                            "duration": "GOOD_TILL_CANCEL",
                            "orderType": "STOP",
                            "stopPrice": 11.27,
                            "orderLegCollection": [
                                {
                                    "instruction": "SELL",
                                    "quantity": 5,
                                    "instrument": {
                                        "assetType": "EQUITY",
                                        "symbol": "XYZ"
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        });

        let symbol = InstrumentRequest::Equity {
            symbol: "XYZ".to_string(),
        };

        let child_child_order_req1 = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::Limit)
            .session(Session::Normal)
            .duration(Duration::GoodTillCancel)
            .price(15.27)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Sell,
                quantity: 5.0,
                instrument: symbol.clone(),
            }])
            .build()
            .unwrap();
        let child_child_order_req2 = OrderRequestBuilder::default()
            .order_type(OrderTypeRequest::Stop)
            .session(Session::Normal)
            .duration(Duration::GoodTillCancel)
            .stop_price(11.27)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Sell,
                quantity: 5.0,
                instrument: symbol.clone(),
            }])
            .build()
            .unwrap();
        let child_order_req = OrderRequestBuilder::default()
            .order_strategy_type(OrderStrategyType::Oco)
            .child_order_strategies(vec![child_child_order_req1, child_child_order_req2])
            .build()
            .unwrap();
        let order_req = OrderRequestBuilder::default()
            .order_strategy_type(OrderStrategyType::Trigger)
            .session(Session::Normal)
            .duration(Duration::Day)
            .order_type(OrderTypeRequest::Limit)
            .price(14.97)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Buy,
                quantity: 5.0,
                instrument: symbol.clone(),
            }])
            .child_order_strategies(vec![child_order_req])
            .build()
            .unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }

    #[test]
    fn test_sell_trailing_stop() {
        // Sell Trailing Stop: Stock
        // Sell 10 shares of XYZ with a Trailing Stop where the trail is a -$10 offset from the time the order is submitted. As the stock price goes up, the -$10 trailing offset will follow. If stock XYZ goes from $110 to $130, your trail will automatically be adjusted to $120. If XYZ falls to $120 or below, a Market order is submitted. This order is good for the Day.
        let expected = json!({
            "complexOrderStrategyType": "NONE",
            "orderType": "TRAILING_STOP",
            "session": "NORMAL",
            "stopPriceLinkBasis": "BID",
            "stopPriceLinkType": "VALUE",
            "stopPriceOffset": 10,
            "duration": "DAY",
            "orderStrategyType": "SINGLE",
            "orderLegCollection": [
                {
                    "instruction": "SELL",
                    "quantity": 10,
                    "instrument": {
                        "symbol": "XYZ",
                        "assetType": "EQUITY"
                    }
                }
            ]
        });

        let symbol = InstrumentRequest::Equity {
            symbol: "XYZ".to_string(),
        };

        let order_req = OrderRequestBuilder::default()
            .complex_order_strategy_type(ComplexOrderStrategyType::None)
            .order_type(OrderTypeRequest::TrailingStop)
            .session(Session::Normal)
            .duration(Duration::Day)
            .stop_price_link_basis(StopPriceLinkBasis::Bid)
            .stop_price_link_type(StopPriceLinkType::Value)
            .stop_price_offset(10.0)
            .price(14.97)
            .order_leg_collection(vec![OrderLegCollectionRequest {
                instruction: Instruction::Sell,
                quantity: 10.0,
                instrument: symbol.clone(),
            }])
            .build()
            .unwrap();
        let order_req = serde_json::to_value(order_req).unwrap();
        assert_json_matches!(
            order_req,
            expected,
            Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat)
        );
    }
}