nautilus-binance 0.55.0

Binance exchange integration adapter for the Nautilus trading engine
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Binance Futures WebSocket handler for JSON streams.
//!
//! The handler is a stateless I/O boundary: it deserializes raw JSON into
//! venue-specific types and emits them on the output channel. Domain conversion
//! happens in the data and execution client layers.

use std::{
    fmt::Debug,
    sync::{
        Arc,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
};

use ahash::AHashMap;
use nautilus_network::{
    RECONNECTED,
    websocket::{SubscriptionState, WebSocketClient},
};

use super::{
    messages::{
        BinanceFuturesAccountConfigMsg, BinanceFuturesAccountUpdateMsg, BinanceFuturesAggTradeMsg,
        BinanceFuturesAlgoUpdateMsg, BinanceFuturesBookTickerMsg, BinanceFuturesDepthUpdateMsg,
        BinanceFuturesKlineMsg, BinanceFuturesLiquidationMsg, BinanceFuturesListenKeyExpiredMsg,
        BinanceFuturesMarginCallMsg, BinanceFuturesMarkPriceMsg, BinanceFuturesOrderUpdateMsg,
        BinanceFuturesTickerMsg, BinanceFuturesTradeMsg, BinanceFuturesWsErrorMsg,
        BinanceFuturesWsErrorResponse, BinanceFuturesWsStreamsCommand,
        BinanceFuturesWsStreamsMessage, BinanceFuturesWsSubscribeRequest,
        BinanceFuturesWsSubscribeResponse,
    },
    parse_data::extract_event_type,
};
use crate::common::{
    consts::BINANCE_RATE_LIMIT_KEY_SUBSCRIPTION,
    enums::{BinanceWsEventType, BinanceWsMethod},
};

/// Handler for Binance Futures WebSocket JSON streams.
///
/// Deserializes raw JSON into venue-specific types without performing
/// domain conversion. The data and execution client layers own instrument
/// lookups and Nautilus type construction.
pub struct BinanceFuturesDataWsFeedHandler {
    #[allow(dead_code)]
    signal: Arc<AtomicBool>,
    cmd_rx: tokio::sync::mpsc::UnboundedReceiver<BinanceFuturesWsStreamsCommand>,
    raw_rx: tokio::sync::mpsc::UnboundedReceiver<Vec<u8>>,
    #[allow(dead_code)]
    out_tx: tokio::sync::mpsc::UnboundedSender<BinanceFuturesWsStreamsMessage>,
    inner: Option<WebSocketClient>,
    subscriptions_state: SubscriptionState,
    request_id_counter: Arc<AtomicU64>,
    pending_requests: AHashMap<u64, Vec<String>>,
}

impl Debug for BinanceFuturesDataWsFeedHandler {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(BinanceFuturesDataWsFeedHandler))
            .field("pending_requests", &self.pending_requests.len())
            .finish_non_exhaustive()
    }
}

impl BinanceFuturesDataWsFeedHandler {
    /// Creates a new handler instance.
    pub fn new(
        signal: Arc<AtomicBool>,
        cmd_rx: tokio::sync::mpsc::UnboundedReceiver<BinanceFuturesWsStreamsCommand>,
        raw_rx: tokio::sync::mpsc::UnboundedReceiver<Vec<u8>>,
        out_tx: tokio::sync::mpsc::UnboundedSender<BinanceFuturesWsStreamsMessage>,
        subscriptions_state: SubscriptionState,
        request_id_counter: Arc<AtomicU64>,
    ) -> Self {
        Self {
            signal,
            cmd_rx,
            raw_rx,
            out_tx,
            inner: None,
            subscriptions_state,
            request_id_counter,
            pending_requests: AHashMap::new(),
        }
    }

    /// Returns the next message from the handler.
    ///
    /// Processes both commands and raw WebSocket messages.
    pub async fn next(&mut self) -> Option<BinanceFuturesWsStreamsMessage> {
        loop {
            if self.signal.load(Ordering::Relaxed) {
                return None;
            }

            tokio::select! {
                Some(cmd) = self.cmd_rx.recv() => {
                    self.handle_command(cmd).await;
                }
                Some(raw) = self.raw_rx.recv() => {
                    if let Some(msg) = self.handle_raw_message(raw).await {
                        return Some(msg);
                    }
                }
                else => {
                    return None;
                }
            }
        }
    }

    async fn handle_command(&mut self, cmd: BinanceFuturesWsStreamsCommand) {
        match cmd {
            BinanceFuturesWsStreamsCommand::SetClient(client) => {
                self.inner = Some(client);
            }
            BinanceFuturesWsStreamsCommand::Disconnect => {
                if let Some(client) = &self.inner {
                    let () = client.disconnect().await;
                }
                self.inner = None;
            }
            BinanceFuturesWsStreamsCommand::Subscribe { streams } => {
                self.send_subscribe(streams).await;
            }
            BinanceFuturesWsStreamsCommand::Unsubscribe { streams } => {
                self.send_unsubscribe(streams).await;
            }
        }
    }

