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 )
42 .expect("Failed to create HTTP client");
43
44 let instruments = http_client
45 .request_instruments(true) // active_only
46 .await?;
47
48 tracing::info!("Fetched {} instruments", instruments.len());
49
50 let mut ws_client = BitmexWebSocketClient::new(
51 None, // url: defaults to wss://ws.bitmex.com/realtime
52 None,
53 None,
54 None,
55 Some(5), // 5 second heartbeat
56 )
57 .unwrap();
58 ws_client.initialize_instruments_cache(instruments);
59 ws_client.connect().await?;
60
61 // Give the connection a moment to stabilize
62 tokio::time::sleep(Duration::from_millis(500)).await;
63
64 // Subscribe for all execution related topics
65 ws_client
66 .subscribe(vec![
67 "execution".to_string(),
68 "order".to_string(),
69 "margin".to_string(),
70 "position".to_string(),
71 "wallet".to_string(),
72 ])
73 .await?;
74
75 // Create a future that completes on CTRL+C
76 let sigint = tokio::signal::ctrl_c();
77 tokio::pin!(sigint);
78
79 let stream = ws_client.stream();
80 tokio::pin!(stream); // Pin the stream to allow polling in the loop
81
82 loop {
83 tokio::select! {
84 Some(event) = stream.next() => {
85 tracing::debug!("{event:?}");
86 }
87 _ = &mut sigint => {
88 tracing::info!("Received SIGINT, closing connection...");
89 ws_client.close().await?;
90 break;
91 }
92 else => break,
93 }
94 }
95
96 Ok(())
97}