nautilus-bitmex 0.64.0

BitMEX exchange integration adapter for the Nautilus trading engine
Documentation
// -------------------------------------------------------------------------------------------------
//  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.
// -------------------------------------------------------------------------------------------------

// Under development
#![allow(dead_code)]
#![allow(unused_variables)]

use std::time::Duration;

use futures_util::StreamExt;
use nautilus_bitmex::{
    common::enums::BitmexEnvironment, http::client::BitmexHttpClient,
    websocket::client::BitmexWebSocketClient,
};
use nautilus_network::websocket::TransportBackend;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    nautilus_common::logging::ensure_logging_initialized();

    log::info!("Fetching instruments from HTTP API...");
    let http_client = BitmexHttpClient::new(
        None,                       // base_url: defaults to production
        None,                       // api_key
        None,                       // api_secret
        BitmexEnvironment::Mainnet, // environment
        60,                         // timeout_secs
        3,                          // max_retries
        1_000,                      // retry_delay_ms
        10_000,                     // retry_delay_max_ms
        10_000,                     // recv_window_ms
        10,                         // max_requests_per_second
        120,                        // max_requests_per_minute
        None,                       // proxy_url
    )
    .expect("Failed to create HTTP client");

    let instruments = http_client
        .request_instruments(true) // active_only
        .await?;

    log::info!("Fetched {} instruments", instruments.len());

    let mut ws_client = BitmexWebSocketClient::new(
        None, // url: defaults to wss://ws.bitmex.com/realtime
        None,
        None,
        None,
        5, // 5 second heartbeat
        None,
        TransportBackend::default(),
        None,
    )
    .unwrap();
    ws_client.connect().await?;

    // Give the connection a moment to stabilize
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Subscribe for all execution related topics
    ws_client
        .subscribe(vec![
            "execution".to_string(),
            "order".to_string(),
            "margin".to_string(),
            "position".to_string(),
            "wallet".to_string(),
        ])
        .await?;

    // Create a future that completes on CTRL+C
    let sigint = tokio::signal::ctrl_c();
    tokio::pin!(sigint);

    let stream = ws_client.stream();
    tokio::pin!(stream); // Pin the stream to allow polling in the loop

    loop {
        tokio::select! {
            Some(event) = stream.next() => {
                log::debug!("{event:?}");
            }
            _ = &mut sigint => {
                log::info!("Received SIGINT, closing connection...");
                ws_client.close().await?;
                break;
            }
            else => break,
        }
    }

    Ok(())
}