Skip to main content

Crate brokapi_bybit

Crate brokapi_bybit 

Source
Expand description

Bybit connectors: the market data connector and the user data connector.

This crate is the connector layer for the Bybit API V5, category linear (USDT perpetuals and futures). It sits on top of the protocol crate brokapi-bybit-protocol (HTTP and WebSocket clients, wire models) and the runtime crates (brokapi-runtime, brokapi-sansio), and it implements the traits of brokapi-core:

  • MarketDataConnector implements OrderBookSource, MarketDataSource and InstrumentSource. It is not tied to an account and needs no key for the data.
  • UserDataConnector implements OrderExecutor, StopOrderExecutor, ExecutionSource and PortfolioSource for one unified trading account.

Both connectors start with an async fn start(config, limiter, out). Commands return their answer directly. Subscriptions deliver Envelopes into the channel out.

The contour is a setting of the config: the main exchange by default, the demo account (api-demo.bybit.com, virtual money, round the clock) with BybitConfig::with_env(BybitEnv::Demo). Anything the shared traits cannot express is reachable through connector.raw() (BybitRaw): the same key, endpoint and rate limiter, one API method at a time. limits::default_limits gives the bundled rate limits. Give every connector of one Bybit login the same limiter, because the limits belong to the login.

The full guide is at https://brokapi.ru/docs/.

§Examples

The example runs against an in-process imitation of the exchange, so it needs no network and no account. With a real account, cfg is built with BybitConfig::from_env() instead.

use std::sync::Arc;
use brokapi_bybit::limits::default_limits;
use brokapi_bybit::{MarketDataConfig, MarketDataConnector, UserDataConfig, UserDataConnector};
use brokapi_core::{PortfolioSource, ShutdownMode};
use brokapi_runtime::BrokerLimiter;
use tokio::sync::mpsc;

// One limiter for the whole login.
let limiter: Arc<BrokerLimiter> = BrokerLimiter::new(&default_limits());
let (md_tx, _md_events) = mpsc::channel(256);
let (ud_tx, _ud_events) = mpsc::channel(256);

let md = MarketDataConnector::start(
    MarketDataConfig::new(cfg.clone(), "bybit-md"),
    Arc::clone(&limiter),
    md_tx,
)
.await?;
let ud = UserDataConnector::start(UserDataConfig::new(cfg, "bybit-ud"), limiter, ud_tx).await?;

let portfolio = ud.portfolio().await?;
println!("{} balances", portfolio.balances.len());

md.shutdown(ShutdownMode::Immediate).await;
ud.shutdown(ShutdownMode::Immediate).await;

Re-exports§

pub use cid::BybitCidCodec;
pub use config::MarketDataConfig;
pub use config::UserDataConfig;
pub use core::MdCore;
pub use core::UdCore;
pub use md::MarketDataConnector;
pub use raw::BybitRaw;
pub use ud::UserDataConnector;

Modules§

cid
The client order id codec: Bybit keeps a client order id (orderLinkId) of up to 36 characters on every order, and a full hyphenated UUID is exactly 36.
config
Configuration of the two connectors: the Bybit client settings plus the choices of the connector itself (source label, reconnect pauses, buffer size).
core
The sans-io cores of the Bybit connectors.
error
Mapping of the errors of the protocol layer to the broker-independent [Error].
limits
Loads limits.toml (section [bybit]) through BrokerLimits::from_toml_section.
map
Mapping of the wire models of the exchange to the broker-independent types.
md
The market data connector: order books, trades, quotes, candles and the directory of instruments.
pump
The task that feeds frames of the stream to a core and the events of the core to the emitter.
raw
Direct access to any Bybit API method (the “back door”): same key pair, same endpoint, same [BrokerLimiter] as the connector it came from.
subs
The subscription records a connector keeps, and the generators of subscription ids and request ids.
ud
The user data connector: orders, stop orders, fills and the portfolio of one Bybit account.
util
Small helpers: instrument ids, intervals, times.