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
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use serde_with::{TimestampMilliSeconds, serde_as};

/// Quote info of Equity security
#[serde_with::apply(
    Option => #[serde(skip_serializing_if = "Option::is_none")],
)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EquityResponse {
    /// nullable: true
    /// Asset Sub Type (only there if applicable)
    pub asset_sub_type: Option<EquityAssetSubType>,

    /// example: 1234567890
    ///
    /// SSID of instrument
    pub ssid: i64,

    /// example: AAPL
    ///
    /// Symbol of instrument
    pub symbol: String,

    /// example: true
    ///
    /// is quote realtime
    pub realtime: bool,

    /// nullable: true
    ///
    /// NBBO - realtime, NFL - Non-fee liable quote.
    pub quote_type: QuoteType,
    pub extended: Option<ExtendedMarket>,
    pub fundamental: Option<Fundamental>,
    pub quote: QuoteEquity,
    pub reference: ReferenceEquity,
    pub regular: Option<RegularMarket>,
}

/// Quote data for extended hours
#[serde_as]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExtendedMarket {
    /// example: 124.85
    ///
    /// Extended market ask price
    pub ask_price: f64,

    /// example: 51771
    ///
    /// Extended market ask size
    pub ask_size: i64,

    /// example: 124.85
    ///
    /// Extended market bid price
    pub bid_price: f64,

    /// example: 51771
    ///
    /// Extended market bid size
    pub bid_size: i64,

    /// example: 124.85
    ///
    /// Extended market last price
    pub last_price: f64,

    /// example: 51771
    ///
    /// Regular market last size
    pub last_size: i64,

    /// example: 1.1246
    ///
    /// mark price
    pub mark: f64,

    /// example: 1621368000400
    ///
    /// Extended market quote time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    pub quote_time: chrono::DateTime<chrono::Utc>,

    /// example: 12345
    ///
    /// Total volume
    pub total_volume: u64,

    /// example: 1621368000400
    ///
    /// Extended market trade time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    pub trade_time: chrono::DateTime<chrono::Utc>,
}

/// Fundamentals of a security
#[serde_with::apply(
Option => #[serde(skip_serializing_if = "Option::is_none")],
)]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Fundamental {
    /// Average 10 day volume
    pub avg_10_days_volume: f64,

    /// Average 1 day volume
    pub avg_1_year_volume: f64,

    /// example: 2021-04-28T00:00:00Z
    ///
    /// pattern: yyyy-MM-dd'T'HH:mm:ssZ
    ///
    /// Declaration date in yyyy-mm-ddThh:mm:ssZ
    pub declaration_date: Option<chrono::DateTime<chrono::Utc>>,

    /// example: 0.88
    ///
    /// Dividend Amount
    pub div_amount: f64,

    /// example: 2021-05-07T00:00:00Z
    ///
    /// Dividend date in yyyy-mm-ddThh:mm:ssZ
    pub div_ex_date: Option<chrono::DateTime<chrono::Utc>>,

    /// nullable: true
    ///
    /// Dividend frequency 1 – once a year or annually 2 – 2x a year or semi-annualy 3 - 3x a year (ex. ARCO, EBRPF) 4 – 4x a year or quarterly 6 - 6x per yr or every other month 11 – 11x a year (ex. FBND, FCOR) 12 – 12x a year or monthly
    pub div_freq: DivFrequency,

    /// example: 0.22
    ///
    /// Dividend Pay Amount
    pub div_pay_amount: f64,

    /// example: 2021-05-13T00:00:00Z
    ///
    /// pattern: yyyy-MM-dd'T'HH:mm:ssZ
    ///
    /// Dividend pay date in yyyy-mm-ddThh:mm:ssZ
    pub div_pay_date: Option<chrono::DateTime<chrono::Utc>>,

    /// example: 0.7
    ///
    /// Dividend yield
    pub div_yield: f64,

    /// example: 4.45645
    ///
    /// Earnings per Share
    pub eps: f64,

    /// example: -1
    ///
    /// Fund Leverage Factor + > 0 <-
    pub fund_leverage_factor: f64,

    /// nullable: true
    ///
    /// `FundStrategy` "A" - Active "L" - Leveraged "P" - Passive "Q" - Quantitative "S" - Short
    pub fund_strategy: Option<FundStrategy>,

    /// example: 2021-02-12T00:00:00Z
    ///
    /// pattern: yyyy-MM-dd'T'HH:mm:ssZ
    ///
    /// Next Dividend date
    pub next_div_ex_date: Option<chrono::DateTime<chrono::Utc>>,

    /// example: 2021-02-12T00:00:00Z
    ///
    /// pattern: yyyy-MM-dd'T'HH:mm:ssZ
    ///
    /// Next Dividend pay date
    pub next_div_pay_date: Option<chrono::DateTime<chrono::Utc>>,

    /// example: 28.599
    ///
    /// P/E Ratio
    pub pe_ratio: f64,

    // not in schema
    pub last_earnings_date: Option<chrono::DateTime<chrono::Utc>>,
}

