Skip to main content

rustrade_data/
event.rs

1use crate::{
2    error::DataError,
3    streams::consumer::MarketStreamResult,
4    subscription::{
5        book::{OrderBookEvent, OrderBookL1},
6        candle::Candle,
7        greeks::OptionGreeks,
8        liquidation::Liquidation,
9        trade::PublicTrade,
10    },
11};
12use chrono::{DateTime, Utc};
13use derive_more::From;
14use rustrade_instrument::{exchange::ExchangeId, instrument::market_data::MarketDataInstrument};
15use serde::{Deserialize, Serialize};
16
17/// Convenient new type containing a collection of [`MarketEvent<T>`](MarketEvent)s.
18#[derive(Debug)]
19pub struct MarketIter<InstrumentKey, T>(pub Vec<Result<MarketEvent<InstrumentKey, T>, DataError>>);
20
21impl<InstrumentKey, T> FromIterator<Result<MarketEvent<InstrumentKey, T>, DataError>>
22    for MarketIter<InstrumentKey, T>
23{
24    fn from_iter<Iter>(iter: Iter) -> Self
25    where
26        Iter: IntoIterator<Item = Result<MarketEvent<InstrumentKey, T>, DataError>>,
27    {
28        Self(iter.into_iter().collect())
29    }
30}
31
32/// Normalised Barter [`MarketEvent<T>`](Self) wrapping the `T` data variant in metadata.
33///
34/// Note: `T` can be an enum such as the [`DataKind`] if required.
35///
36/// See [`crate::subscription`] for all existing Barter Market event variants.
37///
38/// ### Examples
39/// - [`MarketEvent<PublicTrade>`](PublicTrade)
40/// - [`MarketEvent<OrderBookL1>`](OrderBookL1)
41/// - [`MarketEvent<DataKind>`](DataKind)
42#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
43pub struct MarketEvent<InstrumentKey = MarketDataInstrument, T = DataKind> {
44    pub time_exchange: DateTime<Utc>,
45    pub time_received: DateTime<Utc>,
46    pub exchange: ExchangeId,
47    pub instrument: InstrumentKey,
48    pub kind: T,
49}
50
51impl<InstrumentKey, T> MarketEvent<InstrumentKey, T> {
52    pub fn map_kind<F, O>(self, op: F) -> MarketEvent<InstrumentKey, O>
53    where
54        F: FnOnce(T) -> O,
55    {
56        MarketEvent {
57            time_exchange: self.time_exchange,
58            time_received: self.time_received,
59            exchange: self.exchange,
60            instrument: self.instrument,
61            kind: op(self.kind),
62        }
63    }
64}
65
66impl<InstrumentKey> MarketEvent<InstrumentKey, DataKind> {
67    pub fn as_public_trade(&self) -> Option<MarketEvent<&InstrumentKey, &PublicTrade>> {
68        match &self.kind {
69            DataKind::Trade(public_trade) => Some(self.as_event(public_trade)),
70            _ => None,
71        }
72    }
73
74    pub fn as_order_book_l1(&self) -> Option<MarketEvent<&InstrumentKey, &OrderBookL1>> {
75        match &self.kind {
76            DataKind::OrderBookL1(orderbook) => Some(self.as_event(orderbook)),
77            _ => None,
78        }
79    }
80
81    pub fn as_order_book(&self) -> Option<MarketEvent<&InstrumentKey, &OrderBookEvent>> {
82        match &self.kind {
83            DataKind::OrderBook(orderbook) => Some(self.as_event(orderbook)),
84            _ => None,
85        }
86    }
87
88    pub fn as_candle(&self) -> Option<MarketEvent<&InstrumentKey, &Candle>> {
89        match &self.kind {
90            DataKind::Candle(candle) => Some(self.as_event(candle)),
91            _ => None,
92        }
93    }
94
95    pub fn as_liquidation(&self) -> Option<MarketEvent<&InstrumentKey, &Liquidation>> {
96        match &self.kind {
97            DataKind::Liquidation(liquidation) => Some(self.as_event(liquidation)),
98            _ => None,
99        }
100    }
101
102    pub fn as_option_greeks(&self) -> Option<MarketEvent<&InstrumentKey, &OptionGreeks>> {
103        match &self.kind {
104            DataKind::OptionGreeks(greeks) => Some(self.as_event(greeks)),
105            _ => None,
106        }
107    }
108
109    fn as_event<'a, K>(&'a self, kind: &'a K) -> MarketEvent<&'a InstrumentKey, &'a K> {
110        MarketEvent {
111            time_exchange: self.time_exchange,
112            time_received: self.time_received,
113            exchange: self.exchange,
114            instrument: &self.instrument,
115            kind,
116        }
117    }
118}
119
120/// Available kinds of normalised Barter [`MarketEvent<T>`](MarketEvent).
121///
122/// ### Notes
123/// - [`Self`] is only used as the [`MarketEvent<DataKind>`](MarketEvent) `Output` when combining
124///   several [`Streams<SubscriptionKind::Event>`](crate::streams::Streams) using the
125///   [`MultiStreamBuilder<Output>`](crate::streams::builder::multi::MultiStreamBuilder), or via
126///   the [`DynamicStreams::select_all`](crate::streams::builder::dynamic::DynamicStreams) method.
127/// - [`Self`] is purposefully not supported in any
128///   [`Subscription`](crate::subscription::Subscription)s directly, it is only used to
129///   make ergonomic [`Streams`](crate::streams::Streams) containing many
130///   [`MarketEvent<T>`](MarketEvent) kinds.
131#[derive(Clone, PartialEq, Debug, Deserialize, Serialize, From)]
132pub enum DataKind {
133    Trade(PublicTrade),
134    OrderBookL1(OrderBookL1),
135    OrderBook(OrderBookEvent),
136    Candle(Candle),
137    Liquidation(Liquidation),
138    OptionGreeks(OptionGreeks),
139}
140
141impl DataKind {
142    pub fn kind_name(&self) -> &str {
143        match self {
144            DataKind::Trade(_) => "public_trade",
145            DataKind::OrderBookL1(_) => "l1",
146            DataKind::OrderBook(_) => "l2",
147            DataKind::Candle(_) => "candle",
148            DataKind::Liquidation(_) => "liquidation",
149            DataKind::OptionGreeks(_) => "option_greeks",
150        }
151    }
152}
153
154impl<InstrumentKey> From<MarketEvent<InstrumentKey, OptionGreeks>>
155    for MarketEvent<InstrumentKey, DataKind>
156{
157    fn from(value: MarketEvent<InstrumentKey, OptionGreeks>) -> Self {
158        value.map_kind(OptionGreeks::into)
159    }
160}
161
162impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, OptionGreeks>>
163    for MarketStreamResult<InstrumentKey, DataKind>
164{
165    fn from(value: MarketStreamResult<InstrumentKey, OptionGreeks>) -> Self {
166        value.map_ok(MarketEvent::from)
167    }
168}
169
170impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, PublicTrade>>
171    for MarketStreamResult<InstrumentKey, DataKind>
172{
173    fn from(value: MarketStreamResult<InstrumentKey, PublicTrade>) -> Self {
174        value.map_ok(MarketEvent::from)
175    }
176}
177
178impl<InstrumentKey> From<MarketEvent<InstrumentKey, PublicTrade>>
179    for MarketEvent<InstrumentKey, DataKind>
180{
181    fn from(value: MarketEvent<InstrumentKey, PublicTrade>) -> Self {
182        value.map_kind(PublicTrade::into)
183    }
184}
185
186impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, OrderBookL1>>
187    for MarketStreamResult<InstrumentKey, DataKind>
188{
189    fn from(value: MarketStreamResult<InstrumentKey, OrderBookL1>) -> Self {
190        value.map_ok(MarketEvent::from)
191    }
192}
193
194impl<InstrumentKey> From<MarketEvent<InstrumentKey, OrderBookL1>>
195    for MarketEvent<InstrumentKey, DataKind>
196{
197    fn from(value: MarketEvent<InstrumentKey, OrderBookL1>) -> Self {
198        value.map_kind(OrderBookL1::into)
199    }
200}
201
202impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, OrderBookEvent>>
203    for MarketStreamResult<InstrumentKey, DataKind>
204{
205    fn from(value: MarketStreamResult<InstrumentKey, OrderBookEvent>) -> Self {
206        value.map_ok(MarketEvent::from)
207    }
208}
209
210impl<InstrumentKey> From<MarketEvent<InstrumentKey, OrderBookEvent>>
211    for MarketEvent<InstrumentKey, DataKind>
212{
213    fn from(value: MarketEvent<InstrumentKey, OrderBookEvent>) -> Self {
214        value.map_kind(OrderBookEvent::into)
215    }
216}
217
218impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, Candle>>
219    for MarketStreamResult<InstrumentKey, DataKind>
220{
221    fn from(value: MarketStreamResult<InstrumentKey, Candle>) -> Self {
222        value.map_ok(MarketEvent::from)
223    }
224}
225
226impl<InstrumentKey> From<MarketEvent<InstrumentKey, Candle>>
227    for MarketEvent<InstrumentKey, DataKind>
228{
229    fn from(value: MarketEvent<InstrumentKey, Candle>) -> Self {
230        value.map_kind(Candle::into)
231    }
232}
233
234impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, Liquidation>>
235    for MarketStreamResult<InstrumentKey, DataKind>
236{
237    fn from(value: MarketStreamResult<InstrumentKey, Liquidation>) -> Self {
238        value.map_ok(MarketEvent::from)
239    }
240}
241
242impl<InstrumentKey> From<MarketEvent<InstrumentKey, Liquidation>>
243    for MarketEvent<InstrumentKey, DataKind>
244{
245    fn from(value: MarketEvent<InstrumentKey, Liquidation>) -> Self {
246        value.map_kind(Liquidation::into)
247    }
248}