rustrade_data/exchange/mod.rs
1use self::subscription::ExchangeSub;
2use crate::{
3 MarketStream, SnapshotFetcher,
4 instrument::InstrumentData,
5 subscriber::{Subscriber, validator::SubscriptionValidator},
6 subscription::{Map, SubscriptionKind},
7};
8use rustrade_instrument::exchange::ExchangeId;
9use rustrade_integration::{Validator, error::SocketError, protocol::websocket::WsMessage};
10use serde::{Deserialize, Serialize, de::DeserializeOwned};
11use std::{fmt::Debug, time::Duration};
12use url::Url;
13
14/// `BinanceSpot` & `BinanceFuturesUsd` [`Connector`] and [`StreamSelector`] implementations.
15pub mod binance;
16
17/// `Bitfinex` [`Connector`] and [`StreamSelector`] implementations.
18pub mod bitfinex;
19
20/// `Bitmex [`Connector`] and [`StreamSelector`] implementations.
21pub mod bitmex;
22
23/// `Bybit` ['Connector'] and ['StreamSelector'] implementation
24pub mod bybit;
25
26/// `Coinbase` [`Connector`] and [`StreamSelector`] implementations.
27pub mod coinbase;
28
29/// `GateioSpot`, `GateioFuturesUsd` & `GateioFuturesBtc` [`Connector`] and [`StreamSelector`]
30/// implementations.
31pub mod gateio;
32
33/// `Kraken` [`Connector`] and [`StreamSelector`] implementations.
34pub mod kraken;
35
36/// `Okx` [`Connector`] and [`StreamSelector`] implementations.
37pub mod okx;
38
39/// `Hyperliquid` and `HyperliquidSpot` `Connector` and `StreamSelector` implementations (behind `hyperliquid` feature).
40#[cfg(feature = "hyperliquid")]
41pub mod hyperliquid;
42
43/// `Ibkr` market data stream (behind `ibkr` feature).
44#[cfg(feature = "ibkr")]
45pub mod ibkr;
46
47/// `Alpaca` market data stream (behind `alpaca` feature).
48#[cfg(feature = "alpaca")]
49pub mod alpaca;
50
51/// `Databento` historical and live market data (behind `databento` feature).
52#[cfg(feature = "databento")]
53pub mod databento;
54
55/// `Massive` (formerly Polygon.io) market data (behind `massive` feature).
56#[cfg(feature = "massive")]
57pub mod massive;
58
59/// Defines the generic [`ExchangeSub`] containing a market and channel combination used by an
60/// exchange [`Connector`] to build [`WsMessage`] subscription payloads.
61pub mod subscription;
62
63/// Default [`Duration`] the [`Connector::SubValidator`] will wait to receive all success responses to actioned
64/// `Subscription` requests.
65pub const DEFAULT_SUBSCRIPTION_TIMEOUT: Duration = Duration::from_secs(10);
66
67/// Defines the [`MarketStream`] kind associated with an exchange
68/// `Subscription` [`SubscriptionKind`].
69///
70/// ### Notes
71/// Must be implemented by an exchange [`Connector`] if it supports a specific
72/// [`SubscriptionKind`].
73pub trait StreamSelector<Instrument, Kind>
74where
75 Self: Connector,
76 Instrument: InstrumentData,
77 Kind: SubscriptionKind,
78{
79 type SnapFetcher: SnapshotFetcher<Self, Kind>;
80 type Stream: MarketStream<Self, Instrument, Kind>;
81}
82
83/// Primary exchange abstraction. Defines how to translate Barter types into exchange specific
84/// types, as well as connecting, subscribing, and interacting with the exchange server.
85///
86/// ### Notes
87/// This must be implemented for a new exchange integration!
88pub trait Connector
89where
90 Self: Clone + Default + Debug + for<'de> Deserialize<'de> + Serialize + Sized,
91{
92 /// Unique identifier for the exchange server being connected with.
93 const ID: ExchangeId;
94
95 /// Type that defines how to translate a Barter `Subscription` into an exchange specific
96 /// channel to be subscribed to.
97 ///
98 /// ### Examples
99 /// - [`BinanceChannel("@depth@100ms")`](binance::channel::BinanceChannel)
100 /// - [`KrakenChannel("trade")`](kraken::channel::KrakenChannel)
101 type Channel: AsRef<str>;
102
103 /// Type that defines how to translate a Barter
104 /// `Subscription` into an exchange specific market that
105 /// can be subscribed to.
106 ///
107 /// ### Examples
108 /// - [`BinanceMarket("btcusdt")`](binance::market::BinanceMarket)
109 /// - [`KrakenMarket("BTC/USDT")`](kraken::market::KrakenMarket)
110 type Market: AsRef<str>;
111
112 /// [`Subscriber`] type that establishes a connection with the exchange server, and actions
113 /// `Subscription`s over the socket.
114 type Subscriber: Subscriber;
115
116 /// [`SubscriptionValidator`] type that listens to responses from the exchange server and
117 /// validates if the actioned `Subscription`s were
118 /// successful.
119 type SubValidator: SubscriptionValidator;
120
121 /// Deserialisable type that the [`Self::SubValidator`] expects to receive from the exchange server in
122 /// response to the `Subscription` [`Self::requests`]
123 /// sent over the [`WebSocket`](rustrade_integration::protocol::websocket::WebSocket). Implements
124 /// [`Validator`] in order to determine if [`Self`]
125 /// communicates a successful `Subscription` outcome.
126 type SubResponse: Validator<Error = SocketError> + Debug + DeserializeOwned;
127
128 /// Base [`Url`] of the exchange server being connected with.
129 fn url() -> Result<Url, url::ParseError>;
130
131 /// Defines [`PingInterval`] of custom application-level
132 /// [`WebSocket`](rustrade_integration::protocol::websocket::WebSocket) pings for the exchange
133 /// server being connected with.
134 ///
135 /// Defaults to `None`, meaning that no custom pings are sent.
136 fn ping_interval() -> Option<PingInterval> {
137 None
138 }
139
140 /// Defines how to translate a collection of [`ExchangeSub`]s into the [`WsMessage`]
141 /// subscription payloads sent to the exchange server.
142 fn requests(exchange_subs: Vec<ExchangeSub<Self::Channel, Self::Market>>) -> Vec<WsMessage>;
143
144 /// Number of `Subscription` responses expected from the
145 /// exchange server in responses to the requests send. Used to validate all
146 /// `Subscription`s were accepted.
147 fn expected_responses<InstrumentKey>(map: &Map<InstrumentKey>) -> usize {
148 map.0.len()
149 }
150
151 /// Expected [`Duration`] the [`SubscriptionValidator`] will wait to receive all success
152 /// responses to actioned `Subscription` requests.
153 fn subscription_timeout() -> Duration {
154 DEFAULT_SUBSCRIPTION_TIMEOUT
155 }
156}
157
158/// Used when an exchange has servers different
159/// [`InstrumentKind`](rustrade_instrument::instrument::kind::InstrumentKind) market data on distinct servers,
160/// allowing all the [`Connector`] logic to be identical apart from what this trait provides.
161///
162/// ### Examples
163/// - [`BinanceServerSpot`](binance::spot::BinanceServerSpot)
164/// - [`BinanceServerFuturesUsd`](binance::futures::BinanceServerFuturesUsd)
165pub trait ExchangeServer: Default + Debug + Clone + Send {
166 const ID: ExchangeId;
167 fn websocket_url() -> &'static str;
168}
169
170/// Defines the frequency and construction function for custom
171/// [`WebSocket`](rustrade_integration::protocol::websocket::WebSocket) pings - used for exchanges
172/// that require additional application-level pings.
173#[derive(Debug)]
174pub struct PingInterval {
175 pub interval: tokio::time::Interval,
176 pub ping: fn() -> WsMessage,
177}