pub struct UserDataConnector { /* private fields */ }Expand description
The user data connector of Bybit for one account: implements [OrderExecutor], [StopOrderExecutor],
[ExecutionSource] and [PortfolioSource].
Commands (place, cancel) go by HTTP, one request each, never repeated: a lost answer is [Error::Unknown]. The
streams (own orders, fills, positions, wallet) come over one private WebSocket session that logs in with the key,
reconnects on its own and restores its subscriptions. The account must be in one-way position mode (the default);
the unified trading account is the only kind V5 knows.
Quantities are in units of the base coin (0.001 BTC). The connector reads the quantity step, the smallest order and
the price step of a symbol once, and refuses an order off the grid before anything is sent.
The cid goes to the exchange as the orderLinkId of the order, which is how it comes back in the order list, the
stream and the fills. A conditional order (a stop order) is an order with a trigger price: it is placed and cancelled
through the same methods, and listed apart in [StopOrderExecutor::active_stop_orders].
§Examples
use brokapi_bybit::limits::default_limits;
use brokapi_bybit::util::instrument_id;
use brokapi_bybit::{UserDataConfig, UserDataConnector};
use brokapi_core::{
ClientOrderId, Decimal, OrderExecutor, OrderType, PlaceOrderRequest, Price, Qty, ShutdownMode, Side, dec,
};
use brokapi_runtime::BrokerLimiter;
use tokio::sync::mpsc;
let (tx, _events) = mpsc::channel(256);
let ud = UserDataConnector::start(UserDataConfig::new(cfg, "bybit-ud"), BrokerLimiter::new(&default_limits()), tx).await?;
// Commands wait for the first reconcile.
ud.reconcile(&[]).await?;
let cid = ClientOrderId::new_v4();
let ack = ud
.place(PlaceOrderRequest {
cid,
instrument: instrument_id("MOCKUSDT", "linear"),
side: Side::Buy,
order_type: OrderType::Limit,
qty: Qty::from(10), // one step of the mock symbol
price: Some(Price::new(dec!(1.00))),
})
.await?;
assert_eq!(ack.cid, cid);
ud.cancel(cid).await?;
ud.shutdown(ShutdownMode::Immediate).await;Implementations§
Source§impl UserDataConnector
impl UserDataConnector
Sourcepub async fn start(
cfg: UserDataConfig,
limiter: Arc<BrokerLimiter>,
out: Sender<Envelope>,
) -> Result<Self, Error>
pub async fn start( cfg: UserDataConfig, limiter: Arc<BrokerLimiter>, out: Sender<Envelope>, ) -> Result<Self, Error>
Starts the connector: reads the clock of the exchange, checks the key pair with a read of the wallet, and opens
the private WebSocket session in the background. Commands wait for the first
reconcile.
§Errors
[Error::Config] when the client cannot be built or the exchange refuses the key pair (a key of the demo account
used on the main host, a wrong secret, a missing right, an IP not on the list of the key);
[Error::Transport] when the exchange cannot be reached.
Sourcepub fn raw(&self) -> BybitRaw
pub fn raw(&self) -> BybitRaw
Direct access to any API method with this connector’s key and rate limiter. See BybitRaw.
Sourcepub fn price_grid(&self) -> &PriceGrid
pub fn price_grid(&self) -> &PriceGrid
The price steps and quantity steps the connector checks orders against, on top of the rules it reads from the
exchange. Fill it with learn to be stricter.
Sourcepub fn caps(&self) -> UserDataCaps
pub fn caps(&self) -> UserDataCaps
What the connector can do.
place_idempotency:LabelOnly. The documentation says anorderLinkIdis unique and a second order with the same one is refused (code 110072), but how long the exchange remembers a closed order’s id is not stated and not checked on a live account. The library never repeats a place whatever the value.supports_replace: no. An amendment of the exchange keeps the client id of the order, and the sharedreplacepromises a new order under a new cid. The amendment is available throughrawandBybitHttpClient::amend_order.supports_stop_ordersandstreams_executions: yes.ws_rpcandordered_command_channel: no (commands go by HTTP; the exchange has a WebSocket trade channel that this connector does not use, and the demo contour has none).sandbox: yes on the demo contour (virtual money), no on the main one.
Trait Implementations§
Source§impl Connector for UserDataConnector
impl Connector for UserDataConnector
Source§fn caps(&self) -> UserDataCaps
fn caps(&self) -> UserDataCaps
Source§fn shutdown<'async_trait>(
self,
mode: ShutdownMode,
) -> Pin<Box<dyn Future<Output = ShutdownReport> + Send + 'async_trait>>where
Self: 'async_trait,
fn shutdown<'async_trait>(
self,
mode: ShutdownMode,
) -> Pin<Box<dyn Future<Output = ShutdownReport> + Send + 'async_trait>>where
Self: 'async_trait,
Error::Unknown], see [ShutdownReport]) and ends its tasks. Takes the connector by value, so it
cannot be used afterwards.