rustrade-data 0.3.0

High performance & normalised WebSocket intergration for leading cryptocurrency exchanges - batteries included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Live Binance kline (candle) WebSocket payloads and their normalisation to
//! [`Candle`](crate::subscription::candle::Candle).
//!
//! Covers both the [`BinanceSpot`](crate::exchange::binance::spot::BinanceSpot) `@kline_<interval>`
//! stream and the
//! [`BinanceFuturesUsdMarket`](crate::exchange::binance::futures::BinanceFuturesUsdMarket)
//! `@continuousKline_<interval>` stream (perpetual-only). Endpoints are public/unauthenticated.
//!
//! # Closed candles only (no repaint)
//!
//! rustrade emits **closed candles only** — an in-progress kline (`k.x == false`) yields an empty
//! [`MarketIter`](crate::event::MarketIter), so consumers never see a repainting/lookahead value.
//! The exclusive `close_time` boundary is recomputed library-side as `open + interval` (see
//! [`close_time_from_open`](crate::subscription::candle::close_time_from_open)), **not** taken from
//! Binance's wire `T` (its `period-end − 1ms` convention) — consumers comparing against the raw `T`
//! will see a 1ms difference by design.
//!
//! # Reconnection: no replay, no dedup (consumer policy)
//!
//! Across a reconnect the underlying [`Connector`](crate::exchange::Connector) /
//! `ReconnectingStream` re-subscribes but does **not** replay or de-duplicate: a closed candle
//! straddling the disconnect may be **re-delivered or skipped**. De-duplication and gap-back-fill
//! are **consumer policy, not library policy** (consistent with rustrade's "no consumer-specific
//! policy in the library" rule). A consumer wanting a gapless series should reconcile the live
//! candle stream against a
//! [`fetch_candles`](crate::exchange::binance::historical::BinanceHistoricalClient::fetch_candles)
//! backfill keyed on `close_time` (the field both paths agree on, since `open ≡ close − interval`).

use super::BinanceChannel;
use crate::{
    Identifier,
    error::DataError,
    event::{MarketEvent, MarketIter},
    subscription::candle::{Candle, CandleInterval, close_time_from_open},
};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rustrade_instrument::exchange::ExchangeId;
use rustrade_integration::subscription::SubscriptionId;
use serde::{Deserialize, Serialize};
use smol_str::{SmolStr, format_smolstr};

/// The inner `k` payload shared by the [`BinanceSpot`](super::spot::BinanceSpot) `@kline_`
/// and [`BinanceFuturesUsdMarket`](super::futures::BinanceFuturesUsdMarket) `@continuousKline_` streams.
///
/// Both surfaces carry an identical `k` object for the fields rustrade consumes; they differ
/// only in where the market symbol lives (spot top-level `s` vs futures top-level `ps`) and
/// in the continuous payload omitting `k.s`. This struct deliberately omits the symbol so it
/// can be reused by both — the symbol is captured by the outer model and used to build the
/// [`SubscriptionId`].
///
/// ### Correctness notes
/// - OHLCV are JSON **strings** on the wire (e.g. `"0.01634790"`) → parsed via `de_str` to
///   [`Decimal`], never through an `f64` (which silently truncates precision).
/// - `open_time` (`t`) is the candle's open instant; the exclusive `close_time` boundary is
///   recomputed library-side via [`close_time_from_open`] (`open + interval`), **not** taken
///   from the wire `T` (Binance's `period-end − 1ms` convention).
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct BinanceKlineData {
    #[serde(
        alias = "t",
        deserialize_with = "rustrade_integration::serde::de::de_u64_epoch_ms_as_datetime_utc"
    )]
    pub open_time: DateTime<Utc>,
    #[serde(alias = "i")]
    pub interval: CandleInterval,
    #[serde(
        alias = "o",
        deserialize_with = "rustrade_integration::serde::de::de_str"
    )]
    pub open: Decimal,
    #[serde(
        alias = "h",
        deserialize_with = "rustrade_integration::serde::de::de_str"
    )]
    pub high: Decimal,
    #[serde(
        alias = "l",
        deserialize_with = "rustrade_integration::serde::de::de_str"
    )]
    pub low: Decimal,
    #[serde(
        alias = "c",
        deserialize_with = "rustrade_integration::serde::de::de_str"
    )]
    pub close: Decimal,
    #[serde(
        alias = "v",
        deserialize_with = "rustrade_integration::serde::de::de_str"
    )]
    pub volume: Decimal,
    #[serde(alias = "n")]
    pub trade_count: u64,
    /// `true` once the kline interval has closed. rustrade emits **closed candles only**
    /// (no repaint/lookahead) — an in-progress kline yields an empty [`MarketIter`].
    #[serde(alias = "x")]
    pub closed: bool,
}

