Skip to main content

brokapi_bybit/
lib.rs

1//! Bybit connectors: the market data connector and the user data connector.
2//!
3//! This crate is the connector layer for the Bybit API V5, category `linear` (USDT perpetuals and futures). It sits on
4//! top of the protocol crate `brokapi-bybit-protocol` (HTTP and WebSocket clients, wire models) and the runtime crates
5//! (`brokapi-runtime`, `brokapi-sansio`), and it implements the traits of `brokapi-core`:
6//!
7//! - [`MarketDataConnector`] implements `OrderBookSource`, `MarketDataSource` and `InstrumentSource`. It is not tied to
8//!   an account and needs no key for the data.
9//! - [`UserDataConnector`] implements `OrderExecutor`, `StopOrderExecutor`, `ExecutionSource` and `PortfolioSource` for
10//!   one unified trading account.
11//!
12//! Both connectors start with an `async fn start(config, limiter, out)`. Commands return their answer directly.
13//! Subscriptions deliver `Envelope`s into the channel `out`.
14//!
15//! The contour is a setting of the config: the main exchange by default, the demo account (`api-demo.bybit.com`, virtual
16//! money, round the clock) with `BybitConfig::with_env(BybitEnv::Demo)`. Anything the shared traits cannot express is
17//! reachable through `connector.raw()` ([`BybitRaw`]): the same key, endpoint and rate limiter, one API method at a time.
18//! [`limits::default_limits`] gives the bundled rate limits. Give every connector of one Bybit login the same limiter,
19//! because the limits belong to the login.
20//!
21//! The full guide is at <https://brokapi.ru/docs/>.
22//!
23//! # Examples
24//!
25//! The example runs against an in-process imitation of the exchange, so it needs no network and no account. With a real
26//! account, `cfg` is built with `BybitConfig::from_env()` instead.
27//!
28//! ```
29//! use std::sync::Arc;
30//! use brokapi_bybit::limits::default_limits;
31//! use brokapi_bybit::{MarketDataConfig, MarketDataConnector, UserDataConfig, UserDataConnector};
32//! use brokapi_core::{PortfolioSource, ShutdownMode};
33//! use brokapi_runtime::BrokerLimiter;
34//! use tokio::sync::mpsc;
35//! # use brokapi_bybit_protocol::config::BybitConfig;
36//!
37//! # #[tokio::main(flavor = "current_thread")]
38//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
39//! # let server = brokapi_mocks::bybit::MockServer::start(Default::default()).await;
40//! # let cfg = BybitConfig::for_mock("mock-key", "mock-secret", &server.http_base);
41//! // One limiter for the whole login.
42//! let limiter: Arc<BrokerLimiter> = BrokerLimiter::new(&default_limits());
43//! let (md_tx, _md_events) = mpsc::channel(256);
44//! let (ud_tx, _ud_events) = mpsc::channel(256);
45//!
46//! let md = MarketDataConnector::start(
47//!     MarketDataConfig::new(cfg.clone(), "bybit-md"),
48//!     Arc::clone(&limiter),
49//!     md_tx,
50//! )
51//! .await?;
52//! let ud = UserDataConnector::start(UserDataConfig::new(cfg, "bybit-ud"), limiter, ud_tx).await?;
53//!
54//! let portfolio = ud.portfolio().await?;
55//! println!("{} balances", portfolio.balances.len());
56//!
57//! md.shutdown(ShutdownMode::Immediate).await;
58//! ud.shutdown(ShutdownMode::Immediate).await;
59//! # Ok(())
60//! # }
61//! ```
62
63#![warn(missing_docs)]
64
65pub mod cid;
66pub mod config;
67pub mod core;
68pub mod error;
69pub mod limits;
70pub mod map;
71pub mod md;
72pub mod pump;
73pub mod raw;
74pub mod subs;
75pub mod ud;
76pub mod util;
77
78pub use cid::BybitCidCodec;
79pub use config::{MarketDataConfig, UserDataConfig};
80pub use core::{MdCore, UdCore};
81pub use md::MarketDataConnector;
82pub use raw::BybitRaw;
83pub use ud::UserDataConnector;