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