impl BinanceKlineData {
    /// Map a kline payload to a normalised [`Candle`] [`MarketEvent`], honouring the
    /// closed-only policy and the `close_time = open + interval` boundary contract.
    ///
    /// - In-progress klines (`x == false`) yield an empty [`MarketIter`] (no event).
    /// - A closed kline whose computed `close_time` overflows the representable
    ///   [`DateTime<Utc>`] range yields an observable [`DataError`] rather than a silent
    ///   drop or a plausible-but-wrong timestamp (unreachable for real intervals, but the
    ///   boundary contract forbids a silent fallback).
    fn into_market_iter<InstrumentKey>(
        self,
        exchange_id: ExchangeId,
        instrument: InstrumentKey,
    ) -> MarketIter<InstrumentKey, Candle> {
        if !self.closed {
            return MarketIter(vec![]);
        }

        match close_time_from_open(self.open_time, self.interval.to_step()) {
            Some(close_time) => MarketIter(vec![Ok(MarketEvent {
                time_exchange: close_time,
                time_received: Utc::now(),
                exchange: exchange_id,
                instrument,
                kind: Candle {
                    close_time,
                    open: self.open,
                    high: self.high,
                    low: self.low,
                    close: self.close,
                    volume: self.volume,
                    trade_count: self.trade_count,
                },
            })]),
            None => MarketIter(vec![Err(DataError::Socket(format!(
                "Binance candle close_time overflow: open_time {} + interval {} exceeds the representable DateTime<Utc> range",
                self.open_time, self.interval
            )))]),
        }
    }
}

/// [`BinanceSpot`](super::spot::BinanceSpot) real-time kline (candle) message.
///
/// ### Raw Payload Example
/// See docs: <https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-streams>
/// ```json
/// {
///     "e": "kline",
///     "E": 1638747660000,
///     "s": "BTCUSDT",
///     "k": {
///         "t": 1638747660000, "T": 1638747719999, "s": "BTCUSDT", "i": "1m",
///         "f": 100, "L": 200, "o": "0.0010", "c": "0.0020", "h": "0.0025",
///         "l": "0.0015", "v": "1000", "n": 100, "x": false, "q": "1.0000",
///         "V": "500", "Q": "0.500", "B": "123456"
///     }
/// }
/// ```
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct BinanceKline {
    /// Top-level market symbol, UPPERCASE (e.g. `BTCUSDT`).
    #[serde(alias = "s")]
    pub symbol: SmolStr,
    #[serde(alias = "k")]
    pub kline: BinanceKlineData,
}

impl Identifier<Option<SubscriptionId>> for BinanceKline {
    /// Builds the instrument-map key `{channel}|{MARKET}` (e.g. `@kline_1m|BTCUSDT`) from the
    /// payload's interval (`k.i`) and UPPERCASE symbol (`s`) — matching
    /// [`ExchangeSub::id`](crate::exchange::ExchangeSub), **not** the lowercase stream name.
    fn id(&self) -> Option<SubscriptionId> {
        Some(SubscriptionId(format_smolstr!(
            "{}|{}",
            BinanceChannel::spot_candle(self.kline.interval).0,
            self.symbol
        )))
    }
}

impl<InstrumentKey> From<(ExchangeId, InstrumentKey, BinanceKline)>
    for MarketIter<InstrumentKey, Candle>
{
    fn from((exchange_id, instrument, kline): (ExchangeId, InstrumentKey, BinanceKline)) -> Self {
        kline.kline.into_market_iter(exchange_id, instrument)
    }
}

/// [`BinanceFuturesUsdMarket`](super::futures::BinanceFuturesUsdMarket) real-time continuous-contract
/// (perpetual) kline message.
///
/// Differs from the spot [`BinanceKline`] shape: the symbol is the top-level `ps` (pair) and
/// `ct` (contract type), and the inner `k` object has **no** `s` field.
///
/// ### Raw Payload Example
/// See docs: <https://binance-docs.github.io/apidocs/futures/en/#continuous-contract-kline-candlestick-streams>
/// ```json
/// {
///     "e": "continuous_kline",
///     "E": 1607443058651,
///     "ps": "BTCUSDT",
///     "ct": "PERPETUAL",
///     "k": {
///         "t": 1607443020000, "T": 1607443079999, "i": "1m", "f": 116467658886,
///         "L": 116468012423, "o": "18787.00", "c": "18804.04", "h": "18804.04",
///         "l": "18786.54", "v": "197.664", "n": 543, "x": false, "q": "3715253.19494",
///         "V": "184.769", "Q": "3472925.84746", "B": "0"
///     }
/// }
/// ```
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct BinanceContinuousKline {
    /// Top-level pair symbol, UPPERCASE (e.g. `BTCUSDT`). For a perpetual-only venue the pair
    /// equals the symbol.
    #[serde(alias = "ps")]
    pub pair: SmolStr,
    // The wire `ct` (contract type, e.g. `"PERPETUAL"`) is intentionally not deserialized: the
    // `_perpetual@continuousKline_` subscription this model decodes only ever receives PERPETUAL
    // frames, and `id()` hardcodes the perpetual channel prefix to match. A non-perpetual frame
    // (impossible given the subscription) would build a non-matching `SubscriptionId` and be
    // dropped at the instrument-map lookup rather than mis-attributed.
    #[serde(alias = "k")]
    pub kline: BinanceKlineData,
}