    async fn send_subscribe(&mut self, streams: Vec<String>) {
        let Some(client) = &self.inner else {
            log::warn!("Cannot subscribe: no client connected");
            return;
        };

        let request_id = self.request_id_counter.fetch_add(1, Ordering::Relaxed);

        self.pending_requests.insert(request_id, streams.clone());

        for stream in &streams {
            self.subscriptions_state.mark_subscribe(stream);
        }

        let request = BinanceFuturesWsSubscribeRequest {
            method: BinanceWsMethod::Subscribe,
            params: streams,
            id: request_id,
        };

        let json = match serde_json::to_string(&request) {
            Ok(j) => j,
            Err(e) => {
                log::error!("Failed to serialize subscribe request: {e}");
                return;
            }
        };

        if let Err(e) = client
            .send_text(json, Some(BINANCE_RATE_LIMIT_KEY_SUBSCRIPTION.as_slice()))
            .await
        {
            log::error!("Failed to send subscribe request: {e}");
        }
    }

    async fn send_unsubscribe(&self, streams: Vec<String>) {
        let Some(client) = &self.inner else {
            log::warn!("Cannot unsubscribe: no client connected");
            return;
        };

        let request_id = self.request_id_counter.fetch_add(1, Ordering::Relaxed);

        let request = BinanceFuturesWsSubscribeRequest {
            method: BinanceWsMethod::Unsubscribe,
            params: streams.clone(),
            id: request_id,
        };

        let json = match serde_json::to_string(&request) {
            Ok(j) => j,
            Err(e) => {
                log::error!("Failed to serialize unsubscribe request: {e}");
                return;
            }
        };

        if let Err(e) = client
            .send_text(json, Some(BINANCE_RATE_LIMIT_KEY_SUBSCRIPTION.as_slice()))
            .await
        {
            log::error!("Failed to send unsubscribe request: {e}");
        }

        for stream in &streams {
            self.subscriptions_state.mark_unsubscribe(stream);
            self.subscriptions_state.confirm_unsubscribe(stream);
        }
    }

    async fn handle_raw_message(&mut self, raw: Vec<u8>) -> Option<BinanceFuturesWsStreamsMessage> {
        if let Ok(text) = std::str::from_utf8(&raw)
            && text == RECONNECTED
        {
            log::info!("WebSocket reconnected signal received");
            return Some(BinanceFuturesWsStreamsMessage::Reconnected);
        }

        let json: serde_json::Value = match serde_json::from_slice(&raw) {
            Ok(j) => j,
            Err(e) => {
                log::warn!("Failed to parse JSON message: {e}");
                return None;
            }
        };

        if json.get("result").is_some() || json.get("id").is_some() {
            self.handle_subscription_response(&json);
            return None;
        }

        if let Some(code) = json.get("code")
            && let Some(code) = code.as_i64()
        {
            let msg = json
                .get("msg")
                .and_then(|m| m.as_str())
                .unwrap_or("Unknown error")
                .to_string();
            return Some(BinanceFuturesWsStreamsMessage::Error(
                BinanceFuturesWsErrorMsg { code, msg },
            ));
        }

        self.handle_stream_data(&json)
    }

    fn handle_subscription_response(&mut self, json: &serde_json::Value) {
        if let Ok(response) =
            serde_json::from_value::<BinanceFuturesWsSubscribeResponse>(json.clone())
        {
            if let Some(streams) = self.pending_requests.remove(&response.id) {
                if response.result.is_none() {
                    for stream in &streams {
                        self.subscriptions_state.confirm_subscribe(stream);
                    }
                    log::debug!("Subscription confirmed: streams={streams:?}");
                } else {
                    for stream in &streams {
                        self.subscriptions_state.mark_failure(stream);
                    }
                    log::warn!(
                        "Subscription failed: streams={streams:?}, result={:?}",
                        response.result
                    );
                }
            }
        } else if let Ok(error) =
            serde_json::from_value::<BinanceFuturesWsErrorResponse>(json.clone())
        {
            if let Some(id) = error.id
                && let Some(streams) = self.pending_requests.remove(&id)
            {
                for stream in &streams {
                    self.subscriptions_state.mark_failure(stream);
                }
            }
            log::warn!(
                "WebSocket error response: code={}, msg={}",
                error.code,
                error.msg
            );
        }
    }

