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
use crate::{
responses::market_data::{StreamMarketDepthAggregatesResp, StreamMarketDepthQuotesResp},
Client, Error,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
/// Market Depth Quotes
pub struct MarketDepthQuotes {
/// Contains bid quotes, ordered from high to low price.
bids: Vec<MarketDepthQuote>,
/// Contains ask quotes, ordered from high to low price.
asks: Vec<MarketDepthQuote>,
}
impl MarketDepthQuotes {
fn new() -> Self {
Self {
bids: Vec::new(),
asks: Vec::new(),
}
}
/// Stream realtime market depth quotes for the given Symbol.
///
/// NOTE: `symbol` must be a string of a valid symbol.
/// E.g: `"NVDA"`.
///
/// NOTE: `levels` must be `None` (defaults to 20 levels) or `Some(i32)`.
/// This is for specifying how many levels of price to stream quotes for.
///
/// # Example
/// ---
/// Stream market depth quotes on S&P 500 ETF Trust `"SPY"` to watch order
/// flow, maybe to use for detecting iceberg orders, or watching for large
/// block trades, etc.
/// ```ignore
/// let depth_levels: i32 = 10;
/// let streamed_quotes = client
/// .stream_market_depth_quotes("SPY", Some(depth_levels), |stream_data| {
/// // The response type is `responses::MarketData::StreamMarketDepthQuotesResp`
/// // which has multiple variants the main one you care about is `Quote`
/// // which will contain market depth quote data sent from the stream.
/// match stream_data {
/// StreamMarketDepthQuotesResp::Quote(quote) => {
/// // Do something with the market depth data.
/// println!("{quote:?}");
/// }
/// StreamMarketDepthQuotesResp::Heartbeat(heartbeat) => {
/// // Response for periodic signals letting you know the connection is
/// // still alive. A heartbeat is sent every 5 seconds of inactivity.
/// println!("{heartbeat:?}");
///
/// // for the sake of this example after we recieve the
/// // tenth heartbeat, we will stop the stream session.
/// if heartbeat.heartbeat > 10 {
/// // Example: stopping a stream connection
/// return Err(Error::StopStream);
/// }
/// }
/// StreamMarketDepthQuotesResp::Status(status) => {
/// // Signal sent on state changes in the stream
/// // (closed, opened, paused, resumed)
/// println!("{status:?}");
/// }
/// StreamMarketDepthQuotesResp::Error(err) => {
/// // Response for when an error was encountered,
/// // with details on the error
/// println!("{err:?}");
/// }
/// }
///
/// Ok(())
/// })
/// .await?;
///
/// // After the stream ends print all the collected market depth quotes
/// println!("{streamed_quotes:?}");
/// ```
pub async fn stream<F, S: Into<String>>(
client: &mut Client,
symbol: S,
levels: Option<i32>,
mut on_chunk: F,
) -> Result<MarketDepthQuotes, Error>
where
F: FnMut(StreamMarketDepthQuotesResp) -> Result<(), Error>,
{
let endpoint = format!(
"marketdata/stream/marketdepth/quotes/{}?maxlevels={}",
symbol.into(),
levels.unwrap_or(20),
);
let mut collected_market_depth_quotes = MarketDepthQuotes::new();
client
.stream(&endpoint, |chunk| {
let parsed_chunk = serde_json::from_value::<StreamMarketDepthQuotesResp>(chunk)?;
on_chunk(parsed_chunk.clone())?;
// Only collect quotes, so when the stream is done
// all the quotes that were streamed can be returned.
if let StreamMarketDepthQuotesResp::Quote(quote) = parsed_chunk {
if let Some(bid) = quote.bids.first() {
collected_market_depth_quotes.bids.push(bid.clone());
}
if let Some(ask) = quote.asks.first() {
collected_market_depth_quotes.asks.push(ask.clone());
}
}
Ok(())
})
.await?;
Ok(collected_market_depth_quotes)
}
}
impl Client {
/// Stream realtime market depth quotes for the given Symbol.
///
/// NOTE: `symbol` must be a string of a valid symbol.
/// E.g: `"NVDA"`.
///
/// NOTE: `levels` must be `None` (defaults to 20 levels) or `Some(i32)`.
/// This is for specifying how many levels of price to stream quotes for.
///
/// # Example
/// ---
/// Stream market depth quotes on S&P 500 ETF Trust `"SPY"` to watch order
/// flow, maybe to use for detecting iceberg orders, or watching for large
/// block trades, etc.
/// ```ignore
/// let depth_levels: i32 = 10;
/// let streamed_quotes = client
/// .stream_market_depth_quotes("SPY", Some(depth_levels), |stream_data| {
/// // The response type is `responses::MarketData::StreamMarketDepthQuotesResp`
/// // which has multiple variants the main one you care about is `Quote`
/// // which will contain market depth quote data sent from the stream.
/// match stream_data {
/// StreamMarketDepthQuotesResp::Quote(quote) => {
/// // Do something with the market depth data.
/// println!("{quote:?}");
/// }
/// StreamMarketDepthQuotesResp::Heartbeat(heartbeat) => {
/// // Response for periodic signals letting you know the connection is
/// // still alive. A heartbeat is sent every 5 seconds of inactivity.
/// println!("{heartbeat:?}");
///
/// // for the sake of this example after we recieve the
/// // tenth heartbeat, we will stop the stream session.
/// if heartbeat.heartbeat > 10 {
/// // Example: stopping a stream connection
/// return Err(Error::StopStream);
/// }
/// }
/// StreamMarketDepthQuotesResp::Status(status) => {
/// // Signal sent on state changes in the stream
/// // (closed, opened, paused, resumed)
/// println!("{status:?}");
/// }
/// StreamMarketDepthQuotesResp::Error(err) => {
/// // Response for when an error was encountered,
/// // with details on the error
/// println!("{err:?}");
/// }
/// }
///
/// Ok(())
/// })
/// .await?;
///
/// // After the stream ends print all the collected market depth quotes
/// println!("{streamed_quotes:?}");
/// ```
pub async fn stream_market_depth_quotes<S: Into<String>, F>(
&mut self,
symbol: S,
levels: Option<i32>,
on_chunk: F,
) -> Result<MarketDepthQuotes, Error>
where
F: FnMut(StreamMarketDepthQuotesResp) -> Result<(), Error>,
{
MarketDepthQuotes::stream(self, symbol, levels, on_chunk).await
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
/// Order book / market depth quote.
pub struct MarketDepthQuote {
/// The timestamp for this quote, represented as an RFC3339 formatted
/// date, a profile of the ISO 8601 date standard.
/// E.g. `"2026-06-28T12:34:01Z"`.
pub time_stamp: String,
/// The side of the quote in the order book (Bid or Ask).
pub side: MarketDepthSide,
/// The price of the quote.
pub price: String,
/// The total number of shares offered/requested by participants.
pub size: String,
/// The number of orders aggregated together for this quote by the
/// participant (market maker or ECN).
pub order_count: i32,
/// The name of the participant associated with this quote.
pub name: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct MarketDepthAggregates {
/// Aggregated bid quotes, ordered from high to low price.
bids: Vec<MarketDepthAggregate>,
/// Aggregated ask quotes, ordered from low to high price.
asks: Vec<MarketDepthAggregate>,
}
impl MarketDepthAggregates {
fn new() -> Self {
Self {
bids: Vec::new(),
asks: Vec::new(),
}
}
/// Stream realtime aggregates of market depth for the given Symbol.
///
/// NOTE: `symbol` must be a string of a valid symbol.
/// E.g: `"AMD"`.
///
/// NOTE: `levels` must be `None` (defaults to 20 levels) or `Some(i32)`.
/// This is for specifying how many levels of price to stream quotes for.
///
/// # Example
/// ---
/// Stream market depth aggregates on December 2024 Natural Gas Futures `"NGZ24"`
/// to watch order flow, maybe detecting iceberg orders, or whatever else.
/// ```ignore
/// let depth_levels: i32 = 25;
/// let streamed_aggregates = client
/// .stream_market_depth_aggregates("NGZ24", Some(depth_levels), |stream_data| {
/// // The response type is `responses::MarketData::StreamAggregatesResp`
/// // which has multiple variants the main one you care about is `Aggregate`
/// // which will contain market depth aggregate data sent from the stream.
/// match stream_data {
/// StreamMarketDepthAggregatesResp::Aggregate(quote) => {
/// // Do something with the quote for example derive
/// // a quote for a long amd short nvidia trade.
/// println!("{quote:?}");
/// }
/// StreamMarketDepthAggregatesResp::Heartbeat(heartbeat) => {
/// // Response for periodic signals letting you know the connection is
/// // still alive. A heartbeat is sent every 5 seconds of inactivity.
/// println!("{heartbeat:?}");
///
/// // for the sake of this example after we recieve the
/// // tenth heartbeat, we will stop the stream session.
/// if heartbeat.heartbeat > 10 {
/// // Example: stopping a stream connection
/// return Err(Error::StopStream);
/// }
/// }
/// StreamMarketDepthAggregatesResp::Status(status) => {
/// // Signal sent on state changes in the stream
/// // (closed, opened, paused, resumed)
/// println!("{status:?}");
/// }
/// StreamMarketDepthAggregatesResp::Error(err) => {
/// // Response for when an error was encountered,
/// // with details on the error
/// println!("{err:?}");
/// }
/// }
///
/// Ok(())
/// })
/// .await?;
///
/// // After the stream ends print all the collected
/// // market depth aggregates.
/// println!("{streamed_aggregates:?}");
/// ```
pub async fn stream<F, S: Into<String>>(
client: &mut Client,
symbol: S,
levels: Option<i32>,
mut on_chunk: F,
) -> Result<MarketDepthAggregates, Error>
where
F: FnMut(StreamMarketDepthAggregatesResp) -> Result<(), Error>,
{
let endpoint = format!(
"marketdata/stream/marketdepth/aggregates/{}?maxlevels={}",
symbol.into(),
levels.unwrap_or(20),
);
let mut collected_market_depth_quotes = MarketDepthAggregates::new();
client
.stream(&endpoint, |chunk| {
let parsed_chunk =
serde_json::from_value::<StreamMarketDepthAggregatesResp>(chunk)?;
on_chunk(parsed_chunk.clone())?;
// Only collect quotes, so when the stream is done
// all the quotes that were streamed can be returned.
if let StreamMarketDepthAggregatesResp::Aggregate(quote) = parsed_chunk {
if let Some(bid) = quote.bids.first() {
collected_market_depth_quotes.bids.push(bid.clone());
}
if let Some(ask) = quote.asks.first() {
collected_market_depth_quotes.asks.push(ask.clone());
}
}
Ok(())
})
.await?;
Ok(collected_market_depth_quotes)
}
}
impl Client {
/// Stream realtime aggregates of market depth for the given Symbol.
///
/// NOTE: `symbol` must be a string of a valid symbol.
/// E.g: `"AMD"`.
///
/// NOTE: `levels` must be `None` (defaults to 20 levels) or `Some(i32)`.
/// This is for specifying how many levels of price to stream quotes for.
///
/// # Example
/// ---
/// Stream market depth aggregates on December 2024 Natural Gas Futures `"NGZ24"`
/// to watch order flow, maybe detecting iceberg orders, or whatever else.
/// ```ignore
/// let depth_levels: i32 = 25;
/// let streamed_aggregates = client
/// .stream_market_depth_aggregates("NGZ24", Some(depth_levels), |stream_data| {
/// // The response type is `responses::MarketData::StreamAggregatesResp`
/// // which has multiple variants the main one you care about is `Aggregate`
/// // which will contain market depth aggregate data sent from the stream.
/// match stream_data {
/// StreamMarketDepthAggregatesResp::Aggregate(quote) => {
/// // Do something with the quote for example derive
/// // a quote for a long amd short nvidia trade.
/// println!("{quote:?}");
/// }
/// StreamMarketDepthAggregatesResp::Heartbeat(heartbeat) => {
/// // Response for periodic signals letting you know the connection is
/// // still alive. A heartbeat is sent every 5 seconds of inactivity.
/// println!("{heartbeat:?}");
///
/// // for the sake of this example after we recieve the
/// // tenth heartbeat, we will stop the stream session.
/// if heartbeat.heartbeat > 10 {
/// // Example: stopping a stream connection
/// return Err(Error::StopStream);
/// }
/// }
/// StreamMarketDepthAggregatesResp::Status(status) => {
/// // Signal sent on state changes in the stream
/// // (closed, opened, paused, resumed)
/// println!("{status:?}");
/// }
/// StreamMarketDepthAggregatesResp::Error(err) => {
/// // Response for when an error was encountered,
/// // with details on the error
/// println!("{err:?}");
/// }
/// }
///
/// Ok(())
/// })
/// .await?;
///
/// // After the stream ends print all the collected
/// // market depth aggregates.
/// println!("{streamed_aggregates:?}");
/// ```
pub async fn stream_market_depth_aggregates<S: Into<String>, F>(
&mut self,
symbol: S,
levels: Option<i32>,
on_chunk: F,
) -> Result<MarketDepthAggregates, Error>
where
F: FnMut(StreamMarketDepthAggregatesResp) -> Result<(), Error>,
{
MarketDepthAggregates::stream(self, symbol, levels, on_chunk).await
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
/// Aggregated quotes, ordered from high to low price.
pub struct MarketDepthAggregate {
/// The earliest participant timestamp for this quote, represented as an
/// RFC3339 formatted date, a profile of the ISO 8601 date standard.
/// E.g. `"2022-06-28T12:34:01Z"`.
pub earliest_time: String,
/// The latest participant timestamp for this quote, represented as an
/// RFC3339 formatted date, a profile of the ISO 8601 date standard.
/// E.g. `"2022-06-28T12:34:56Z"`.
pub latest_time: String,
/// The side of the quote on the order book (bid or ask).
pub side: MarketDepthSide,
/// The price of the quote.
pub price: String,
/// The total number of shares, or contracts offered/requested by all participants.
pub total_size: String,
/// The largest number of shares, or contracts offered/requested by any participant.
pub biggest_size: String,
/// The smallest number of shares, or contracts offered/requested by any participant.
pub smallest_size: String,
/// The number of participants offering/requesting this price.
pub num_participants: i32,
/// The sum of the order counts for all participants offering/requesting
/// this price.
pub total_order_count: i32,
}
#[derive(Clone, Debug, Deserialize, Serialize, Copy)]
/// The different sides of the market depth order book.
pub enum MarketDepthSide {
/// Represents the bid side of the order book.
Bid,
/// Represents the ask side of the order book.
Ask,
}