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
use serde::Deserialize;
use serde::Serialize;

use crate::model::trader::accounts::AccountsInstrument;

use super::preview_order::Instruction;

#[allow(clippy::struct_field_names)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
    pub session: Session,
    pub duration: Duration,
    pub order_type: OrderType,
    pub cancel_time: Option<chrono::DateTime<chrono::Utc>>,
    pub complex_order_strategy_type: ComplexOrderStrategyType,
    pub quantity: f64,
    pub filled_quantity: f64,
    pub remaining_quantity: f64,
    pub requested_destination: RequestedDestination,
    pub destination_link_name: 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: f64,
    pub tax_lot_method: Option<TaxLotMethod>,
    /// xml: `OrderedMap` { "name": "orderLegCollection", "wrapped": true }
    pub order_leg_collection: Vec<OrderLegCollection>,
    pub activation_price: Option<f64>,
    pub special_instruction: Option<SpecialInstruction>,
    pub order_strategy_type: OrderStrategyType,
    pub order_id: i64,
    /// default: false
    pub cancelable: bool,
    /// default: false
    pub editable: bool,
    pub status: Status,
    pub entered_time: chrono::DateTime<chrono::Utc>,
    pub close_time: Option<chrono::DateTime<chrono::Utc>>,
    pub tag: Option<String>,
    pub account_number: 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<Order>>,
    pub status_description: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderLegCollection {
    pub order_leg_type: OrderLegType,
    pub leg_id: i64,
    pub instrument: AccountsInstrument,
    pub instruction: Instruction,
    pub position_effect: PositionEffect,
    pub quantity: f64,
    pub quantity_type: Option<QuantityType>,
    pub div_cap_gains: Option<DivCapGains>,
    pub to_symbol: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderActivity {
    pub activity_type: ActivityType,
    pub execution_type: ExecutionType,
    pub quantity: f64,
    pub order_remaining_quantity: f64,
    /// xml: `OrderedMap` { "name": "executionLegs", "wrapped": true }
    pub execution_legs: Vec<ExecutionLeg>,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecutionLeg {
    pub leg_id: i64,
    pub price: f64,
    pub quantity: f64,
    pub mismarked_quantity: f64,
    pub instrument_id: i64,
    pub time: chrono::DateTime<chrono::Utc>,
}

/// The market session during which the order trade should be executed.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Session {
    /// Normal market hours, from 9:30am to 4:00pm Eastern.
    #[default]
    Normal,
    /// Premarket session, from 8:00am to 9:30am Eastern.
    Am,
    /// After-market session, from 4:00pm to 8:00pm Eastern.
    Pm,
    /// Orders are active during all trading sessions except the overnight
    /// session. This is the union of ``NORMAL``, ``AM``, and ``PM``.
    Seamless,
}

/// Length of time over which the trade will be active.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Duration {
    /// Cancel the trade at the end of the trading day. Note if the order cannot
    /// be filled all at once, you may see partial executions throughout the day.
    #[default]
    Day,
    /// Keep the trade open for six months, or until the end of the cancel date,
    /// whichever is shorter. Note if the order cannot be filled all at once, you
    /// may see partial executions over the lifetime of the order.
    GoodTillCancel,
    /// Either execute the order immediately at the specified price, or cancel it
    /// immediately.
    FillOrKill,
    ImmediateOrCancel,
    EndOfWeek,
    EndOfMonth,
    NextEndOfMonth,
    Unknown,
}

/// Type of order to place.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderType {
    #[default]
    /// Execute the order immediately at the best-available price.
    /// `More Info <https://www.investopedia.com/terms/m/marketorder.asp>`__.
    Market,
    /// 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,
    Unknown,
}

