use crate::Str;
use derive_more::Display;
use exc_service::{ExchangeError, Request};
use futures::stream::BoxStream;
use indicator::{Tick, TickValue, Tickable};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
pub type TickerStream = BoxStream<'static, Result<Ticker, ExchangeError>>;
#[derive(Debug, Clone)]
pub struct SubscribeTickers {
pub instrument: Str,
}
impl SubscribeTickers {
pub fn new(inst: impl AsRef<str>) -> Self {
Self {
instrument: Str::new(inst),
}
}
}
impl Request for SubscribeTickers {
type Response = TickerStream;
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Display)]
#[display(
fmt = "ts={ts}, last=({last}, {size}), bid=({bid:?}, {bid_size:?}), ask=({ask:?}, {ask_size:?})"
)]
pub struct Ticker {
#[serde(with = "time::serde::rfc3339")]
pub ts: OffsetDateTime,
pub last: Decimal,
#[serde(default)]
pub size: Decimal,
#[serde(default)]
pub buy: Option<bool>,
#[serde(default)]
pub bid: Option<Decimal>,
#[serde(default)]
pub bid_size: Option<Decimal>,
#[serde(default)]
pub ask: Option<Decimal>,
#[serde(default)]
pub ask_size: Option<Decimal>,
}
impl Tickable for Ticker {
type Value = Self;
fn tick(&self) -> Tick {
Tick::new(self.ts)
}
fn value(&self) -> &Self::Value {
self
}
fn into_tick_value(self) -> TickValue<Self::Value> {
TickValue::new(self.ts, self)
}
}