1use derive_more::Display;
2use exc_service::{ExchangeError, Request};
3use futures::stream::BoxStream;
4use indicator::{Tick, TickValue, Tickable};
5use rust_decimal::Decimal;
6use serde::{Deserialize, Serialize};
7use time::OffsetDateTime;
8
9use crate::Str;
10
11pub type TradeStream = BoxStream<'static, Result<Trade, ExchangeError>>;
13
14#[derive(Debug, Clone, Copy, Serialize, Deserialize, Display)]
16#[display(fmt = "ts={ts} ({price}, {size}, {buy})")]
17pub struct Trade {
18 #[serde(with = "time::serde::rfc3339")]
20 pub ts: OffsetDateTime,
21 pub price: Decimal,
23 pub size: Decimal,
25 pub buy: bool,
27}
28
29#[derive(Debug, Clone)]
31pub struct SubscribeTrades {
32 pub instrument: Str,
34}
35
36impl SubscribeTrades {
37 pub fn new(inst: impl AsRef<str>) -> Self {
39 Self {
40 instrument: Str::new(inst),
41 }
42 }
43}
44
45impl Request for SubscribeTrades {
46 type Response = TradeStream;
47}
48
49impl Tickable for Trade {
50 type Value = Self;
51
52 fn tick(&self) -> Tick {
53 Tick::new(self.ts)
54 }
55
56 fn value(&self) -> &Self::Value {
57 self
58 }
59
60 fn into_tick_value(self) -> TickValue<Self::Value> {
61 TickValue::new(self.ts, self)
62 }
63}