Skip to main content

tardis_rs/machine/
models.rs

1use crate::Exchange;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4
5/// The options that can be specified for calling Tardis Machine Server's replay-normalized.
6#[derive(Debug, Clone, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct ReplayNormalizedRequestOptions {
9    /// Requested [`Exchange`].
10    pub exchange: Exchange,
11
12    /// Optional symbols of requested historical data feed.
13    /// Use /exchanges/:exchange HTTP API to get allowed symbols for requested exchange.
14    #[serde(skip_serializing_if = "Option::is_none")]
15    #[serde(default)]
16    pub symbols: Option<Vec<String>>,
17
18    /// Replay period start date (UTC) in a ISO 8601 format, e.g., 2019-04-01
19    pub from: DateTime<Utc>,
20
21    /// Replay period start date (UTC) in a ISO 8601 format, e.g., 2019-04-02
22    pub to: DateTime<Utc>,
23
24    /// Array of normalized [data types](https://docs.tardis.dev/api/tardis-machine#normalized-data-types)
25    /// for which real-time data will be provided.
26    pub data_types: Vec<String>,
27
28    /// When set to true, sends also disconnect messages that mark events when real-time WebSocket
29    /// connection that was used to collect the historical data got disconnected.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    #[serde(default)]
32    pub with_disconnect_messages: Option<bool>,
33}
34
35/// The options that can be specified for calling Tardis Machine Server's stream-normalized.
36#[derive(Debug, Clone, Serialize)]
37#[serde(rename_all = "camelCase")]
38pub struct StreamNormalizedRequestOptions {
39    /// Requested [`Exchange`].
40    pub exchange: Exchange,
41
42    /// Optional symbols of requested real-time data feed.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[serde(default)]
45    pub symbols: Option<Vec<String>>,
46
47    /// Array of normalized [data types](https://docs.tardis.dev/api/tardis-machine#normalized-data-types)
48    /// for which real-time data will be provided.
49    pub data_types: Vec<String>,
50
51    /// When set to true, sends disconnect messages anytime underlying exchange real-time WebSocket
52    /// connection(s) gets disconnected.
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[serde(default)]
55    pub with_disconnect_messages: Option<bool>,
56
57    /// Specifies time in milliseconds after which connection to real-time exchanges' WebSocket API
58    /// is restarted if no message has been received.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    #[serde(default, rename = "timeoutIntervalMS")]
61    pub timeout_interval_ms: Option<u64>,
62}
63
64/// The possible type of message returned from Tardis Machine Server.
65#[allow(missing_docs)]
66#[derive(Debug, Clone, Serialize, Deserialize)]
67#[serde(rename_all = "snake_case", tag = "type")]
68pub enum Message {
69    Trade(Trade),
70    BookChange(BookChange),
71    DerivativeTicker(DerivativeTicker),
72    BookSnapshot(BookSnapshot),
73    TradeBar(TradeBar),
74    Disconnect(Disconnect),
75}
76
77/// Side of the trade.
78#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
79#[serde(rename_all = "lowercase")]
80pub enum TradeSide {
81    /// Buy order.
82    Buy,
83
84    /// Sell order.
85    Sell,
86
87    /// Unknown order.
88    Unknown,
89}
90
91/// Individual trade.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93#[serde(rename_all = "camelCase")]
94pub struct Trade {
95    /// Instrument symbol as provided by exchange
96    pub symbol: String,
97
98    /// Exchange ID
99    pub exchange: Exchange,
100
101    /// Trade id if provided by exchange
102    pub id: Option<String>,
103
104    /// Trade price as provided by exchange
105    pub price: f64,
106
107    /// Trade amount as provided by exchange
108    pub amount: f64,
109
110    /// Liquidity taker side (aggressor)
111    pub side: TradeSide,
112
113    /// Trade timestamp provided by exchange (ISO 8601 format)
114    pub timestamp: DateTime<Utc>,
115
116    /// Message arrival timestamp (ISO 8601 format)
117    pub local_timestamp: DateTime<Utc>,
118}
119
120/// Initial L2 (market by price) order book snapshot (isSnapshot=true) plus incremental updates for
121/// each order book change.  Please note that amount is the updated amount at that price level,
122/// not a delta. An amount of 0 indicates the price level can be removed.
123#[derive(Debug, Clone, Serialize, Deserialize)]
124#[serde(rename_all = "camelCase")]
125pub struct BookChange {
126    /// Instrument symbol as provided by exchange
127    pub symbol: String,
128
129    /// Exchange ID
130    pub exchange: Exchange,
131
132    /// If true marks initial order book snapshot
133    pub is_snapshot: bool,
134
135    /// Updated bids price-amount levels
136    pub bids: Vec<BookLevel>,
137
138    /// Updated asks price-amount levels
139    pub asks: Vec<BookLevel>,
140
141    /// Order book update timestamp if provided by exchange,
142    /// otherwise equals to localTimestamp, (ISO 8601 format)
143    pub timestamp: DateTime<Utc>,
144
145    /// Message arrival timestamp (ISO 8601 format)
146    pub local_timestamp: DateTime<Utc>,
147}
148
149/// Derivative instrument ticker info sourced from real-time ticker & instrument channels.
150#[derive(Debug, Clone, Serialize, Deserialize)]
151#[serde(rename_all = "camelCase")]
152pub struct DerivativeTicker {
153    /// Instrument symbol as provided by exchange
154    pub symbol: String,
155
156    /// Exchange ID
157    pub exchange: Exchange,
158
159    /// Last instrument price if provided by exchange
160    pub last_price: Option<f64>,
161
162    /// Last open interest if provided by exchange
163    pub open_interest: Option<f64>,
164
165    /// Last funding rate if provided by exchange
166    pub funding_rate: Option<f64>,
167
168    /// Last index price if provided by exchange
169    pub index_price: Option<f64>,
170
171    /// Last mark price if provided by exchange
172    pub mark_price: Option<f64>,
173
174    /// Message timestamp provided by exchange (ISO 8601 format)
175    pub timestamp: DateTime<Utc>,
176
177    /// Message arrival timestamp (ISO 8601 format)
178    pub local_timestamp: DateTime<Utc>,
179}
180
181/// A particular level in the order book.
182#[derive(Debug, Clone, Serialize, Deserialize)]
183#[serde(rename_all = "camelCase")]
184pub struct BookLevel {
185    /// The desired price of the order.
186    pub price: f64,
187
188    /// The quantity of the order.
189    pub amount: f64,
190}
191
192/// Order book snapshot for selected number_of_levels (top bids and asks), snapshot_interval and time_unit.
193/// When snapshot_interval is set to 0 , snapshots are taken anytime order book state within specified
194/// levels has changed, otherwise snapshots are taken anytime snapshot_interval time has passed and
195/// there was an order book state change within specified levels. Order book snapshots are computed
196/// from exchanges' real-time order book streaming L2 data (market by price).
197#[derive(Debug, Clone, Serialize, Deserialize)]
198#[serde(rename_all = "camelCase")]
199pub struct BookSnapshot {
200    /// Instrument symbol as provided by exchange
201    pub symbol: String,
202
203    /// Exchange ID
204    pub exchange: Exchange,
205
206    /// Name with format book_snapshot_{depth}_{interval}{time_unit}
207    pub name: String,
208
209    /// Requested number of levels (top bids/asks)
210    pub depth: u64,
211
212    /// Requested snapshot interval in milliseconds
213    pub interval: u64,
214
215    /// Top "depth" bids price-amount levels
216    pub bids: Vec<BookLevel>,
217
218    /// Top "depth" asks price-amount levels
219    pub asks: Vec<BookLevel>,
220
221    /// Snapshot timestamp based on last book_change message processed timestamp adjusted to snapshot interval
222    pub timestamp: DateTime<Utc>,
223
224    /// Message arrival timestamp that triggered snapshot (ISO 8601 format)
225    pub local_timestamp: DateTime<Utc>,
226}
227
228/// Kind of the trade bar.
229#[allow(missing_docs)]
230#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
231#[serde(rename_all = "lowercase")]
232pub enum TradeBarKind {
233    Time,
234    Volume,
235    Tick,
236}
237
238/// Trades data in aggregated form, known as OHLC, candlesticks, klines etc. Not only most common
239/// time based aggregation is supported, but volume and tick count based as well. Bars are computed
240/// from tick-by-tick raw trade data, if in given interval no trades happened, there is no bar produced.
241#[derive(Debug, Clone, Serialize, Deserialize)]
242#[serde(rename_all = "camelCase")]
243pub struct TradeBar {
244    /// Instrument symbol as provided by exchange
245    pub symbol: String,
246
247    /// Exchange ID
248    pub exchange: Exchange,
249
250    /// name with format trade_bar_{interval}
251    pub name: String,
252
253    /// requested trade bar interval
254    pub interval: u64,
255
256    /// open price
257    pub open: f64,
258
259    /// high price
260    pub high: f64,
261
262    /// low price
263    pub low: f64,
264
265    /// close price
266    pub close: f64,
267
268    /// total volume traded in given interval
269    pub volume: f64,
270
271    /// buy volume traded in given interval
272    pub buy_volume: f64,
273
274    /// sell volume traded in given interval
275    pub sell_volume: f64,
276
277    /// trades count in given interval
278    pub trades: u64,
279
280    /// volume weighted average price
281    pub vwap: f64,
282
283    /// timestamp of first trade for given bar (ISO 8601 format)
284    pub open_timestamp: DateTime<Utc>,
285
286    /// timestamp of last trade for given bar (ISO 8601 format)
287    pub close_timestamp: DateTime<Utc>,
288
289    /// end of interval period timestamp (ISO 8601 format)
290    pub timestamp: DateTime<Utc>,
291
292    /// message arrival timestamp that triggered given bar computation (ISO 8601 format)
293    pub local_timestamp: DateTime<Utc>,
294}
295
296/// Message that marks events when real-time WebSocket connection that was used to collect the
297/// historical data got disconnected.
298#[derive(Debug, Clone, Serialize, Deserialize)]
299#[serde(rename_all = "camelCase")]
300pub struct Disconnect {
301    /// Exchange ID
302    pub exchange: Exchange,
303
304    /// message arrival timestamp that triggered given bar computation (ISO 8601 format)
305    pub local_timestamp: DateTime<Utc>,
306}