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 std::time::Duration;
21
22use futures_util::StreamExt;
23use nautilus_bitmex::{http::client::BitmexHttpClient, websocket::client::BitmexWebSocketClient};
24use tracing::level_filters::LevelFilter;
25
26#[tokio::main]
27async fn main() -> Result<(), Box<dyn std::error::Error>> {
28 tracing_subscriber::fmt()
29 .with_max_level(LevelFilter::TRACE)
30 .init();
31
32 tracing::info!("Fetching instruments from HTTP API...");
33 let http_client = BitmexHttpClient::new(
34 None, // base_url: defaults to production
35 None, // api_key
36 None, // api_secret
37 false, // testnet
38 Some(60), // timeout_secs
39 None, // max_retries
40 None, // retry_delay_ms
41 None, // retry_delay_max_ms
42 None, // recv_window_ms
43 None, // max_requests_per_second
44 None, // max_requests_per_minute
45 None, // proxy_url
46 )
47 .expect("Failed to create HTTP client");
48
49 let instruments = http_client
50 .request_instruments(true) // active_only
51 .await?;
52
53 tracing::info!("Fetched {} instruments", instruments.len());
54
55 let mut ws_client = BitmexWebSocketClient::new(
56 None, // url: defaults to wss://ws.bitmex.com/realtime
57 None,
58 None,
59 None,
60 Some(5), // 5 second heartbeat
61 )
62 .unwrap();
63 ws_client.cache_instruments(instruments);
64 ws_client.connect().await?;
65
66 // Give the connection a moment to stabilize
67 tokio::time::sleep(Duration::from_millis(500)).await;
68
69 // Subscribe for all execution related topics
70 ws_client
71 .subscribe(vec![
72 "execution".to_string(),
73 "order".to_string(),
74 "margin".to_string(),
75 "position".to_string(),
76 "wallet".to_string(),
77 ])
78 .await?;
79
80 // Create a future that completes on CTRL+C
81 let sigint = tokio::signal::ctrl_c();
82 tokio::pin!(sigint);
83
84 let stream = ws_client.stream();
85 tokio::pin!(stream); // Pin the stream to allow polling in the loop
86
87 loop {
88 tokio::select! {
89 Some(event) = stream.next() => {
90 tracing::debug!("{event:?}");
91 }
92 _ = &mut sigint => {
93 tracing::info!("Received SIGINT, closing connection...");
94 ws_client.close().await?;
95 break;
96 }
97 else => break,
98 }
99 }
100
101 Ok(())
102}