impl Identifier<Option<SubscriptionId>> for BinanceContinuousKline {
    /// Builds the instrument-map key `{channel}|{MARKET}` (e.g.
    /// `_perpetual@continuousKline_1m|BTCUSDT`) from the payload's interval (`k.i`) and
    /// UPPERCASE pair (`ps`) — matching [`ExchangeSub::id`](crate::exchange::ExchangeSub). The
    /// continuous payload has no `k.s`, so the pair is the only symbol source.
    fn id(&self) -> Option<SubscriptionId> {
        Some(SubscriptionId(format_smolstr!(
            "{}|{}",
            BinanceChannel::futures_candle(self.kline.interval).0,
            self.pair
        )))
    }
}

impl<InstrumentKey> From<(ExchangeId, InstrumentKey, BinanceContinuousKline)>
    for MarketIter<InstrumentKey, Candle>
{
    fn from(
        (exchange_id, instrument, kline): (ExchangeId, InstrumentKey, BinanceContinuousKline),
    ) -> Self {
        kline.kline.into_market_iter(exchange_id, instrument)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test code: panics on bad input are acceptable
mod tests {
    use super::*;
    use chrono::TimeZone;
    use rust_decimal_macros::dec;

    /// Spot `@kline_1m` frame for a closed candle.
    const SPOT_CLOSED: &str = r#"
    {
        "e": "kline", "E": 1638747660000, "s": "BTCUSDT",
        "k": {
            "t": 1638747660000, "T": 1638747719999, "s": "BTCUSDT", "i": "1m",
            "f": 100, "L": 200, "o": "0.0010", "c": "0.0020", "h": "0.0025",
            "l": "0.0015", "v": "1000", "n": 100, "x": true, "q": "1.0000",
            "V": "500", "Q": "0.500", "B": "123456"
        }
    }"#;

    /// Futures `continuousKline_1s` frame for an in-progress candle (note: no `k.s`).
    const FUTURES_OPEN: &str = r#"
    {
        "e": "continuous_kline", "E": 1607443058651, "ps": "BTCUSDT", "ct": "PERPETUAL",
        "k": {
            "t": 1607443020000, "T": 1607443079999, "i": "1s",
            "f": 116467658886, "L": 116468012423, "o": "18787.00", "c": "18804.04",
            "h": "18804.04", "l": "18786.54", "v": "197.664", "n": 543, "x": false,
            "q": "3715253.19494", "V": "184.769", "Q": "3472925.84746", "B": "0"
        }
    }"#;

    /// Futures `continuousKline_1m` frame for a **closed** candle (note: no `k.s`; the symbol
    /// source is the top-level `ps` pair).
    const FUTURES_CLOSED: &str = r#"
    {
        "e": "continuous_kline", "E": 1607443079999, "ps": "BTCUSDT", "ct": "PERPETUAL",
        "k": {
            "t": 1607443020000, "T": 1607443079999, "i": "1m",
            "f": 116467658886, "L": 116468012423, "o": "18787.00", "c": "18804.04",
            "h": "18810.00", "l": "18786.54", "v": "197.664", "n": 543, "x": true,
            "q": "3715253.19494", "V": "184.769", "Q": "3472925.84746", "B": "0"
        }
    }"#;

    #[test]
    fn spot_kline_deserialises_and_builds_map_key() {
        let kline = serde_json::from_str::<BinanceKline>(SPOT_CLOSED).unwrap();
        assert_eq!(kline.symbol, "BTCUSDT");
        assert_eq!(kline.kline.interval, CandleInterval::Min1);
        assert!(kline.kline.closed);
        assert_eq!(kline.kline.open, dec!(0.0010));
        assert_eq!(
            kline.id(),
            Some(SubscriptionId::from("@kline_1m|BTCUSDT")),
            "map key must be {{channel}}|{{MARKET}}, not the lowercase stream name"
        );
    }

    #[test]
    fn futures_continuous_kline_deserialises_without_k_s() {
        let kline = serde_json::from_str::<BinanceContinuousKline>(FUTURES_OPEN).unwrap();
        assert_eq!(kline.pair, "BTCUSDT");
        assert_eq!(kline.kline.interval, CandleInterval::Sec1);
        assert!(!kline.kline.closed);
        assert_eq!(
            kline.id(),
            Some(SubscriptionId::from(
                "_perpetual@continuousKline_1s|BTCUSDT"
            ))
        );
    }

    #[test]
    fn high_precision_ohlcv_round_trips_via_decimal_not_f64() {
        // A value an f64 intermediate would truncate; str→Decimal must preserve it exactly.
        let input = r#"
        {
            "e": "kline", "E": 1, "s": "ETHUSDT",
            "k": { "t": 0, "T": 1, "s": "ETHUSDT", "i": "1m", "o": "0.000000010000000",
                   "c": "0", "h": "0", "l": "0", "v": "0", "n": 0, "x": true }
        }"#;
        let kline = serde_json::from_str::<BinanceKline>(input).unwrap();
        assert_eq!(kline.kline.open, dec!(0.000000010000000));
    }

    #[test]
    fn closed_candle_maps_to_candle_with_boundary_close_time() {
        let kline = serde_json::from_str::<BinanceKline>(SPOT_CLOSED).unwrap();
        let MarketIter(events) =
            MarketIter::<u64, Candle>::from((ExchangeId::BinanceSpot, 1u64, kline));
        assert_eq!(events.len(), 1);
        let event = events.into_iter().next().unwrap().unwrap();
        // close_time = open (1638747660000ms) + 1m = 1638747720000ms — NOT the wire `T`
        // (1638747719999ms, the `period-end − 1ms` convention).
        let expected = Utc.timestamp_millis_opt(1638747720000).unwrap();
        assert_eq!(event.kind.close_time, expected);
        assert_eq!(event.time_exchange, expected);
        assert_eq!(event.kind.trade_count, 100);
    }

    #[test]
    fn in_progress_candle_emits_nothing() {
        let kline = serde_json::from_str::<BinanceContinuousKline>(FUTURES_OPEN).unwrap();
        let MarketIter(events) =
            MarketIter::<u64, Candle>::from((ExchangeId::BinanceFuturesUsd, 1u64, kline));
        assert!(events.is_empty(), "in-progress klines must not emit events");
    }

    #[test]
    fn closed_continuous_kline_maps_with_boundary_close_time_via_ps_pair() {
        // Symmetric to `closed_candle_maps_to_candle_with_boundary_close_time`, but for the
        // futures continuous frame: the symbol comes from `ps` (no `k.s`) and the OHLCV must
        // map through unchanged.
        let kline = serde_json::from_str::<BinanceContinuousKline>(FUTURES_CLOSED).unwrap();
        let MarketIter(events) =
            MarketIter::<u64, Candle>::from((ExchangeId::BinanceFuturesUsd, 7u64, kline));
        assert_eq!(
            events.len(),
            1,
            "a closed continuous kline must emit exactly one candle"
        );
        let event = events.into_iter().next().unwrap().unwrap();
        // close_time = open (1607443020000ms) + 1m = 1607443080000ms — the exclusive boundary,
        // NOT the wire `T` (1607443079999ms).
        let expected = Utc.timestamp_millis_opt(1607443080000).unwrap();
        assert_eq!(event.kind.close_time, expected);
        assert_eq!(event.time_exchange, expected);
        assert_eq!(event.kind.open, dec!(18787.00));
        assert_eq!(event.kind.high, dec!(18810.00));
        assert_eq!(event.kind.low, dec!(18786.54));
        assert_eq!(event.kind.close, dec!(18804.04));
        assert_eq!(event.kind.volume, dec!(197.664));
        assert_eq!(event.kind.trade_count, 543);
    }

    #[test]
    fn zero_volume_candle_is_delivered_not_filtered() {
        // Binance REST gap-fills zero-trade periods (V=0, OHLC=prev_close); the library must
        // not drop them. (WS omits them, but if one arrives it is still delivered.)
        let input = r#"
        {
            "e": "kline", "E": 1, "s": "BTCUSDT",
            "k": { "t": 0, "T": 59999, "s": "BTCUSDT", "i": "1m", "o": "100", "c": "100",
                   "h": "100", "l": "100", "v": "0", "n": 0, "x": true }
        }"#;
        let kline = serde_json::from_str::<BinanceKline>(input).unwrap();
        let MarketIter(events) =
            MarketIter::<u64, Candle>::from((ExchangeId::BinanceSpot, 1u64, kline));
        assert_eq!(events.len(), 1);
        let event = events.into_iter().next().unwrap().unwrap();
        assert_eq!(event.kind.volume, dec!(0));
        assert_eq!(event.kind.trade_count, 0);
    }
}