    fn handle_stream_data(
        &self,
        json: &serde_json::Value,
    ) -> Option<BinanceFuturesWsStreamsMessage> {
        let event_type = extract_event_type(json)?;

        match event_type {
            BinanceWsEventType::AggTrade => {
                serde_json::from_value::<BinanceFuturesAggTradeMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::AggTrade)
                    .map_err(|e| log::warn!("Failed to parse aggregate trade: {e}"))
                    .ok()
            }
            BinanceWsEventType::Trade => {
                serde_json::from_value::<BinanceFuturesTradeMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::Trade)
                    .map_err(|e| log::warn!("Failed to parse trade: {e}"))
                    .ok()
            }
            BinanceWsEventType::BookTicker => {
                serde_json::from_value::<BinanceFuturesBookTickerMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::BookTicker)
                    .map_err(|e| log::warn!("Failed to parse book ticker: {e}"))
                    .ok()
            }
            BinanceWsEventType::DepthUpdate => {
                serde_json::from_value::<BinanceFuturesDepthUpdateMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::DepthUpdate)
                    .map_err(|e| log::warn!("Failed to parse depth update: {e}"))
                    .ok()
            }
            BinanceWsEventType::MarkPriceUpdate => {
                serde_json::from_value::<BinanceFuturesMarkPriceMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::MarkPrice)
                    .map_err(|e| log::warn!("Failed to parse mark price: {e}"))
                    .ok()
            }
            BinanceWsEventType::Kline => {
                serde_json::from_value::<BinanceFuturesKlineMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::Kline)
                    .map_err(|e| log::warn!("Failed to parse kline: {e}"))
                    .ok()
            }
            BinanceWsEventType::ForceOrder => {
                serde_json::from_value::<BinanceFuturesLiquidationMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::ForceOrder)
                    .map_err(|e| log::warn!("Failed to parse force order: {e}"))
                    .ok()
            }
            BinanceWsEventType::Ticker24Hr => {
                serde_json::from_value::<BinanceFuturesTickerMsg>(json.clone())
                    .map(BinanceFuturesWsStreamsMessage::Ticker)
                    .map_err(|e| log::warn!("Failed to parse ticker: {e}"))
                    .ok()
            }
            BinanceWsEventType::MiniTicker24Hr => {
                log::debug!("Mini ticker not yet supported, skipping");
                None
            }
            BinanceWsEventType::AccountUpdate => {
                serde_json::from_value::<BinanceFuturesAccountUpdateMsg>(json.clone())
                    .map(|msg| {
                        log::debug!(
                            "Account update: reason={:?}, balances={}, positions={}",
                            msg.account.reason,
                            msg.account.balances.len(),
                            msg.account.positions.len()
                        );
                        BinanceFuturesWsStreamsMessage::AccountUpdate(msg)
                    })
                    .map_err(|e| log::warn!("Failed to parse account update: {e}"))
                    .ok()
            }
            BinanceWsEventType::OrderTradeUpdate => {
                serde_json::from_value::<BinanceFuturesOrderUpdateMsg>(json.clone())
                    .map(|msg| {
                        log::debug!(
                            "Order update: symbol={}, order_id={}, exec={:?}, status={:?}",
                            msg.order.symbol,
                            msg.order.order_id,
                            msg.order.execution_type,
                            msg.order.order_status
                        );
                        BinanceFuturesWsStreamsMessage::OrderUpdate(Box::new(msg))
                    })
                    .map_err(|e| log::warn!("Failed to parse order update: {e}"))
                    .ok()
            }
            BinanceWsEventType::AlgoUpdate => {
                serde_json::from_value::<BinanceFuturesAlgoUpdateMsg>(json.clone())
                    .map(|msg| {
                        log::debug!(
                            "Algo order update: symbol={}, algo_id={}, status={:?}",
                            msg.algo_order.symbol,
                            msg.algo_order.algo_id,
                            msg.algo_order.algo_status
                        );
                        BinanceFuturesWsStreamsMessage::AlgoUpdate(Box::new(msg))
                    })
                    .map_err(|e| log::warn!("Failed to parse algo order update: {e}"))
                    .ok()
            }
            BinanceWsEventType::MarginCall => {
                serde_json::from_value::<BinanceFuturesMarginCallMsg>(json.clone())
                    .map(|msg| {
                        log::warn!(
                            "Margin call: cross_wallet_balance={}, positions_at_risk={}",
                            msg.cross_wallet_balance,
                            msg.positions.len()
                        );
                        BinanceFuturesWsStreamsMessage::MarginCall(msg)
                    })
                    .map_err(|e| log::warn!("Failed to parse margin call: {e}"))
                    .ok()
            }
            BinanceWsEventType::AccountConfigUpdate => {
                serde_json::from_value::<BinanceFuturesAccountConfigMsg>(json.clone())
                    .map(|msg| {
                        if let Some(ref lc) = msg.leverage_config {
                            log::debug!(
                                "Account config update: symbol={}, leverage={}",
                                lc.symbol,
                                lc.leverage
                            );
                        }
                        BinanceFuturesWsStreamsMessage::AccountConfigUpdate(msg)
                    })
                    .map_err(|e| log::warn!("Failed to parse account config update: {e}"))
                    .ok()
            }
            BinanceWsEventType::ListenKeyExpired => {
                if let Ok(msg) =
                    serde_json::from_value::<BinanceFuturesListenKeyExpiredMsg>(json.clone())
                {
                    log::warn!("Listen key expired at {}", msg.event_time);
                }
                Some(BinanceFuturesWsStreamsMessage::ListenKeyExpired)
            }
            BinanceWsEventType::Unknown => {
                log::warn!("Unknown event type in message: {json}");
                None
            }
        }
    }
}