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
use crate::{
responses::MarketData::{GetBarsResp, GetBarsRespRaw, StreamBarsResp},
Client, Error,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
/// Market Data Bars (candlestick bars)
pub struct Bar {
/// The closing price of the current bar.
pub close: String,
/// The number of times a trade was made at a price
/// less than or equal to the previous trade price.
pub down_ticks: u64,
/// The number of shares or contracts traded on down ticks.
pub down_volume: u64,
/// The unix epoch time.
pub epoch: i64,
/// The highest price traded in the current bar.
pub high: String,
/// Conveys that all historical bars in the request have been delivered.
pub is_end_of_history: bool,
/// Set when there is data in the bar and the data
/// is being built in "real time" from a trade.
pub is_real_time: Option<bool>,
/// The lowest price traded in the current bar.
pub low: String,
/// The opening price of the current bar.
pub open: String,
/// The number of open contracts.
///
/// NOTE: Futures and Options ONLY.
pub open_interest: Option<String>,
/// Timestamp represented as an RFC3339 formatted date.
/// E.g: `"2024-09-01T23:30:30Z"`
pub time_stamp: String,
/// The total number of ticks (upticks and downticks together).
pub total_ticks: u64,
/// The sum of up and down volume.
pub total_volume: String,
/// The number of times a trade was made at the same price
/// of the previous trade price.
///
/// DEPRECATED: it's value will always be 0
pub unchanged_ticks: u8,
/// The number of shares or contracts traded on unchanged ticks.
///
/// DEPRECATED: it's value will always be 0
pub unchanged_volume: u8,
/// The number of times a trade was made at a price
/// greater than or equal to the previous trade price.
pub up_ticks: u64,
/// The number of shares or contracts traded on up ticks.
pub up_volume: u64,
/// Indicates if the current `Bar` is Open or Closed.
pub bar_status: BarStatus,
}
impl Bar {
/// Fetch `Vec<Bar>` for a given query `GetBarsQuery`
///
/// # Example
/// ---
///
/// Get the 10 most recent 5 minute bars of trading
/// activity for November 2024 Crude Oil Futures.
/// ```ignore
/// let fetch_bars_query = MarketData::GetBarsQueryBuilder::new()
/// .symbol("CLX24")
/// .unit(BarUnit::Minute)
/// .interval("5")
/// .bars_back("10")
/// .build()?;
///
/// let bars = client.fetch_bars(&fetch_bars_query).await?;
///
/// // Do something with the bars, maybe make a chart?
/// println!("{bars:?}");
/// ```
pub async fn fetch(client: &mut Client, query: &GetBarsQuery) -> Result<Vec<Bar>, Error> {
let endpoint = format!(
"marketdata/barcharts/{}{}",
query.symbol,
query.as_query_string()
);
let resp_raw = client
.get(&endpoint)
.await?
.json::<GetBarsRespRaw>()
.await?;
let resp: GetBarsResp = resp_raw.into();
if let Some(bars) = resp.bars {
Ok(bars)
} else {
Err(resp.error.unwrap_or(Error::UnknownTradeStationAPIError))
}
}
/// Stream bars of market activity for a given query `GetBarsQuery`
///
/// # Example
/// ---
///
/// Stream bars of November 2024 Crude Oil Futures trading activity
/// in 4 hour (240 minute) intervals.
/// ```ignore
/// let stream_bars_query = MarketData::StreamBarsQueryBuilder::new()
/// .symbol("CLX24")
/// .unit(BarUnit::Minute)
/// .interval("240")
/// .build()?;
///
/// let streamed_bars = client
/// .stream_bars(&stream_bars_query, |stream_data| {
/// // The response type is `responses::market_data::StreamBarsResp`
/// // which has multiple variants the main one you care about is
/// // `Bar` which will contain order data sent from the stream.
/// match stream_data {
/// StreamBarsResp::Bar(bar) => {
/// // Do something with the bars like making a chart
/// println!("{bar:?}")
/// }
/// StreamBarsResp::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);
/// }
/// }
/// StreamBarsResp::Status(status) => {
/// // Signal sent on state changes in the stream
/// // (closed, opened, paused, resumed)
/// println!("{status:?}");
/// }
/// StreamBarsResp::Error(err) => {
/// // Response for when an error was encountered,
/// // with details on the error
/// println!("{err:?}");
/// }
/// }
///
/// Ok(())
/// })
/// .await?;
///
/// // All the bars collected during the stream
/// println!("{streamed_bars:?}");
/// ```
pub async fn stream_bars<F>(
client: &mut Client,
query: &StreamBarsQuery,
mut on_chunk: F,
) -> Result<Vec<Bar>, Error>
where
F: FnMut(StreamBarsResp) -> Result<(), Error>,
{
let endpoint = format!(
"marketdata/stream/barcharts/{}{}",
query.symbol,
query.as_query_string()
);
let mut collected_bars: Vec<Bar> = Vec::new();
client
.stream(&endpoint, |chunk| {
let parsed_chunk = serde_json::from_value::<StreamBarsResp>(chunk)?;
on_chunk(parsed_chunk.clone())?;
// Only collect orders, so when the stream is done
// all the orders that were streamed can be returned
if let StreamBarsResp::Bar(bar) = parsed_chunk {
collected_bars.push(*bar);
}
Ok(())
})
.await?;
Ok(collected_bars)
}
}
impl Client {
/// Fetch `Vec<Bar>` for a given query `GetBarsQuery`
pub async fn fetch_bars(&mut self, query: &GetBarsQuery) -> Result<Vec<Bar>, Error> {
Bar::fetch(self, query).await
}
/// Stream bars of market activity for a q given query `StreamBarsQuery`
pub async fn stream_bars<F>(
&mut self,
query: &StreamBarsQuery,
on_chunk: F,
) -> Result<Vec<Bar>, Error>
where
F: FnMut(StreamBarsResp) -> Result<(), Error>,
{
Bar::stream_bars(self, query, on_chunk).await
}
}
pub struct GetBarsQuery {
/// The symbol of the security you want bars for.
///
/// E.g: `"SR3Z24"` for bars on Three Month SOFR Futures December 2024 Contract.
/// or
/// E.g: `"PLTR"` for bars on the stock Palantir.
pub symbol: String,
/// The interval (of time units) that each bar will consist of
///
/// NOTE: Always defaults to 1, and if using the unit `BarUnit::Minute`
/// then the max allowed interval is 1440.
///
/// E.g: If unit is set to `BarUnit::Minute` than an interval of 5
/// would mean each `Bar` is a 5 minute aggregation of market data.
pub interval: i16,
/// The unit of measurement for time in each bar interval.
pub unit: BarUnit,
/// Number of bars back to fetch.
///
/// NOTE: Always defaults to 1, and the max number of intraday bars back
/// is 57,600. There is no limit on `BarUnit::Daily`, `BarUnit::Weekly`,
/// or `BarUnit::Monthly` unit.
///
/// NOTE: This parameter is mutually exclusive with the `first_date` parameter.
pub bars_back: i16,
/// The first date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
///
/// NOTE: This parameter is mutually exclusive with the `bars_back` parameter.
pub first_date: String,
/// The last date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
///
/// NOTE: Defaults to the current timestamp.
///
/// NOTE: This parameter is mutually exclusive with the `start_date` parameter
/// and should be used instead of that parameter, since startdate is deprecated.
pub last_date: String,
/// The United States (US) stock market session template.
///
/// NOTE: Ignored for non U.S equity symbols.
pub session_template: SessionTemplate,
/// DEPRECATED: Use `last_date` instead of `start_date` !
///
/// The last date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
pub start_date: String,
}
impl GetBarsQuery {
pub fn as_query_string(&self) -> String {
let mut query_string = String::from("?");
query_string.push_str(&format!("interval={}&", self.interval));
query_string.push_str(&format!("unit={:?}&", self.unit));
query_string.push_str(&format!("barsBack={}&", self.bars_back));
if !self.first_date.is_empty() {
query_string.push_str(&format!("firstDate={}&", self.first_date));
}
if !self.last_date.is_empty() {
query_string.push_str(&format!("lastDate={}&", self.last_date));
}
query_string.push_str(&format!("sessionTemplate={:?}&", self.session_template));
if !self.start_date.is_empty() {
query_string.push_str(&format!("startDate={}&", self.start_date));
}
if query_string.ends_with('&') {
query_string.pop();
}
query_string
}
}
pub struct StreamBarsQuery {
/// The symbol of the security you want bars for.
///
/// E.g: `"SR3Z24"` for bars on Three Month SOFR Futures December 2024 Contract.
/// or
/// E.g: `"PLTR"` for bars on the stock Palantir.
pub symbol: String,
/// The interval (of time units) that each bar will consist of
///
/// NOTE: Always defaults to 1, and if using the unit `BarUnit::Minute`
/// then the max allowed interval is 1440.
///
/// E.g: If unit is set to `BarUnit::Minute` than an interval of 5
/// would mean each `Bar` is a 5 minute aggregation of market data.
pub interval: i16,
/// The unit of measurement for time in each bar interval.
pub unit: BarUnit,
/// Number of bars back to fetch.
///
/// NOTE: Always defaults to 1, and the max number of intraday bars back
/// is 57,600. There is no limit on `BarUnit::Daily`, `BarUnit::Weekly`,
/// or `BarUnit::Monthly` unit.
///
/// NOTE: This parameter is mutually exclusive with the `first_date` parameter.
pub bars_back: i16,
/// The United States (US) stock market session template.
///
/// NOTE: Ignored for non U.S equity symbols.
pub session_template: SessionTemplate,
}
impl StreamBarsQuery {
pub fn as_query_string(&self) -> String {
let mut query_string = String::from("?");
query_string.push_str(&format!("interval={}&", self.interval));
query_string.push_str(&format!("unit={:?}&", self.unit));
query_string.push_str(&format!("barsBack={}&", self.bars_back));
query_string.push_str(&format!("sessionTemplate={:?}&", self.session_template));
if query_string.ends_with('&') {
query_string.pop();
}
query_string
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The different types of Market Session Templates.
pub enum SessionTemplate {
/// U.S Equities Pre Market Session Template
USEQPre,
/// U.S Equities Post Market Session Template
USEQPost,
/// U.S Equities Pre And Post Market Session Template
USEQPreAndPost,
/// U.S Equities 24 Hour Session Template
USEQ24Hour,
/// U.S Equities Normal Market Session Template
Default,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The types of unit of measurement for time in each bar interval.
pub enum BarUnit {
/// Minute Bars
Minute,
/// Daily Bars
Daily,
/// Weekly Bars
Weekly,
/// Monthly Bars
Monthly,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The 2 types of state a `Bar` can be in, open or closed.
pub enum BarStatus {
/// Indicates the `Bar` is still trading.
Open,
/// Indicates the `Bar` is finished trading.
Closed,
}
#[derive(Debug, Default)]
/// Builder pattern struct for `GetBarsQuery`.
pub struct GetBarsQueryBuilder {
/// The symbol of the security you want bars for.
///
/// E.g: `"SR3Z24"` for bars on Three Month SOFR Futures December 2024 Contract.
/// or
/// E.g: `"PLTR"` for bars on the stock Palantir.
symbol: Option<String>,
/// The interval (of time units) that each bar will consist of
///
/// NOTE: Always defaults to 1, and if using the unit `BarUnit::Minute`
/// then the max allowed interval is 1440.
///
/// E.g: If unit is set to `BarUnit::Minute` than an interval of 5
/// would mean each `Bar` is a 5 minute aggregation of market data.
interval: Option<i16>,
/// The unit of measurement for time in each bar interval.
unit: Option<BarUnit>,
/// Number of bars back to fetch.
///
/// NOTE: Always defaults to 1, and the max number of intraday bars back
/// is 57,600. There is no limit on `BarUnit::Daily`, `BarUnit::Weekly`,
/// or `BarUnit::Monthly` unit.
///
/// NOTE: This parameter is mutually exclusive with the `first_date` parameter.
bars_back: Option<i16>,
/// The first date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
///
/// NOTE: This parameter is mutually exclusive with the `bars_back` parameter.
first_date: Option<String>,
/// The last date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
///
/// NOTE: Defaults to the current timestamp.
///
/// NOTE: This parameter is mutually exclusive with the `start_date` parameter
/// and should be used instead of that parameter, since startdate is deprecated.
last_date: Option<String>,
/// The United States (US) stock market session template.
///
/// NOTE: Ignored for non U.S equity symbols.
session_template: Option<SessionTemplate>,
/// DEPRECATED: Use `last_date` instead of `start_date` !
///
/// The last date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
start_date: Option<String>,
}
impl GetBarsQueryBuilder {
/// Initialize a new builder for `GetBarsQuery`.
pub fn new() -> Self {
GetBarsQueryBuilder {
symbol: None,
interval: None,
unit: None,
bars_back: None,
first_date: None,
last_date: None,
session_template: None,
start_date: None,
}
}
/// Set the symbol of the security you want bars for.
///
/// E.g: `"SR3Z24"` for bars on Three Month SOFR Futures December 2024 Contract.
/// or
/// E.g: `"PLTR"` for bars on the stock Palantir.
pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
self.symbol = Some(symbol.into());
self
}
/// Set the interval (of time units) that each bar will consist of
///
/// NOTE: Always defaults to 1, and if using the unit `BarUnit::Minute`
/// then the max allowed interval is 1440.
///
/// E.g: If unit is set to `BarUnit::Minute` than an interval of 5
/// would mean each `Bar` is a 5 minute aggregation of market data.
pub fn interval(mut self, interval: i16) -> Self {
self.interval = Some(interval);
self
}
/// Set the unit of measurement for time in each bar interval.
pub fn unit(mut self, unit: BarUnit) -> Self {
self.unit = Some(unit);
self
}
/// Set the number of bars back to fetch.
///
/// NOTE: Always defaults to 1, and the max number of intraday bars back
/// is 57,600. There is no limit on `BarUnit::Daily`, `BarUnit::Weekly`,
/// or `BarUnit::Monthly` unit.
///
/// NOTE: This parameter is mutually exclusive with the `first_date` parameter.
pub fn bars_back(mut self, bars_back: i16) -> Self {
self.bars_back = Some(bars_back);
self
}
/// Set the first date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
///
/// NOTE: This parameter is mutually exclusive with the `bars_back` parameter.
pub fn first_date(mut self, first_date: impl Into<String>) -> Self {
self.first_date = Some(first_date.into());
self
}
/// Set the last date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
///
/// NOTE: Defaults to the current timestamp.
///
/// NOTE: This parameter is mutually exclusive with the `start_date` parameter
/// and should be used instead of that parameter, since startdate is deprecated.
pub fn last_date(mut self, last_date: impl Into<String>) -> Self {
self.last_date = Some(last_date.into());
self
}
/// Set the United States (US) stock market session template.
///
/// NOTE: Ignored for non U.S equity symbols.
pub fn session_template(mut self, session_template: SessionTemplate) -> Self {
self.session_template = Some(session_template);
self
}
/// DEPRECATED: Use `last_date` instead of `start_date` !
///
/// Set the last date formatted as `"YYYY-MM-DD"`, or `"2020-04-20T18:00:00Z"`.
pub fn start_date(mut self, start_date: impl Into<String>) -> Self {
self.start_date = Some(start_date.into());
self
}
/// Finish building, returning a `GetBarsQuery`.
///
/// NOTE: You must set `symbol` before calling `build`.
pub fn build(self) -> Result<GetBarsQuery, Error> {
Ok(GetBarsQuery {
symbol: self.symbol.ok_or_else(|| Error::SymbolNotSet)?,
interval: self.interval.unwrap_or(1),
unit: self.unit.unwrap_or(BarUnit::Daily),
bars_back: self.bars_back.unwrap_or(1),
first_date: self.first_date.unwrap_or_default(),
last_date: self.last_date.unwrap_or_default(),
session_template: self.session_template.unwrap_or(SessionTemplate::Default),
start_date: self.start_date.unwrap_or_default(),
})
}
}
#[derive(Debug, Default)]
/// Builder pattern struct for `StreamBarsQuery`.
pub struct StreamBarsQueryBuilder {
/// The symbol of the security you want bars for.
///
/// E.g: `"SR3Z24"` for bars on Three Month SOFR Futures December 2024 Contract.
/// or
/// E.g: `"PLTR"` for bars on the stock Palantir.
symbol: Option<String>,
/// The interval (of time units) that each bar will consist of
///
/// NOTE: Always defaults to 1, and if using the unit `BarUnit::Minute`
/// then the max allowed interval is 1440.
///
/// E.g: If unit is set to `BarUnit::Minute` than an interval of 5
/// would mean each `Bar` is a 5 minute aggregation of market data.
interval: Option<i16>,
/// The unit of measurement for time in each bar interval.
unit: Option<BarUnit>,
/// Number of bars back to fetch.
///
/// NOTE: Always defaults to 1, and the max number of intraday bars back
/// is 57,600. There is no limit on `BarUnit::Daily`, `BarUnit::Weekly`,
/// or `BarUnit::Monthly` unit.
///
/// NOTE: This parameter is mutually exclusive with the `first_date` parameter.
bars_back: Option<i16>,
/// The United States (US) stock market session template.
///
/// NOTE: Ignored for non U.S equity symbols.
session_template: Option<SessionTemplate>,
}
impl StreamBarsQueryBuilder {
/// Initialize a new builder for `GetBarsQuery`.
pub fn new() -> Self {
StreamBarsQueryBuilder {
symbol: None,
interval: None,
unit: None,
bars_back: None,
session_template: None,
}
}
/// Set the symbol of the security you want bars for.
///
/// E.g: `"SR3Z24"` for bars on Three Month SOFR Futures December 2024 Contract.
/// or
/// E.g: `"PLTR"` for bars on the stock Palantir.
pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
self.symbol = Some(symbol.into());
self
}
/// Set the interval (of time units) that each bar will consist of
///
/// NOTE: Always defaults to 1, and if using the unit `BarUnit::Minute`
/// then the max allowed interval is 1440.
///
/// E.g: If unit is set to `BarUnit::Minute` than an interval of 5
/// would mean each `Bar` is a 5 minute aggregation of market data.
pub fn interval(mut self, interval: i16) -> Self {
self.interval = Some(interval);
self
}
/// Set the unit of measurement for time in each bar interval.
pub fn unit(mut self, unit: BarUnit) -> Self {
self.unit = Some(unit);
self
}
/// Set the number of bars back to fetch.
///
/// NOTE: Always defaults to 1, and the max number of intraday bars back
/// is 57,600. There is no limit on `BarUnit::Daily`, `BarUnit::Weekly`,
/// or `BarUnit::Monthly` unit.
///
/// NOTE: This parameter is mutually exclusive with the `first_date` parameter.
pub fn bars_back(mut self, bars_back: i16) -> Self {
self.bars_back = Some(bars_back);
self
}
/// Set the United States (US) stock market session template.
///
/// NOTE: Ignored for non U.S equity symbols.
pub fn session_template(mut self, session_template: SessionTemplate) -> Self {
self.session_template = Some(session_template);
self
}
/// Finish building, returning a `StreamBarsQuery`.
///
/// NOTE: You must set `symbol` before calling `build`.
pub fn build(self) -> Result<StreamBarsQuery, Error> {
Ok(StreamBarsQuery {
symbol: self.symbol.ok_or_else(|| Error::SymbolNotSet)?,
interval: self.interval.unwrap_or(1),
unit: self.unit.unwrap_or(BarUnit::Daily),
bars_back: self.bars_back.unwrap_or(1),
session_template: self.session_template.unwrap_or(SessionTemplate::Default),
})
}
}