pub struct Bybit { /* private fields */ }Expand description
Bybit exchange structure.
Implementations§
Source§impl Bybit
impl Bybit
Sourcepub fn get_auth(&self) -> Result<BybitAuth, Error>
pub fn get_auth(&self) -> Result<BybitAuth, Error>
Get the authentication instance if credentials are configured.
Sourcepub fn check_required_credentials(&self) -> Result<(), Error>
pub fn check_required_credentials(&self) -> Result<(), Error>
Check that required credentials are configured.
Sourcepub async fn fetch_markets(
&self,
) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
pub async fn fetch_markets( &self, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
Fetch all trading markets.
§Returns
Returns a vector of Market structures containing market information.
§Errors
Returns an error if the API request fails or response parsing fails.
§Example
let bybit = Bybit::builder().build()?;
let markets = bybit.fetch_markets().await?;
println!("Found {} markets", markets.len());Sourcepub async fn load_markets(
&self,
reload: bool,
) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
pub async fn load_markets( &self, reload: bool, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
Sourcepub async fn fetch_tickers(
&self,
symbols: Option<Vec<String>>,
) -> Result<Vec<Ticker>, Error>
pub async fn fetch_tickers( &self, symbols: Option<Vec<String>>, ) -> Result<Vec<Ticker>, Error>
Sourcepub async fn fetch_order_book(
&self,
symbol: &str,
limit: Option<u32>,
) -> Result<OrderBook, Error>
pub async fn fetch_order_book( &self, symbol: &str, limit: Option<u32>, ) -> Result<OrderBook, Error>
Sourcepub async fn fetch_trades(
&self,
symbol: &str,
limit: Option<u32>,
) -> Result<Vec<Trade>, Error>
pub async fn fetch_trades( &self, symbol: &str, limit: Option<u32>, ) -> Result<Vec<Trade>, Error>
Sourcepub async fn fetch_ohlcv_v2(
&self,
request: OhlcvRequest,
) -> Result<Vec<OHLCV>, Error>
pub async fn fetch_ohlcv_v2( &self, request: OhlcvRequest, ) -> Result<Vec<OHLCV>, Error>
Fetch OHLCV (candlestick) data using the builder pattern.
This is the preferred method for fetching OHLCV data. It accepts an OhlcvRequest
built using the builder pattern, which provides validation and a more ergonomic API.
§Arguments
request- OHLCV request built viaOhlcvRequest::builder()
§Returns
Returns a vector of OHLCV structures.
§Errors
Returns an error if the market is not found or the API request fails.
Requirements: 2.3, 2.6
Sourcepub async fn fetch_ohlcv(
&self,
symbol: &str,
timeframe: &str,
since: Option<i64>,
limit: Option<u32>,
) -> Result<Vec<OHLCV>, Error>
👎Deprecated since 0.2.0: Use fetch_ohlcv_v2 with OhlcvRequest::builder() instead
pub async fn fetch_ohlcv( &self, symbol: &str, timeframe: &str, since: Option<i64>, limit: Option<u32>, ) -> Result<Vec<OHLCV>, Error>
Fetch OHLCV (candlestick) data (deprecated).
§Deprecated
This method is deprecated. Use fetch_ohlcv_v2 with
OhlcvRequest::builder() instead for a more ergonomic API.
§Arguments
symbol- Trading pair symbol.timeframe- Candlestick timeframe (e.g., “1m”, “5m”, “1h”, “1d”).since- Optional start timestamp in milliseconds.limit- Optional limit on number of candles (maximum: 1000).
§Returns
Returns a vector of OHLCV structures.
Sourcepub async fn fetch_balance(&self) -> Result<Balance, Error>
pub async fn fetch_balance(&self) -> Result<Balance, Error>
Sourcepub async fn fetch_my_trades(
&self,
symbol: &str,
since: Option<i64>,
limit: Option<u32>,
) -> Result<Vec<Trade>, Error>
pub async fn fetch_my_trades( &self, symbol: &str, since: Option<i64>, limit: Option<u32>, ) -> Result<Vec<Trade>, Error>
Sourcepub async fn create_order_v2(
&self,
request: OrderRequest,
) -> Result<Order, Error>
pub async fn create_order_v2( &self, request: OrderRequest, ) -> Result<Order, Error>
Create a new order.
§Arguments
symbol- Trading pair symbol.order_type- Order type (Market, Limit).side- Order side (Buy or Sell).amount- Order quantity asAmounttype.price- Optional price asPricetype (required for limit orders).
§Returns
Returns the created Order structure with order details.
Requirements: 2.2, 2.6
Sourcepub async fn create_order(
&self,
symbol: &str,
order_type: OrderType,
side: OrderSide,
amount: Amount,
price: Option<Price>,
) -> Result<Order, Error>
👎Deprecated since 0.2.0: Use create_order_v2 with OrderRequest::builder() instead
pub async fn create_order( &self, symbol: &str, order_type: OrderType, side: OrderSide, amount: Amount, price: Option<Price>, ) -> Result<Order, Error>
Create a new order (deprecated).
§Deprecated
This method is deprecated. Use create_order_v2 with
OrderRequest::builder() instead for a more ergonomic API.
§Arguments
symbol- Trading pair symbol.order_type- Order type (Market, Limit).side- Order side (Buy or Sell).amount- Order quantity asAmounttype.price- Optional price asPricetype (required for limit orders).
§Returns
Returns the created Order structure with order details.
Sourcepub async fn fetch_open_orders(
&self,
symbol: Option<&str>,
since: Option<i64>,
limit: Option<u32>,
) -> Result<Vec<Order>, Error>
pub async fn fetch_open_orders( &self, symbol: Option<&str>, since: Option<i64>, limit: Option<u32>, ) -> Result<Vec<Order>, Error>
Source§impl Bybit
impl Bybit
Sourcepub fn builder() -> BybitBuilder
pub fn builder() -> BybitBuilder
Creates a new Bybit instance using the builder pattern.
This is the recommended way to create a Bybit instance.
§Example
use ccxt_exchanges::bybit::Bybit;
let bybit = Bybit::builder()
.api_key("your-api-key")
.secret("your-secret")
.testnet(true)
.build()
.unwrap();Sourcepub fn new_with_options(
config: ExchangeConfig,
options: BybitOptions,
) -> Result<Bybit, Error>
pub fn new_with_options( config: ExchangeConfig, options: BybitOptions, ) -> Result<Bybit, Error>
Creates a new Bybit instance with custom options.
This is used internally by the builder pattern.
§Arguments
config- Exchange configuration.options- Bybit-specific options.
Sourcepub fn base(&self) -> &BaseExchange
pub fn base(&self) -> &BaseExchange
Returns a reference to the base exchange.
Sourcepub fn base_mut(&mut self) -> &mut BaseExchange
pub fn base_mut(&mut self) -> &mut BaseExchange
Returns a mutable reference to the base exchange.
Sourcepub fn options(&self) -> &BybitOptions
pub fn options(&self) -> &BybitOptions
Returns the Bybit options.
Sourcepub fn set_options(&mut self, options: BybitOptions)
pub fn set_options(&mut self, options: BybitOptions)
Sets the Bybit options.
Sourcepub fn rate_limit(&self) -> u32
pub fn rate_limit(&self) -> u32
Returns the rate limit in requests per second.
Sourcepub fn is_sandbox(&self) -> bool
pub fn is_sandbox(&self) -> bool
Returns true if sandbox/testnet mode is enabled.
Sandbox mode is enabled when either:
config.sandboxis set totrueoptions.testnetis set totrue
§Returns
true if sandbox mode is enabled, false otherwise.
§Example
use ccxt_exchanges::bybit::Bybit;
use ccxt_core::ExchangeConfig;
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let bybit = Bybit::new(config).unwrap();
assert!(bybit.is_sandbox());Sourcepub fn timeframes(&self) -> HashMap<String, String>
pub fn timeframes(&self) -> HashMap<String, String>
Returns the supported timeframes.
Sourcepub fn default_type(&self) -> DefaultType
pub fn default_type(&self) -> DefaultType
Returns the default type configuration.
Sourcepub fn default_sub_type(&self) -> Option<DefaultSubType>
pub fn default_sub_type(&self) -> Option<DefaultSubType>
Returns the default sub-type configuration.
Sourcepub fn is_contract_type(&self) -> bool
pub fn is_contract_type(&self) -> bool
Checks if the current default_type is a contract type (Swap, Futures, or Option).
This is useful for determining whether contract-specific API parameters should be used.
§Returns
true if the default_type is Swap, Futures, or Option; false otherwise.
Sourcepub fn is_inverse(&self) -> bool
pub fn is_inverse(&self) -> bool
Checks if the current configuration uses inverse (coin-margined) contracts.
§Returns
true if default_sub_type is Inverse; false otherwise.
Sourcepub fn is_linear(&self) -> bool
pub fn is_linear(&self) -> bool
Checks if the current configuration uses linear (USDT-margined) contracts.
§Returns
true if default_sub_type is Linear or not specified (defaults to Linear); false otherwise.
Sourcepub fn category(&self) -> &'static str
pub fn category(&self) -> &'static str
Returns the Bybit category string based on the current default_type and default_sub_type.
Bybit V5 API uses category parameter for filtering:
Spot-> “spot”Swap+ Linear -> “linear”Swap+ Inverse -> “inverse”Futures+ Linear -> “linear”Futures+ Inverse -> “inverse”Option-> “option”Margin-> “spot” (margin trading uses spot category)
§Returns
The category string to use for Bybit API calls.
Sourcepub fn create_ws(&self) -> BybitWs
pub fn create_ws(&self) -> BybitWs
Creates a public WebSocket client.
The WebSocket URL is selected based on the configured default_type:
Spot-> spot public streamSwap/Futures+ Linear -> linear public streamSwap/Futures+ Inverse -> inverse public streamOption-> option public stream
§Returns
Returns a BybitWs instance for public data streams.
§Example
use ccxt_exchanges::bybit::Bybit;
use ccxt_core::ExchangeConfig;
let bybit = Bybit::new(ExchangeConfig::default())?;
let ws = bybit.create_ws();
ws.connect().await?;Sourcepub fn signed_request(
&self,
endpoint: impl Into<String>,
) -> BybitSignedRequestBuilder<'_>
pub fn signed_request( &self, endpoint: impl Into<String>, ) -> BybitSignedRequestBuilder<'_>
Creates a signed request builder for authenticated API calls.
This method provides a fluent API for constructing authenticated requests to Bybit’s private endpoints. The builder handles:
- Credential validation
- Millisecond timestamp generation
- HMAC-SHA256 signature generation (hex encoded)
- Authentication header injection (X-BAPI-* headers)
§Arguments
endpoint- API endpoint path (e.g., “/v5/account/wallet-balance”)
§Returns
Returns a BybitSignedRequestBuilder for method chaining.
§Example
use ccxt_exchanges::bybit::Bybit;
use ccxt_exchanges::bybit::signed_request::HttpMethod;
use ccxt_core::ExchangeConfig;
let bybit = Bybit::builder()
.api_key("your-api-key")
.secret("your-secret")
.build()?;
// GET request
let balance = bybit.signed_request("/v5/account/wallet-balance")
.param("accountType", "UNIFIED")
.execute()
.await?;
// POST request
let order = bybit.signed_request("/v5/order/create")
.method(HttpMethod::Post)
.param("category", "spot")
.param("symbol", "BTCUSDT")
.param("side", "Buy")
.param("orderType", "Limit")
.param("qty", "0.001")
.param("price", "50000")
.execute()
.await?;