Module bybit_websocket_trading

Source
Expand description

Bybit V5 WebSocket Trading Implementation

This module provides a high-performance WebSocket trading client for Bybit V5 exchange with robust connection management, automatic reconnection, and comprehensive health monitoring.

§Features

  • V5 unified authentication: Support for all product types (spot, linear, inverse, option)
  • Batch operations: Create, cancel, and amend up to 20 orders in a single request
  • Position updates: Real-time position change notifications
  • Connection health monitoring: Real-time metrics and health status
  • Automatic reconnection: Configurable backoff strategy with exponential delays
  • Zero-copy message processing: Optimized for low-latency trading

§Connection Management

The client maintains connection health through:

  • WebSocket ping frames sent every 20 seconds (as recommended by Bybit)
  • JSON ping requests for API-level health checks
  • Automatic reconnection on connection loss
  • Configurable pong timeout (default: 10 seconds)

§Batch Operations

Supports Bybit V5 batch operations:

  • order.create-batch: Create up to 20 orders (10 for spot) in one request
  • order.cancel-batch: Cancel multiple orders by IDs
  • order.amend-batch: Modify multiple orders

§Usage Example

use rusty_common::auth::exchanges::bybit::BybitAuth;
use rusty_ems::exchanges::bybit_websocket_trading::BybitWebSocketTrader;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    let auth = Arc::new(BybitAuth::new(
        "api_key".into(),
        "secret_key".into()
    ));

    let trader = BybitWebSocketTrader::new(auth, false); // false = mainnet
    let (report_tx, report_rx) = flume::bounded(100);

    // Connect and authenticate
    trader.connect(report_tx).await?;

    // Check connection health
    let health = trader.get_connection_health();
    println!("Connection healthy: {}", health.is_healthy);

    Ok(())
}

Structs§

BybitAmendOrderRequest
Order amendment request
BybitBatchOrderRequest
Bybit WebSocket batch order request
BybitCancelAllRequest
Cancel all orders request
BybitOrderRequest
Bybit WebSocket order request for individual orders
BybitWebSocketTrader
Bybit WebSocket Trading Client
ConnectionHealth
Connection health metrics

Enums§

BybitAccountType
Bybit account type
BybitCategory
Bybit V5 product categories