pub struct BybitWebSocketClient { /* private fields */ }Expand description
Public/market data WebSocket client for Bybit.
Implementations§
Source§impl BybitWebSocketClient
impl BybitWebSocketClient
Sourcepub fn new_public(url: Option<String>, heartbeat: u64) -> Self
pub fn new_public(url: Option<String>, heartbeat: u64) -> Self
Creates a new Bybit public WebSocket client.
Sourcepub fn set_auth_wait_timeout(&mut self, timeout: Duration)
pub fn set_auth_wait_timeout(&mut self, timeout: Duration)
Sets the timeout for waiting on (re)authentication before failing an
authenticated operation. Defaults to AUTH_WAIT_TIMEOUT (5s).
Sourcepub fn set_recv_window_ms(&self, recv_window_ms: u64)
pub fn set_recv_window_ms(&self, recv_window_ms: u64)
Sets the receive window sent with WebSocket trade commands.
Sourcepub fn new_public_with(
product_type: BybitProductType,
environment: BybitEnvironment,
url: Option<String>,
heartbeat: u64,
transport_backend: TransportBackend,
proxy_url: Option<String>,
) -> Self
pub fn new_public_with( product_type: BybitProductType, environment: BybitEnvironment, url: Option<String>, heartbeat: u64, transport_backend: TransportBackend, proxy_url: Option<String>, ) -> Self
Creates a new Bybit public WebSocket client targeting the specified product/environment.
Sourcepub fn with_socket_control(self, control: SocketControl) -> Self
pub fn with_socket_control(self, control: SocketControl) -> Self
Configures socket state reporting and reconnect control.
Sourcepub fn new_private(
environment: BybitEnvironment,
api_key: Option<String>,
api_secret: Option<String>,
url: Option<String>,
heartbeat: u64,
transport_backend: TransportBackend,
proxy_url: Option<String>,
) -> Self
pub fn new_private( environment: BybitEnvironment, api_key: Option<String>, api_secret: Option<String>, url: Option<String>, heartbeat: u64, transport_backend: TransportBackend, proxy_url: Option<String>, ) -> Self
Creates a new Bybit private WebSocket client.
If api_key or api_secret are not provided, they will be loaded from
environment variables based on the environment:
- Demo:
BYBIT_DEMO_API_KEY,BYBIT_DEMO_API_SECRET - Testnet:
BYBIT_TESTNET_API_KEY,BYBIT_TESTNET_API_SECRET - Mainnet:
BYBIT_API_KEY,BYBIT_API_SECRET
Sourcepub fn new_trade(
environment: BybitEnvironment,
api_key: Option<String>,
api_secret: Option<String>,
url: Option<String>,
heartbeat: u64,
transport_backend: TransportBackend,
proxy_url: Option<String>,
) -> Self
pub fn new_trade( environment: BybitEnvironment, api_key: Option<String>, api_secret: Option<String>, url: Option<String>, heartbeat: u64, transport_backend: TransportBackend, proxy_url: Option<String>, ) -> Self
Creates a new Bybit trade WebSocket client for order operations.
If api_key or api_secret are not provided, they will be loaded from
environment variables based on the environment:
- Demo:
BYBIT_DEMO_API_KEY,BYBIT_DEMO_API_SECRET - Testnet:
BYBIT_TESTNET_API_KEY,BYBIT_TESTNET_API_SECRET - Mainnet:
BYBIT_API_KEY,BYBIT_API_SECRET
Sourcepub async fn connect(&mut self) -> BybitWsResult<()>
pub async fn connect(&mut self) -> BybitWsResult<()>
Establishes the WebSocket connection.
§Errors
Returns an error if the underlying WebSocket connection cannot be established, after retrying multiple times with exponential backoff.
Sourcepub async fn close(&mut self) -> BybitWsResult<()>
pub async fn close(&mut self) -> BybitWsResult<()>
Disconnects the WebSocket client and stops the background task.
Sourcepub async fn wait_until_active(&self, timeout_secs: f64) -> BybitWsResult<()>
pub async fn wait_until_active(&self, timeout_secs: f64) -> BybitWsResult<()>
Waits until the WebSocket client becomes active or times out.
§Errors
Returns an error if the timeout is exceeded before the client becomes active.
Sourcepub async fn subscribe(&self, topics: Vec<String>) -> BybitWsResult<()>
pub async fn subscribe(&self, topics: Vec<String>) -> BybitWsResult<()>
Subscribe to the provided topic strings.
Sourcepub async fn unsubscribe(&self, topics: Vec<String>) -> BybitWsResult<()>
pub async fn unsubscribe(&self, topics: Vec<String>) -> BybitWsResult<()>
Unsubscribe from the provided topics.
Sourcepub fn stream(&mut self) -> impl Stream<Item = BybitWsMessage> + use<>
pub fn stream(&mut self) -> impl Stream<Item = BybitWsMessage> + use<>
Returns a stream of venue-typed BybitWsMessage items.
§Panics
Panics if called before Self::connect or if the stream has already been taken.
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Returns the number of currently registered subscriptions.
Sourcepub fn credential(&self) -> Option<&Credential>
pub fn credential(&self) -> Option<&Credential>
Returns the credential associated with this client, if any.
Sourcepub fn set_account_id(&mut self, account_id: AccountId)
pub fn set_account_id(&mut self, account_id: AccountId)
Sets the account ID for account message parsing.
Sourcepub fn set_mm_level(&self, mm_level: u8)
pub fn set_mm_level(&self, mm_level: u8)
Sets the account market maker level.
Sourcepub fn account_id(&self) -> Option<AccountId>
pub fn account_id(&self) -> Option<AccountId>
Returns the account ID if set.
Sourcepub fn product_type(&self) -> Option<BybitProductType>
pub fn product_type(&self) -> Option<BybitProductType>
Returns the product type for public connections.
Sourcepub fn bar_types_cache(&self) -> &Arc<AtomicMap<String, BarType>> ⓘ
pub fn bar_types_cache(&self) -> &Arc<AtomicMap<String, BarType>> ⓘ
Returns a reference to the bar types cache.
Sourcepub fn cache_instrument(&self, instrument: InstrumentAny)
pub fn cache_instrument(&self, instrument: InstrumentAny)
Adds an instrument to the shared instruments cache.
Sourcepub fn instruments_snapshot(&self) -> AHashMap<Ustr, InstrumentAny>
pub fn instruments_snapshot(&self) -> AHashMap<Ustr, InstrumentAny>
Returns a snapshot of the instruments cache keyed by symbol.
Sourcepub fn set_bars_timestamp_on_close(&self, value: bool)
pub fn set_bars_timestamp_on_close(&self, value: bool)
Sets whether bar timestamps use the close time.
Sourcepub fn bars_timestamp_on_close(&self) -> bool
pub fn bars_timestamp_on_close(&self) -> bool
Returns whether bar timestamps use the close time.
Sourcepub fn add_option_greeks_sub(&self, instrument_id: InstrumentId)
pub fn add_option_greeks_sub(&self, instrument_id: InstrumentId)
Adds an instrument ID to the option greeks subscription set.
Sourcepub fn remove_option_greeks_sub(&self, instrument_id: &InstrumentId)
pub fn remove_option_greeks_sub(&self, instrument_id: &InstrumentId)
Removes an instrument ID from the option greeks subscription set.
Sourcepub fn option_greeks_subs(&self) -> &Arc<AtomicSet<InstrumentId>> ⓘ
pub fn option_greeks_subs(&self) -> &Arc<AtomicSet<InstrumentId>> ⓘ
Returns a reference to the option greeks subscription set.
Sourcepub fn trade_subs(&self) -> &Arc<AtomicSet<InstrumentId>> ⓘ
pub fn trade_subs(&self) -> &Arc<AtomicSet<InstrumentId>> ⓘ
Returns a reference to the trade subscriptions set.
Sourcepub fn instruments_cache_ref(&self) -> &Arc<AtomicMap<Ustr, InstrumentAny>> ⓘ
pub fn instruments_cache_ref(&self) -> &Arc<AtomicMap<Ustr, InstrumentAny>> ⓘ
Returns a reference to the live instruments cache Arc.
Sourcepub async fn subscribe_orderbook(
&self,
instrument_id: InstrumentId,
depth: u32,
) -> BybitWsResult<()>
pub async fn subscribe_orderbook( &self, instrument_id: InstrumentId, depth: u32, ) -> BybitWsResult<()>
Subscribes to orderbook updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://bybit-exchange.github.io/docs/v5/websocket/public/orderbook
Sourcepub async fn unsubscribe_orderbook(
&self,
instrument_id: InstrumentId,
depth: u32,
) -> BybitWsResult<()>
pub async fn unsubscribe_orderbook( &self, instrument_id: InstrumentId, depth: u32, ) -> BybitWsResult<()>
Unsubscribes from orderbook updates for a specific instrument.
Sourcepub async fn subscribe_trades(
&self,
instrument_id: InstrumentId,
) -> BybitWsResult<()>
pub async fn subscribe_trades( &self, instrument_id: InstrumentId, ) -> BybitWsResult<()>
Subscribes to public trade updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://bybit-exchange.github.io/docs/v5/websocket/public/trade
Sourcepub async fn unsubscribe_trades(
&self,
instrument_id: InstrumentId,
) -> BybitWsResult<()>
pub async fn unsubscribe_trades( &self, instrument_id: InstrumentId, ) -> BybitWsResult<()>
Unsubscribes from public trade updates for a specific instrument.
Sourcepub async fn subscribe_ticker(
&self,
instrument_id: InstrumentId,
) -> BybitWsResult<()>
pub async fn subscribe_ticker( &self, instrument_id: InstrumentId, ) -> BybitWsResult<()>
Subscribes to ticker updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://bybit-exchange.github.io/docs/v5/websocket/public/ticker
Sourcepub async fn unsubscribe_ticker(
&self,
instrument_id: InstrumentId,
) -> BybitWsResult<()>
pub async fn unsubscribe_ticker( &self, instrument_id: InstrumentId, ) -> BybitWsResult<()>
Unsubscribes from ticker updates for a specific instrument.
Sourcepub async fn subscribe_bars(&self, bar_type: BarType) -> BybitWsResult<()>
pub async fn subscribe_bars(&self, bar_type: BarType) -> BybitWsResult<()>
Subscribes to kline/candlestick updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://bybit-exchange.github.io/docs/v5/websocket/public/kline
Sourcepub async fn unsubscribe_bars(&self, bar_type: BarType) -> BybitWsResult<()>
pub async fn unsubscribe_bars(&self, bar_type: BarType) -> BybitWsResult<()>
Unsubscribes from kline/candlestick updates for a specific instrument.
Sourcepub async fn subscribe_orders(&self) -> BybitWsResult<()>
pub async fn subscribe_orders(&self) -> BybitWsResult<()>
Subscribes to order updates.
§Errors
Returns an error if the subscription request fails or if not authenticated.
§References
https://bybit-exchange.github.io/docs/v5/websocket/private/order
Sourcepub async fn unsubscribe_orders(&self) -> BybitWsResult<()>
pub async fn unsubscribe_orders(&self) -> BybitWsResult<()>
Unsubscribes from order updates.
Sourcepub async fn subscribe_executions(&self) -> BybitWsResult<()>
pub async fn subscribe_executions(&self) -> BybitWsResult<()>
Subscribes to execution/fill updates.
§Errors
Returns an error if the subscription request fails or if not authenticated.
§References
https://bybit-exchange.github.io/docs/v5/websocket/private/execution
Sourcepub async fn unsubscribe_executions(&self) -> BybitWsResult<()>
pub async fn unsubscribe_executions(&self) -> BybitWsResult<()>
Unsubscribes from execution/fill updates.
Sourcepub async fn subscribe_executions_fast(&self) -> BybitWsResult<()>
pub async fn subscribe_executions_fast(&self) -> BybitWsResult<()>
Subscribes to fast execution updates (slim payload, lower latency).
§Errors
Returns an error if the subscription request fails or if not authenticated.
§References
https://bybit-exchange.github.io/docs/v5/websocket/private/fast-execution
Sourcepub async fn unsubscribe_executions_fast(&self) -> BybitWsResult<()>
pub async fn unsubscribe_executions_fast(&self) -> BybitWsResult<()>
Unsubscribes from fast execution updates.
Sourcepub async fn subscribe_positions(&self) -> BybitWsResult<()>
pub async fn subscribe_positions(&self) -> BybitWsResult<()>
Subscribes to position updates.
§Errors
Returns an error if the subscription request fails or if not authenticated.
§References
https://bybit-exchange.github.io/docs/v5/websocket/private/position
Sourcepub async fn unsubscribe_positions(&self) -> BybitWsResult<()>
pub async fn unsubscribe_positions(&self) -> BybitWsResult<()>
Unsubscribes from position updates.
Sourcepub async fn subscribe_wallet(&self) -> BybitWsResult<()>
pub async fn subscribe_wallet(&self) -> BybitWsResult<()>
Subscribes to wallet/balance updates.
§Errors
Returns an error if the subscription request fails or if not authenticated.
§References
https://bybit-exchange.github.io/docs/v5/websocket/private/wallet
Sourcepub async fn unsubscribe_wallet(&self) -> BybitWsResult<()>
pub async fn unsubscribe_wallet(&self) -> BybitWsResult<()>
Unsubscribes from wallet/balance updates.
Sourcepub async fn place_order(
&self,
params: BybitWsPlaceOrderParams,
) -> BybitWsResult<String>
pub async fn place_order( &self, params: BybitWsPlaceOrderParams, ) -> BybitWsResult<String>
Places an order via WebSocket, returning the request ID for correlation.
§Errors
Returns an error if the order request fails or if not authenticated.
Sourcepub async fn amend_order(
&self,
params: BybitWsAmendOrderParams,
) -> BybitWsResult<String>
pub async fn amend_order( &self, params: BybitWsAmendOrderParams, ) -> BybitWsResult<String>
Amends an existing order via WebSocket, returning the request ID for correlation.
§Errors
Returns an error if the amend request fails or if not authenticated.
Sourcepub async fn cancel_order(
&self,
params: BybitWsCancelOrderParams,
) -> BybitWsResult<String>
pub async fn cancel_order( &self, params: BybitWsCancelOrderParams, ) -> BybitWsResult<String>
Cancels an order via WebSocket, returning the request ID for correlation.
§Errors
Returns an error if the cancel request fails or if not authenticated.
Sourcepub async fn batch_place_orders(
&self,
orders: Vec<BybitWsPlaceOrderParams>,
) -> BybitWsResult<Vec<String>>
pub async fn batch_place_orders( &self, orders: Vec<BybitWsPlaceOrderParams>, ) -> BybitWsResult<Vec<String>>
Batch creates multiple orders via WebSocket, returning the request ID for correlation.
§Errors
Returns an error if the batch request fails or if not authenticated.
Sourcepub async fn batch_amend_orders(
&self,
orders: Vec<BybitWsAmendOrderParams>,
) -> BybitWsResult<Vec<String>>
pub async fn batch_amend_orders( &self, orders: Vec<BybitWsAmendOrderParams>, ) -> BybitWsResult<Vec<String>>
Batch amends multiple orders via WebSocket.
§Errors
Returns an error if the batch request fails or if not authenticated.
Sourcepub async fn batch_cancel_orders(
&self,
orders: Vec<BybitWsCancelOrderParams>,
) -> BybitWsResult<Vec<String>>
pub async fn batch_cancel_orders( &self, orders: Vec<BybitWsCancelOrderParams>, ) -> BybitWsResult<Vec<String>>
Batch cancels multiple orders via WebSocket, returning the request ID for correlation.
§Errors
Returns an error if the batch request fails or if not authenticated.
Sourcepub async fn submit_order(
&self,
product_type: BybitProductType,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
order_side: OrderSide,
order_type: OrderType,
quantity: Quantity,
is_quote_quantity: bool,
time_in_force: Option<TimeInForce>,
price: Option<Price>,
trigger_price: Option<Price>,
trigger_type: Option<TriggerType>,
post_only: Option<bool>,
reduce_only: Option<bool>,
is_leverage: bool,
position_idx: Option<BybitPositionIdx>,
bbo_side_type: Option<BybitBboSideType>,
bbo_level: Option<String>,
) -> BybitWsResult<String>
pub async fn submit_order( &self, product_type: BybitProductType, instrument_id: InstrumentId, client_order_id: ClientOrderId, order_side: OrderSide, order_type: OrderType, quantity: Quantity, is_quote_quantity: bool, time_in_force: Option<TimeInForce>, price: Option<Price>, trigger_price: Option<Price>, trigger_type: Option<TriggerType>, post_only: Option<bool>, reduce_only: Option<bool>, is_leverage: bool, position_idx: Option<BybitPositionIdx>, bbo_side_type: Option<BybitBboSideType>, bbo_level: Option<String>, ) -> BybitWsResult<String>
Submits an order using Nautilus domain objects.
§Errors
Returns an error if order submission fails or if not authenticated.
Sourcepub async fn modify_order(
&self,
product_type: BybitProductType,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
venue_order_id: Option<VenueOrderId>,
quantity: Option<Quantity>,
price: Option<Price>,
) -> BybitWsResult<String>
pub async fn modify_order( &self, product_type: BybitProductType, instrument_id: InstrumentId, client_order_id: ClientOrderId, venue_order_id: Option<VenueOrderId>, quantity: Option<Quantity>, price: Option<Price>, ) -> BybitWsResult<String>
Modifies an existing order using Nautilus domain objects.
§Errors
Returns an error if modification fails or if not authenticated.
Sourcepub async fn cancel_order_by_id(
&self,
product_type: BybitProductType,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
venue_order_id: Option<VenueOrderId>,
) -> BybitWsResult<String>
pub async fn cancel_order_by_id( &self, product_type: BybitProductType, instrument_id: InstrumentId, client_order_id: ClientOrderId, venue_order_id: Option<VenueOrderId>, ) -> BybitWsResult<String>
Cancels an order using Nautilus domain objects.
§Errors
Returns an error if cancellation fails or if not authenticated.
Sourcepub fn build_place_order_params(
&self,
product_type: BybitProductType,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
order_side: OrderSide,
order_type: OrderType,
quantity: Quantity,
is_quote_quantity: bool,
time_in_force: Option<TimeInForce>,
price: Option<Price>,
trigger_price: Option<Price>,
trigger_type: Option<TriggerType>,
post_only: Option<bool>,
reduce_only: Option<bool>,
is_leverage: bool,
take_profit: Option<Price>,
stop_loss: Option<Price>,
position_idx: Option<BybitPositionIdx>,
bbo_side_type: Option<BybitBboSideType>,
bbo_level: Option<String>,
) -> BybitWsResult<BybitWsPlaceOrderParams>
pub fn build_place_order_params( &self, product_type: BybitProductType, instrument_id: InstrumentId, client_order_id: ClientOrderId, order_side: OrderSide, order_type: OrderType, quantity: Quantity, is_quote_quantity: bool, time_in_force: Option<TimeInForce>, price: Option<Price>, trigger_price: Option<Price>, trigger_type: Option<TriggerType>, post_only: Option<bool>, reduce_only: Option<bool>, is_leverage: bool, take_profit: Option<Price>, stop_loss: Option<Price>, position_idx: Option<BybitPositionIdx>, bbo_side_type: Option<BybitBboSideType>, bbo_level: Option<String>, ) -> BybitWsResult<BybitWsPlaceOrderParams>
Builds order params for placing an order.
Sourcepub fn build_amend_order_params(
&self,
product_type: BybitProductType,
instrument_id: InstrumentId,
venue_order_id: Option<VenueOrderId>,
client_order_id: Option<ClientOrderId>,
quantity: Option<Quantity>,
price: Option<Price>,
) -> BybitWsResult<BybitWsAmendOrderParams>
pub fn build_amend_order_params( &self, product_type: BybitProductType, instrument_id: InstrumentId, venue_order_id: Option<VenueOrderId>, client_order_id: Option<ClientOrderId>, quantity: Option<Quantity>, price: Option<Price>, ) -> BybitWsResult<BybitWsAmendOrderParams>
Builds order params for amending an order.
Sourcepub fn build_cancel_order_params(
&self,
product_type: BybitProductType,
instrument_id: InstrumentId,
venue_order_id: Option<VenueOrderId>,
client_order_id: Option<ClientOrderId>,
) -> BybitWsResult<BybitWsCancelOrderParams>
pub fn build_cancel_order_params( &self, product_type: BybitProductType, instrument_id: InstrumentId, venue_order_id: Option<VenueOrderId>, client_order_id: Option<ClientOrderId>, ) -> BybitWsResult<BybitWsCancelOrderParams>
Builds order params for canceling an order via WebSocket.
§Errors
Returns an error if symbol parsing fails or if neither venue_order_id nor client_order_id is provided.
Trait Implementations§
Source§impl Clone for BybitWebSocketClient
impl Clone for BybitWebSocketClient
Source§impl Debug for BybitWebSocketClient
impl Debug for BybitWebSocketClient
Source§impl Drop for BybitWebSocketClient
impl Drop for BybitWebSocketClient
Auto Trait Implementations§
impl !Freeze for BybitWebSocketClient
impl !RefUnwindSafe for BybitWebSocketClient
impl !UnwindSafe for BybitWebSocketClient
impl Send for BybitWebSocketClient
impl Sync for BybitWebSocketClient
impl Unpin for BybitWebSocketClient
impl UnsafeUnpin for BybitWebSocketClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more