/// Quote data of Equity security
#[serde_as]
#[serde_with::apply(
    Option => #[serde(skip_serializing_if = "Option::is_none")],
)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QuoteEquity {
    /// example: 145.09
    ///
    /// Higest price traded in the past 12 months, or 52 weeks
    #[serde(rename = "52WeekHigh")]
    pub n52week_high: f64,

    /// example: 77.581
    ///
    /// Lowest price traded in the past 12 months, or 52 weeks
    #[serde(rename = "52WeekLow")]
    pub n52week_low: f64,

    /// example: XNYS
    ///
    /// ask MIC code
    #[serde(rename = "askMICId")]
    pub ask_micid: Option<String>,

    /// example: 124.63
    ///
    /// Current Best Ask Price
    pub ask_price: f64,

    /// example: 700
    ///
    /// Number of shares for ask
    pub ask_size: i64,

    /// example: 1621376892336
    ///
    /// Last ask time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    pub ask_time: chrono::DateTime<chrono::Utc>,

    /// example: XNYS
    ///
    /// bid MIC code
    #[serde(rename = "bidMICId")]
    pub bid_micid: Option<String>,

    /// example: 124.6
    ///
    /// Current Best Bid Price
    pub bid_price: f64,

    /// example: 300
    ///
    /// Number of shares for bid
    pub bid_size: i64,

    /// example: 1621376892336
    ///
    /// Last bid time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    pub bid_time: chrono::DateTime<chrono::Utc>,

    /// example: 126.27
    ///
    /// Previous day's closing price
    pub close_price: f64,

    /// example: 126.99
    ///
    /// Day's high trade price
    pub high_price: f64,

    /// example: XNYS
    ///
    /// Last MIC Code
    #[serde(rename = "lastMICId")]
    pub last_micid: Option<String>,

    /// example: 122.3
    pub last_price: f64,

    /// example: 100
    ///
    /// Number of shares traded with last trade
    pub last_size: i64,

    /// example: 52.74
    ///
    /// Day's low trade price
    pub low_price: f64,

    /// example: 52.93
    ///
    /// Mark price
    pub mark: f64,

    /// example: -0.01
    ///
    /// Mark Price change
    pub mark_change: Option<f64>,

    /// example: -0.0189
    ///
    /// Mark Price percent change
    pub mark_percent_change: Option<f64>,

    /// example: -0.04
    ///
    /// Current Last-Prev Close
    pub net_change: f64,

    /// example: -0.0756
    ///
    /// Net Percentage Change
    pub net_percent_change: Option<f64>,

    /// example: 52.8
    ///
    /// Price at market open
    pub open_price: f64,

    /// example: 1621376892336
    ///
    /// Last quote time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    pub quote_time: chrono::DateTime<chrono::Utc>,

    /// example: Normal
    ///
    /// Status of security
    pub security_status: String,

    /// example: 20171188
    ///
    /// Aggregated shares traded throughout the day, including pre/post market hours.
    pub total_volume: u64,

    /// example: 1621376731304
    ///
    /// Last trade time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    pub trade_time: chrono::DateTime<chrono::Utc>,

    /// example: 0.0094
    ///
    /// Option Risk/Volatility Measurement
    pub volatility: Option<f64>,

    // not in schema
    pub post_market_change: Option<f64>,
    pub post_market_percent_change: Option<f64>,
}

