bitmex_ws_exec/
ws_exec.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16// Under development
17#![allow(dead_code)]
18#![allow(unused_variables)]
19
20use futures_util::StreamExt;
21use nautilus_bitmex::{http::client::BitmexHttpClient, websocket::client::BitmexWebSocketClient};
22use tokio::time::Duration;
23use tracing::level_filters::LevelFilter;
24
25#[tokio::main]
26async fn main() -> Result<(), Box<dyn std::error::Error>> {
27    tracing_subscriber::fmt()
28        .with_max_level(LevelFilter::TRACE)
29        .init();
30
31    tracing::info!("Fetching instruments from HTTP API...");
32    let http_client = BitmexHttpClient::new(
33        None,     // base_url: defaults to production
34        None,     // api_key
35        None,     // api_secret
36        false,    // testnet
37        Some(60), // timeout_secs
38        None,     // max_retries
39        None,     // retry_delay_ms
40        None,     // retry_delay_max_ms
41        None,     // recv_window_ms
42        None,     // max_requests_per_second
43        None,     // max_requests_per_minute
44        None,     // proxy_url
45    )
46    .expect("Failed to create HTTP client");
47
48    let instruments = http_client
49        .request_instruments(true) // active_only
50        .await?;
51
52    tracing::info!("Fetched {} instruments", instruments.len());
53
54    let mut ws_client = BitmexWebSocketClient::new(
55        None, // url: defaults to wss://ws.bitmex.com/realtime
56        None,
57        None,
58        None,
59        Some(5), // 5 second heartbeat
60    )
61    .unwrap();
62    ws_client.cache_instruments(instruments);
63    ws_client.connect().await?;
64
65    // Give the connection a moment to stabilize
66    tokio::time::sleep(Duration::from_millis(500)).await;
67
68    // Subscribe for all execution related topics
69    ws_client
70        .subscribe(vec![
71            "execution".to_string(),
72            "order".to_string(),
73            "margin".to_string(),
74            "position".to_string(),
75            "wallet".to_string(),
76        ])
77        .await?;
78
79    // Create a future that completes on CTRL+C
80    let sigint = tokio::signal::ctrl_c();
81    tokio::pin!(sigint);
82
83    let stream = ws_client.stream();
84    tokio::pin!(stream); // Pin the stream to allow polling in the loop
85
86    loop {
87        tokio::select! {
88            Some(event) = stream.next() => {
89                tracing::debug!("{event:?}");
90            }
91            _ = &mut sigint => {
92                tracing::info!("Received SIGINT, closing connection...");
93                ws_client.close().await?;
94                break;
95            }
96            else => break,
97        }
98    }
99
100    Ok(())
101}