pub struct Bybit { /* private fields */ }Expand description
A Bybit client over an injected HTTP transport.
Implementations§
Source§impl Bybit
impl Bybit
Sourcepub fn sync_time(&mut self) -> Result<i64, Error>
pub fn sync_time(&mut self) -> Result<i64, Error>
Learn the offset between this machine’s clock and the venue’s, from
GET /v5/market/time.
Explicit rather than automatic: it costs a request, and a client should not make one the caller did not ask for. Call it once after connecting, and again if the process runs long enough for drift to matter.
Returns the new offset in milliseconds (server - local).
§Errors
Returns an Error if the request fails or the response cannot be parsed.
Sourcepub fn with_ws(self, ws: Box<dyn WsTransport>) -> Bybit
pub fn with_ws(self, ws: Box<dyn WsTransport>) -> Bybit
Attach a WebSocket transport, enabling the streaming subscriptions.
Sourcepub fn with_http(
http: Box<dyn HttpTransport>,
options: &ExchangeOptions,
) -> Bybit
pub fn with_http( http: Box<dyn HttpTransport>, options: &ExchangeOptions, ) -> Bybit
Build a public Bybit client over the given transport and options.
Sourcepub fn with_credentials(
http: Box<dyn HttpTransport>,
options: &ExchangeOptions,
credentials: Credentials,
) -> Bybit
pub fn with_credentials( http: Box<dyn HttpTransport>, options: &ExchangeOptions, credentials: Credentials, ) -> Bybit
Build an authenticated Bybit client for signed endpoints.
Sourcepub fn with_clock(self, now_ms: Box<dyn Fn() -> i64 + Send + Sync>) -> Bybit
pub fn with_clock(self, now_ms: Box<dyn Fn() -> i64 + Send + Sync>) -> Bybit
Override the timestamp source (used for deterministic signing in tests).
Sourcepub fn category(&self) -> &'static str
pub fn category(&self) -> &'static str
The Bybit product category this client targets (spot/linear/inverse).
Sourcepub fn wire_symbol(symbol: &Symbol) -> String
pub fn wire_symbol(symbol: &Symbol) -> String
The Bybit wire symbol for a canonical Symbol (BTC/USDT -> BTCUSDT).
Sourcepub fn klines(
&self,
symbol: &Symbol,
interval: &str,
limit: u32,
) -> Result<Vec<Candle>, Error>
pub fn klines( &self, symbol: &Symbol, interval: &str, limit: u32, ) -> Result<Vec<Candle>, Error>
Sourcepub fn order_book(
&self,
symbol: &Symbol,
depth: u32,
) -> Result<OrderBookSnapshot, Error>
pub fn order_book( &self, symbol: &Symbol, depth: u32, ) -> Result<OrderBookSnapshot, Error>
Sourcepub fn subscribe_trades(&mut self, symbol: &Symbol) -> Result<(), Error>
pub fn subscribe_trades(&mut self, symbol: &Symbol) -> Result<(), Error>
Subscribe to the public trade stream for symbol.
§Errors
Returns Error::NotConnected if no WebSocket transport is configured,
or a transport error if the connection or subscription fails.
Sourcepub fn subscribe_derivatives(
&mut self,
symbol: &Symbol,
channel: DerivativesChannel,
) -> Result<(), Error>
pub fn subscribe_derivatives( &mut self, symbol: &Symbol, channel: DerivativesChannel, ) -> Result<(), Error>
Subscribe to a pushed derivatives channel.
Bybit publishes funding, mark and index on the same tickers stream
that carries the ordinary ticker – one topic answering four questions –
so both channels resolve to it and the parser emits whichever the caller
subscribed to. Liquidations are allLiquidation, which reports the taker
side of the forced order rather than the side of the position that was
closed; that is the side this crate’s Liquidation carries, so it maps
straight through.
These streams exist only on the linear (futures) venue.
§Errors
Returns an Error on a spot client, or if the subscription fails.
Sourcepub fn open_interest(&self, symbol: &Symbol) -> Result<OpenInterest, Error>
pub fn open_interest(&self, symbol: &Symbol) -> Result<OpenInterest, Error>
The current open interest (GET /v5/market/open-interest), most recent
point.
Bybit does push open interest, on the tickers stream – but only as one
field among many, and only when it changes. Reading it is what gives a
caller a figure at a known moment rather than whenever the venue last
happened to include it.
§Errors
Returns an Error on a spot client, if the request fails, or if the
venue returns no data point.
Sourcepub fn long_short_ratio(&self, symbol: &Symbol) -> Result<LongShortRatio, Error>
pub fn long_short_ratio(&self, symbol: &Symbol) -> Result<LongShortRatio, Error>
Sourcepub fn poll_events(&mut self) -> Vec<Event>
pub fn poll_events(&mut self) -> Vec<Event>
Drain all stream events available since the last call. Non-blocking; frames that fail to parse are skipped.
Sourcepub fn subscribe_user_data(&mut self) -> Result<(), Error>
pub fn subscribe_user_data(&mut self) -> Result<(), Error>
Open the private user-data stream (wss://.../v5/private). Authenticates
with an op:auth frame (signature = HMAC-SHA256 over GET/realtime<expires>),
then subscribes to the order and wallet topics. Afterwards
poll_events also surfaces the account’s own
Event::OrderUpdate and Event::BalanceUpdate.
A dropped private stream is re-subscribed automatically on the next
poll_events; call
keepalive_user_data periodically to keep it
from being dropped for inactivity.
§Errors
Returns Error::InvalidCredentials without credentials, Error::NotConnected
without a WebSocket transport, or another Error if the request fails.
Sourcepub fn keepalive_user_data(&mut self) -> Result<(), Error>
pub fn keepalive_user_data(&mut self) -> Result<(), Error>
Send an application-level heartbeat (op:ping) on the private stream so it
is not dropped for inactivity. A no-op before
subscribe_user_data.
§Errors
Returns an Error if the ping cannot be sent.
Sourcepub fn place_order(&self, request: &OrderRequest) -> Result<Order, Error>
pub fn place_order(&self, request: &OrderRequest) -> Result<Order, Error>
Sourcepub fn place_order_ws(&mut self, request: &OrderRequest) -> Result<Order, Error>
pub fn place_order_ws(&mut self, request: &OrderRequest) -> Result<Order, Error>
Place an order over the Bybit WebSocket trade API (order.create). Builds
the same args as the REST path and exchanges them on the lazily-opened,
authenticated /v5/trade connection. Bybit returns only the ids, so the
resulting Order carries the request’s fields with a New status.
§Errors
Returns Error::NotConnected without a WebSocket transport, or another
Error if the order is invalid or rejected.
Sourcepub fn cancel_order_ws(
&mut self,
symbol: &Symbol,
order_id: &str,
) -> Result<(), Error>
pub fn cancel_order_ws( &mut self, symbol: &Symbol, order_id: &str, ) -> Result<(), Error>
Cancel an order over the Bybit WebSocket trade API (order.cancel).
§Errors
Returns Error::NotConnected without a WebSocket transport, or another
Error if the order is unknown or the request fails.
impl Bybit
The Bybit product category for a market type.
Source§impl Bybit
impl Bybit
Sourcepub fn set_margin_mode(
&self,
symbol: &Symbol,
mode: MarginMode,
) -> Result<(), Error>
pub fn set_margin_mode( &self, symbol: &Symbol, mode: MarginMode, ) -> Result<(), Error>
Sourcepub fn close_position(&self, symbol: &Symbol) -> Result<Order, Error>
pub fn close_position(&self, symbol: &Symbol) -> Result<Order, Error>
Flatten the open position in symbol with a reduce-only market order.
§Errors
Returns Error::NotFound if there is no open position, or another
Error if the request fails.
Sourcepub fn amend_order(
&self,
symbol: &Symbol,
order_id: &str,
new_price: Option<Decimal>,
new_quantity: Option<Decimal>,
) -> Result<Order, Error>
pub fn amend_order( &self, symbol: &Symbol, order_id: &str, new_price: Option<Decimal>, new_quantity: Option<Decimal>, ) -> Result<Order, Error>
Sourcepub fn place_batch(
&self,
requests: &[OrderRequest],
) -> Result<Vec<Result<Order, Error>>, Error>
pub fn place_batch( &self, requests: &[OrderRequest], ) -> Result<Vec<Result<Order, Error>>, Error>
Trait Implementations§
Source§impl AdvancedOrders for Bybit
impl AdvancedOrders for Bybit
Source§fn place_oco(&mut self, _request: &OcoRequest) -> Result<Vec<Order>, Error>
fn place_oco(&mut self, _request: &OcoRequest) -> Result<Vec<Order>, Error>
Bybit has no standalone one-cancels-other order-list (take-profit/stop are
attached to a position/order via takeProfit/stopLoss), so this returns
an Error::Exchange.
Source§fn amend_order(
&mut self,
symbol: &Symbol,
order_id: &str,
new_price: Option<Decimal>,
new_quantity: Option<Decimal>,
) -> Result<Order, Error>
fn amend_order( &mut self, symbol: &Symbol, order_id: &str, new_price: Option<Decimal>, new_quantity: Option<Decimal>, ) -> Result<Order, Error>
None leaves that field
unchanged. Venues without a native amend emulate it as cancel-replace. Read moreSource§fn place_batch(
&mut self,
requests: &[OrderRequest],
) -> Result<Vec<Result<Order, Error>>, Error>
fn place_batch( &mut self, requests: &[OrderRequest], ) -> Result<Vec<Result<Order, Error>>, Error>
Source§impl Debug for Bybit
Hand-written: the client holds Box<dyn HttpTransport>, Box<dyn WsTransport>
and a Box<dyn Fn() -> i64> clock, none of which a derive can reach. The
transports are shown by whether a connection is open rather than by value, and
the credentials only by whether they are set – printing them would put secret
material into every log line that formats a client.
impl Debug for Bybit
Hand-written: the client holds Box<dyn HttpTransport>, Box<dyn WsTransport>
and a Box<dyn Fn() -> i64> clock, none of which a derive can reach. The
transports are shown by whether a connection is open rather than by value, and
the credentials only by whether they are set – printing them would put secret
material into every log line that formats a client.
Source§impl Derivatives for Bybit
impl Derivatives for Bybit
Source§fn positions(&mut self, symbol: Option<&Symbol>) -> Result<Vec<Position>, Error>
fn positions(&mut self, symbol: Option<&Symbol>) -> Result<Vec<Position>, Error>
symbol; flat positions are omitted. Read moreSource§fn set_leverage(&mut self, symbol: &Symbol, leverage: u32) -> Result<(), Error>
fn set_leverage(&mut self, symbol: &Symbol, leverage: u32) -> Result<(), Error>
symbol. Read moreSource§fn set_margin_mode(
&mut self,
symbol: &Symbol,
mode: MarginMode,
) -> Result<(), Error>
fn set_margin_mode( &mut self, symbol: &Symbol, mode: MarginMode, ) -> Result<(), Error>
symbol. Read moreSource§impl DerivativesStream for Bybit
impl DerivativesStream for Bybit
Source§fn subscribe_derivatives(
&mut self,
symbol: &Symbol,
channel: DerivativesChannel,
) -> Result<(), Error>
fn subscribe_derivatives( &mut self, symbol: &Symbol, channel: DerivativesChannel, ) -> Result<(), Error>
poll_events as
Event::Derivatives. Read moreSource§fn open_interest(&mut self, symbol: &Symbol) -> Result<OpenInterest, Error>
fn open_interest(&mut self, symbol: &Symbol) -> Result<OpenInterest, Error>
Source§fn long_short_ratio(&mut self, symbol: &Symbol) -> Result<LongShortRatio, Error>
fn long_short_ratio(&mut self, symbol: &Symbol) -> Result<LongShortRatio, Error>
Source§impl Execution for Bybit
impl Execution for Bybit
Source§fn place_order(&mut self, request: &OrderRequest) -> Result<Order, Error>
fn place_order(&mut self, request: &OrderRequest) -> Result<Order, Error>
Source§fn cancel_order(&mut self, symbol: &Symbol, order_id: &str) -> Result<(), Error>
fn cancel_order(&mut self, symbol: &Symbol, order_id: &str) -> Result<(), Error>
Source§fn query_order(
&mut self,
symbol: &Symbol,
order_id: &str,
) -> Result<Order, Error>
fn query_order( &mut self, symbol: &Symbol, order_id: &str, ) -> Result<Order, Error>
Source§impl MarketData for Bybit
impl MarketData for Bybit
Source§fn ticker(&mut self, symbol: &Symbol) -> Result<Ticker, Error>
fn ticker(&mut self, symbol: &Symbol) -> Result<Ticker, Error>
symbol. Read moreSource§fn klines(
&mut self,
symbol: &Symbol,
interval: &str,
limit: u32,
) -> Result<Vec<Candle>, Error>
fn klines( &mut self, symbol: &Symbol, interval: &str, limit: u32, ) -> Result<Vec<Candle>, Error>
limit historical candles for symbol at the given interval
(e.g. "1m", "1h"). Returns wickra_core::Candles so the result
feeds the indicator core directly. Read moreSource§fn order_book(
&mut self,
symbol: &Symbol,
depth: u32,
) -> Result<OrderBookSnapshot, Error>
fn order_book( &mut self, symbol: &Symbol, depth: u32, ) -> Result<OrderBookSnapshot, Error>
Source§fn subscribe_trades(&mut self, symbol: &Symbol) -> Result<(), Error>
fn subscribe_trades(&mut self, symbol: &Symbol) -> Result<(), Error>
symbol. Read moreSource§fn subscribe_book(&mut self, symbol: &Symbol) -> Result<(), Error>
fn subscribe_book(&mut self, symbol: &Symbol) -> Result<(), Error>
symbol. Read moreSource§fn subscribe_ticker(&mut self, symbol: &Symbol) -> Result<(), Error>
fn subscribe_ticker(&mut self, symbol: &Symbol) -> Result<(), Error>
symbol. Read moreSource§fn poll_events(&mut self) -> Vec<Event>
fn poll_events(&mut self) -> Vec<Event>
Source§impl WsExecution for Bybit
impl WsExecution for Bybit
Source§fn place_order_ws(&mut self, request: &OrderRequest) -> Result<Order, Error>
fn place_order_ws(&mut self, request: &OrderRequest) -> Result<Order, Error>
Source§impl WsUserData for Bybit
impl WsUserData for Bybit
Source§fn subscribe_user_data(&mut self) -> Result<(), Error>
fn subscribe_user_data(&mut self) -> Result<(), Error>
poll_events on the same client also drains the
user’s order and balance events alongside the public market-data stream. Read moreSource§fn keepalive_user_data(&mut self) -> Result<(), Error>
fn keepalive_user_data(&mut self) -> Result<(), Error>
Auto Trait Implementations§
impl !RefUnwindSafe for Bybit
impl !Sync for Bybit
impl !UnwindSafe for Bybit
impl Freeze for Bybit
impl Send for Bybit
impl Unpin for Bybit
impl UnsafeUnpin for Bybit
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> 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