/// Reference data of Equity security
#[serde_with::apply(
    Option => #[serde(skip_serializing_if = "Option::is_none")],
)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReferenceEquity {
    /// example: A23456789
    ///
    /// CUSIP of Instrument
    pub cusip: String,

    /// example: Apple Inc. - Common Stock
    ///
    /// Description of Instrument
    pub description: String,

    /// example: q
    ///
    /// Exchange Code
    pub exchange: String,

    /// Exchange Name
    pub exchange_name: String,

    /// FSI Desc
    pub fsi_desc: Option<String>,

    /// example: 100
    ///
    /// Hard to borrow quantity.
    pub htb_quantity: Option<i32>,

    /// example: 4.5
    ///
    /// Hard to borrow rate.
    pub htb_rate: Option<f64>,

    /// example: false
    ///
    /// is Hard to borrow security.
    pub is_hard_to_borrow: Option<bool>,

    /// example: false
    ///
    /// is shortable security.
    pub is_shortable: Option<bool>,

    /// OTC Market Tier
    pub otc_market_tier: Option<String>,
}

/// Market info of security
#[serde_as]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegularMarket {
    /// example: 124.85
    ///
    /// Regular market last price
    #[serde(rename = "regularMarketLastPrice")]
    pub last_price: f64,

    /// example: 51771
    ///
    /// Regular market last size
    #[serde(rename = "regularMarketLastSize")]
    pub last_size: i64,

    /// example: -1.42
    ///
    /// Regular market net change
    #[serde(rename = "regularMarketNetChange")]
    pub net_change: f64,

    /// example: -1.1246
    ///
    /// Regular market percent change
    #[serde(rename = "regularMarketPercentChange")]
    pub percent_change: Option<f64>,

    /// example: 1621368000400
    ///
    /// Regular market trade time in milliseconds since Epoch
    #[serde_as(as = "TimestampMilliSeconds<i64>")]
    #[serde(rename = "regularMarketTradeTime")]
    pub trade_time: chrono::DateTime<chrono::Utc>,
}

/// Asset Sub Type (only there if applicable)
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum EquityAssetSubType {
    Coe,
    Prf,
    Adr,
    Gdr,
    Cef,
    Etf,
    Etn,
    Uit,
    War,
    Rgt,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum QuoteType {
    /// NBBO - realtime
    Nbbo,
    /// NFL - Non-fee liable quote
    Nfl,
}

/// Dividend frequency
#[derive(Debug, Clone, Copy, PartialEq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum DivFrequency {
    /// null
    Zero = 0,

    /// 1 – once a year or annually
    One = 1,

    /// 2 – 2x a year or semi-annualy
    Two = 2,

    /// 3 - 3x a year (ex. ARCO, EBRPF)
    Three = 3,

    /// 4 – 4x a year or quarterly
    Four = 4,

    /// 6 - 6x per yr or every other month
    Six = 6,

    /// 11 – 11x a year (ex. FBND, FCOR)
    Eleven = 11,

    /// 12 – 12x a year or monthly
    Twelve = 12,
}

/// Fund Strategy
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum FundStrategy {
    ///  "A" - Active
    #[serde(rename = "A")]
    Active,

    /// "L" - Leveraged
    #[serde(rename = "L")]
    Leveraged,

    /// "P" - Passive
    #[serde(rename = "P")]
    Passive,

    /// "Q" - Quantitative
    #[serde(rename = "Q")]
    Quantitative,

    /// "S" - Short
    #[serde(rename = "S")]
    Short,
}

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

    use std::collections::HashMap;

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

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