alpacars 0.1.2

Async Rust SDK for Alpaca Markets 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
use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Deserializes the `"c"` conditions field, which stocks send as `["A","B"]`
/// but options send as a bare string `"A"` or `" "`.
fn deserialize_conditions<'de, D>(d: D) -> Result<Option<Vec<String>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let v: Option<serde_json::Value> = Option::deserialize(d)?;
    Ok(match v {
        None | Some(serde_json::Value::Null) => None,
        Some(serde_json::Value::String(s)) => Some(vec![s]),
        Some(serde_json::Value::Array(arr)) => Some(
            arr.into_iter()
                .filter_map(|v| v.as_str().map(str::to_owned))
                .collect(),
        ),
        Some(other) => Some(vec![other.to_string()]),
    })
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bar {
    /// Timestamp
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    /// Open price
    #[serde(rename = "o")]
    pub open: f64,
    /// High price
    #[serde(rename = "h")]
    pub high: f64,
    /// Low price
    #[serde(rename = "l")]
    pub low: f64,
    /// Close price
    #[serde(rename = "c")]
    pub close: f64,
    /// Volume
    #[serde(rename = "v")]
    pub volume: f64,
    /// Volume-weighted average price
    #[serde(rename = "vw")]
    pub vwap: Option<f64>,
    /// Number of trades
    #[serde(rename = "n")]
    pub trade_count: Option<f64>,
    /// Exchange
    #[serde(rename = "x")]
    pub exchange: Option<String>,
}

/// Map of symbol → list of bars (auto-paginated)
pub type BarSet = HashMap<String, Vec<Bar>>;
/// Map of symbol → latest bar
pub type LatestBarSet = HashMap<String, Bar>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Quote {
    /// Timestamp
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    /// Ask exchange
    #[serde(rename = "ax")]
    pub ask_exchange: Option<String>,
    /// Ask price
    #[serde(rename = "ap")]
    pub ask_price: f64,
    /// Ask size
    #[serde(rename = "as")]
    pub ask_size: f64,
    /// Bid exchange
    #[serde(rename = "bx")]
    pub bid_exchange: Option<String>,
    /// Bid price
    #[serde(rename = "bp")]
    pub bid_price: f64,
    /// Bid size
    #[serde(rename = "bs")]
    pub bid_size: f64,
    /// Conditions — stocks send an array, options send a bare string; accept both.
    #[serde(rename = "c", default, deserialize_with = "deserialize_conditions")]
    pub conditions: Option<Vec<String>>,
    /// Tape
    #[serde(rename = "z")]
    pub tape: Option<String>,
}

/// Map of symbol → list of quotes
pub type QuoteSet = HashMap<String, Vec<Quote>>;
/// Map of symbol → latest quote
pub type LatestQuoteSet = HashMap<String, Quote>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trade {
    /// Timestamp
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    /// Exchange
    #[serde(rename = "x")]
    pub exchange: Option<String>,
    /// Price
    #[serde(rename = "p")]
    pub price: f64,
    /// Size
    #[serde(rename = "s")]
    pub size: f64,
    /// Trade ID
    #[serde(rename = "i")]
    pub trade_id: Option<serde_json::Value>,
    /// Conditions — stocks send an array, options send a bare string; accept both.
    #[serde(rename = "c", default, deserialize_with = "deserialize_conditions")]
    pub conditions: Option<Vec<String>>,
    /// Tape
    #[serde(rename = "z")]
    pub tape: Option<String>,
    /// Update (for corrections/cancels)
    #[serde(rename = "u")]
    pub update: Option<String>,
}