/// Explicit order strategies for executing multi-leg options orders.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ComplexOrderStrategyType {
    #[default]
    /// No complex order strategy. This is the default.
    None,
    /// `Covered call <https://tickertape.tdameritrade.com/trading/selling-covered-call-options-strategy-income-hedging-15135>`__
    Covered,
    /// `Vertical spread <https://tickertape.tdameritrade.com/trading/vertical-credit-spreads-high-probability-15846>`__
    Vertical,
    /// `Ratio backspread <https://tickertape.tdameritrade.com/trading/pricey-stocks-ratio-spreads-15306>`__
    BackRatio,
    /// `Calendar spread <https://tickertape.tdameritrade.com/trading/calendar-spreads-trading-primer-15095>`__
    Calendar,
    /// `Diagonal spread <https://tickertape.tdameritrade.com/trading/love-your-diagonal-spread-15030>`__
    Diagonal,
    /// `Straddle spread <https://tickertape.tdameritrade.com/trading/straddle-strangle-option-volatility-16208>`__
    Straddle,
    /// `Strandle spread <https://tickertape.tdameritrade.com/trading/straddle-strangle-option-volatility-16208>`__
    Strangle,
    CollarSynthetic,
    /// `Butterfly spread <https://tickertape.tdameritrade.com/trading/butterfly-spread-options-15976>`__
    Butterfly,
    /// `Condor spread <https://www.investopedia.com/terms/c/condorspread.asp>`__
    Condor,
    /// `Iron condor spread <https://tickertape.tdameritrade.com/trading/iron-condor-options-spread-your-trading-wings-15948>`__
    IronCondor,
    /// `Roll a vertical spread <https://tickertape.tdameritrade.com/trading/exit-winning-losing-trades-16685>`__
    VerticalRoll,
    /// `Collar strategy <https://tickertape.tdameritrade.com/trading/stock-hedge-options-collars-15529>`__
    CollarWithStock,
    /// `Double diagonal spread <https://optionstradingiq.com/the-ultimate-guide-to-double-diagonal-spreads/>`__
    DoubleDiagonal,
    /// `Unbalanced butterfy spread  <https://tickertape.tdameritrade.com/trading/unbalanced-butterfly-strong-directional-bias-15913>`__
    UnbalancedButterfly,
    UnbalancedCondor,
    UnbalancedIronCondor,
    UnbalancedVerticalRoll,
    /// Mutual fund swap
    MutualFundSwap,
    /// A custom multi-leg order strategy.
    Custom,
}

/// Destinations for when you want to request a specific destination for your order.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RequestedDestination {
    #[default]
    Inet,
    EcnArca,
    Cboe,
    Amex,
    Phlx,
    Ise,
    Box,
    Nyse,
    Nasdaq,
    Bats,
    C2,
    Auto,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum StopPriceLinkBasis {
    #[default]
    Manual,
    Base,
    Trigger,
    Last,
    Bid,
    Ask,
    AskBid,
    Mark,
    Average,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum StopPriceLinkType {
    #[default]
    Value,
    Percent,
    Tick,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum StopType {
    #[default]
    Standard,
    Bid,
    Ask,
    Last,
    Mark,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PriceLinkBasis {
    #[default]
    Manual,
    Base,
    Trigger,
    Last,
    Bid,
    Ask,
    AskBid,
    Mark,
    Average,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PriceLinkType {
    #[default]
    Value,
    Percent,
    Tick,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum TaxLotMethod {
    #[default]
    Fifo,
    Lifo,
    HighCost,
    LowCost,
    AverageCost,
    SpecificLot,
    LossHarvester,
}

/// Special instruction for trades.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SpecialInstruction {
    #[default]
    /// Disallow partial order execution.
    /// `More info <https://www.investopedia.com/terms/a/aon.asp>`__.
    AllOrNone,
    /// Do not reduce order size in response to cash dividends.
    /// `More info <https://www.investopedia.com/terms/d/dnr.asp>`__.
    DoNotReduce,
    /// Combination of ``ALL_OR_NONE`` and ``DO_NOT_REDUCE``.
    AllOrNoneDoNotReduce,
}

/// Rules for composite orders.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderStrategyType {
    /// No chaining, only a single order is submitted
    #[default]
    Single,
    Cancel,
    Recall,
    Pair,
    Flatten,
    TwoDaySwap,
    BlastAll,
    /// Execution of one order cancels the other
    Oco,
    /// Execution of one order triggers placement of the other
    Trigger,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Status {
    #[default]
    AwaitingParentOrder,
    AwaitingCondition,
    AwaitingStopCondition,
    AwaitingManualReview,
    Accepted,
    AwaitingUrOut,
    PendingActivation,
    Queued,
    Working,
    Rejected,
    PendingCancel,
    Canceled,
    PendingReplace,
    Replaced,
    Filled,
    Expired,
    New,
    AwaitingReleaseTime,
    PendingAcknowledgement,
    PendingRecall,
    Unknown,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ActivityType {
    #[default]
    Execution,
    OrderAction,
}

/// Order Status
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ExecutionType {
    #[default]
    /// Your order is waiting to be completed.
    Open,
    /// Your order cannot be completed until a certain condition is met.
    OpenContingent,
    /// Your order cancellation request has been submitted.
    CancelPending,
    /// Part of the order has been filled, part is still open.
    OpenPartialFill,
    /// Part of your order was filled. The remaining part was canceled.
    ClosedPartialFill,
    /// Your order was completed in full.
    Fill,
    /// Your order was canceled at your request.
    Canceled,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderLegType {
    #[default]
    Equity,
    Option,
    Index,
    MutualFund,
    CashEquivalent,
    FixedIncome,
    Currency,
    CollectiveInvestment,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PositionEffect {
    #[default]
    Opening,
    Closing,
    Automatic,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum QuantityType {
    #[default]
    AllShares,
    Dollars,
    Shares,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum DivCapGains {
    #[default]
    Reinvest,
    Payout,
}

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

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

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

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

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

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

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

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

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