/// Map of symbol → list of trades
pub type TradeSet = HashMap<String, Vec<Trade>>;
/// Map of symbol → latest trade
pub type LatestTradeSet = HashMap<String, Trade>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderbookEntry {
    #[serde(rename = "p")]
    pub price: f64,
    #[serde(rename = "s")]
    pub size: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Orderbook {
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    #[serde(rename = "a")]
    pub asks: Vec<OrderbookEntry>,
    #[serde(rename = "b")]
    pub bids: Vec<OrderbookEntry>,
}

/// Map of symbol → latest orderbook
pub type LatestOrderbookSet = HashMap<String, Orderbook>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Snapshot {
    #[serde(rename = "latestTrade")]
    pub latest_trade: Option<Trade>,
    #[serde(rename = "latestQuote")]
    pub latest_quote: Option<Quote>,
    #[serde(rename = "minuteBar")]
    pub minute_bar: Option<Bar>,
    #[serde(rename = "dailyBar")]
    pub daily_bar: Option<Bar>,
    #[serde(rename = "prevDailyBar")]
    pub prev_daily_bar: Option<Bar>,
}

/// Map of symbol → snapshot
pub type SnapshotSet = HashMap<String, Snapshot>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptionsSnapshot {
    pub symbol: Option<String>,
    #[serde(rename = "latestTrade")]
    pub latest_trade: Option<Trade>,
    #[serde(rename = "latestQuote")]
    pub latest_quote: Option<Quote>,
    #[serde(rename = "minuteBar")]
    pub minute_bar: Option<Bar>,
    #[serde(rename = "dailyBar")]
    pub daily_bar: Option<Bar>,
    #[serde(rename = "prevDailyBar")]
    pub prev_daily_bar: Option<Bar>,
    pub greeks: Option<OptionsGreeks>,
    #[serde(rename = "impliedVolatility")]
    pub implied_volatility: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptionsGreeks {
    pub delta: Option<f64>,
    pub gamma: Option<f64>,
    pub rho: Option<f64>,
    pub theta: Option<f64>,
    pub vega: Option<f64>,
}

/// Map of symbol → options snapshot
pub type OptionsSnapshotSet = HashMap<String, OptionsSnapshot>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradingStatus {
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    #[serde(rename = "S")]
    pub symbol: String,
    #[serde(rename = "sc")]
    pub status_code: Option<String>,
    #[serde(rename = "sm")]
    pub status_message: Option<String>,
    #[serde(rename = "rc")]
    pub reason_code: Option<String>,
    #[serde(rename = "rm")]
    pub reason_message: Option<String>,
    #[serde(rename = "z")]
    pub tape: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradeCancel {
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    #[serde(rename = "x")]
    pub exchange: Option<String>,
    #[serde(rename = "p")]
    pub price: f64,
    #[serde(rename = "s")]
    pub size: f64,
    #[serde(rename = "i")]
    pub trade_id: Option<serde_json::Value>,
    #[serde(rename = "z")]
    pub tape: Option<String>,
    #[serde(rename = "S")]
    pub symbol: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradeCorrection {
    #[serde(rename = "t")]
    pub timestamp: DateTime<Utc>,
    #[serde(rename = "x")]
    pub exchange: Option<String>,
    #[serde(rename = "oi")]
    pub original_trade_id: Option<serde_json::Value>,
    #[serde(rename = "op")]
    pub original_price: Option<f64>,
    #[serde(rename = "os")]
    pub original_size: Option<f64>,
    #[serde(rename = "oc")]
    pub original_conditions: Option<Vec<String>>,
    #[serde(rename = "ci")]
    pub corrected_trade_id: Option<serde_json::Value>,
    #[serde(rename = "cp")]
    pub corrected_price: Option<f64>,
    #[serde(rename = "cs")]
    pub corrected_size: Option<f64>,
    #[serde(rename = "cc")]
    pub corrected_conditions: Option<Vec<String>>,
    #[serde(rename = "z")]
    pub tape: Option<String>,
    #[serde(rename = "S")]
    pub symbol: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NewsImage {
    pub size: String,
    pub url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct News {
    pub id: u64,
    pub headline: String,
    pub author: Option<String>,
    pub created_at: Option<DateTime<Utc>>,
    pub updated_at: Option<DateTime<Utc>>,
    pub summary: Option<String>,
    pub content: Option<String>,
    pub url: Option<String>,
    pub images: Option<Vec<NewsImage>>,
    pub symbols: Option<Vec<String>>,
    pub source: Option<String>,
}

/// Collection of news articles
pub type NewsSet = Vec<News>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActiveStock {
    pub symbol: String,
    pub volume: Option<f64>,
    pub trade_count: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MostActives {
    pub most_actives: Vec<ActiveStock>,
    pub last_updated: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Mover {
    pub symbol: String,
    pub percent_change: Option<f64>,
    pub change: Option<f64>,
    pub price: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Movers {
    pub gainers: Vec<Mover>,
    pub losers: Vec<Mover>,
    pub market_type: Option<String>,
    pub last_updated: Option<DateTime<Utc>>,
}

// Corporate action types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForwardSplit {
    pub symbol: String,
    pub new_rate: f64,
    pub old_rate: f64,
    pub process_date: Option<NaiveDate>,
    pub ex_date: Option<NaiveDate>,
    pub record_date: Option<NaiveDate>,
    pub payable_date: Option<NaiveDate>,
    pub due_bill_redemption_date: Option<NaiveDate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReverseSplit {
    pub symbol: String,
    pub new_rate: f64,
    pub old_rate: f64,
    pub process_date: Option<NaiveDate>,
    pub ex_date: Option<NaiveDate>,
    pub record_date: Option<NaiveDate>,
    pub payable_date: Option<NaiveDate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnitSplit {
    pub old_symbol: String,
    pub old_rate: f64,
    pub new_symbol: String,
    pub new_rate: f64,
    pub alternate_symbol: Option<String>,
    pub alternate_rate: Option<f64>,
    pub process_date: Option<NaiveDate>,
    pub effective_date: Option<NaiveDate>,
    pub payable_date: Option<NaiveDate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CashDividend {
    pub symbol: String,
    pub rate: f64,
    pub special: bool,
    pub foreign: bool,
    pub process_date: Option<NaiveDate>,
    pub ex_date: Option<NaiveDate>,
    pub record_date: Option<NaiveDate>,
    pub payable_date: Option<NaiveDate>,
    pub due_bill_on_date: Option<NaiveDate>,
    pub due_bill_off_date: Option<NaiveDate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StockDividend {
    pub symbol: String,
    pub rate: f64,
    pub process_date: Option<NaiveDate>,
    pub ex_date: Option<NaiveDate>,
    pub record_date: Option<NaiveDate>,
    pub payable_date: Option<NaiveDate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpinOff {
    pub source_symbol: String,
    pub source_rate: f64,
    pub new_symbol: String,
    pub new_rate: f64,
    pub process_date: Option<NaiveDate>,
    pub ex_date: Option<NaiveDate>,
    pub record_date: Option<NaiveDate>,
    pub payable_date: Option<NaiveDate>,
    pub due_bill_redemption_date: Option<NaiveDate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorporateActionsSet {
    pub reverse_splits: Option<Vec<ReverseSplit>>,
    pub forward_splits: Option<Vec<ForwardSplit>>,
    pub unit_splits: Option<Vec<UnitSplit>>,
    pub cash_dividends: Option<Vec<CashDividend>>,
    pub stock_dividends: Option<Vec<StockDividend>>,
    pub spin_offs: Option<Vec<SpinOff>>,
    pub cash_mergers: Option<Vec<serde_json::Value>>,
    pub stock_mergers: Option<Vec<serde_json::Value>>,
    pub stock_and_cash_mergers: Option<Vec<serde_json::Value>>,
    pub redemptions: Option<Vec<serde_json::Value>>,
    pub name_changes: Option<Vec<serde_json::Value>>,
    pub worthless_removals: Option<Vec<serde_json::Value>>,
    pub rights_distributions: Option<Vec<serde_json::Value>>,
    pub next_page_token: Option<String>,
}