Skip to main content

MarketDataConnector

Struct MarketDataConnector 

pub struct MarketDataConnector { /* private fields */ }
Expand description

The market data connector of Bybit: implements [OrderBookSource], [MarketDataSource] and [InstrumentSource].

It is not tied to an account: the public data needs no key. Reads go by HTTP, streams come over one WebSocket session that reconnects on its own and restores its subscriptions. The streams are the public ones of the category (wss://stream.bybit.com/v5/public/linear): the demo contour has no public streams of its own, so a connector made from a demo config reads the same market as one made from a main config.

Instruments are named by the symbol and the category: BTCUSDT@linear (instrument_id).

§Examples

use brokapi_bybit::limits::default_limits;
use brokapi_bybit::util::instrument_id;
use brokapi_bybit::{MarketDataConfig, MarketDataConnector};
use brokapi_core::{OrderBook, OrderBookSource, ShutdownMode};
use brokapi_runtime::BrokerLimiter;

let (tx, _events) = tokio::sync::mpsc::channel(256);
let md = MarketDataConnector::start(MarketDataConfig::new(cfg, "bybit-md"), BrokerLimiter::new(&default_limits()), tx).await?;
let OrderBook::Snapshot { bids, asks, .. } = md.order_book(&instrument_id("MOCKUSDT", "linear"), 5).await? else {
    panic!("a read answers a snapshot");
};
assert!(bids[0].price < asks[0].price);
md.shutdown(ShutdownMode::Immediate).await;

Implementations§

§

impl MarketDataConnector

pub async fn start( cfg: MarketDataConfig, limiter: Arc<BrokerLimiter>, out: Sender<Envelope>, ) -> Result<MarketDataConnector, Error>

Starts the connector: reads the clock of the exchange and opens the WebSocket session in the background.

§Errors

[Error::Config] when the client cannot be built; [Error::Transport] when the exchange cannot be reached.

pub fn raw(&self) -> BybitRaw

Direct access to any API method with this connector’s key and rate limiter. See BybitRaw.

pub fn caps(&self) -> MarketDataCaps

What the connector can do: books up to 1000 levels (the stream sends the whole book after every change, not increments), and all four streams.

pub async fn shutdown(self, mode: ShutdownMode) -> ShutdownReport

Stops the connector. Unless the mode is Immediate it cancels the streams first; Graceful also lets the bot’s channel take what is still buffered.

Trait Implementations§

§

impl Connector for MarketDataConnector

§

type Caps = MarketDataCaps

The type that describes what this connector can do.
§

fn caps(&self) -> MarketDataCaps

What this connector can do. Read it before you use a feature that not every broker has.
§

fn shutdown<'async_trait>( self, mode: ShutdownMode, ) -> Pin<Box<dyn Future<Output = ShutdownReport> + Send + 'async_trait>>
where MarketDataConnector: 'async_trait,

Stops the connector: closes its streams, gives up commands that have no answer yet (they become [Error::Unknown], see [ShutdownReport]) and ends its tasks. Takes the connector by value, so it cannot be used afterwards.
§

impl InstrumentSource for MarketDataConnector

§

fn find<'life0, 'async_trait>( &'life0 self, q: InstrumentQuery, ) -> Pin<Box<dyn Future<Output = Result<Vec<Instrument>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, MarketDataConnector: 'async_trait,

Looks instruments up in the directory. Read more
§

fn get<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, ) -> Pin<Box<dyn Future<Output = Result<Instrument, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads one directory entry. Read more
§

fn trading_params<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, ) -> Pin<Box<dyn Future<Output = Result<TradingParams, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads lot size, price step and the other numbers needed to size and price an order. Read more
§

fn trading_status<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, ) -> Pin<Box<dyn Future<Output = Result<TradingStatus, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads whether the instrument can be traded now. Read more
§

fn schedule<'life0, 'life1, 'async_trait>( &'life0 self, _exchange: &'life1 str, _range: TimeRange, ) -> Pin<Box<dyn Future<Output = Result<Schedule, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads the trading sessions of an exchange over range. Read more
§

impl MarketDataSource for MarketDataConnector

§

fn last_trades<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, limit: u32, ) -> Pin<Box<dyn Future<Output = Result<Vec<MarketTrade>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads the newest limit deals of the public tape. Read more
§

fn subscribe_market_trades<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, opts: SubscribeOpts, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Starts a stream of the public deals of one instrument. Read more
§

fn subscribe_quotes<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, opts: SubscribeOpts, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Starts a stream of the best bid and ask of one instrument. Read more
§

fn candles<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, tf: Timeframe, range: TimeRange, ) -> Pin<Box<dyn Future<Output = Result<Vec<Candle>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads the bars of tf that fall in range. Read more
§

fn candles_max_span(&self, tf: Timeframe) -> Option<Duration>

The longest span of time one candles request may cover for tf, or None when the connector does not know a limit. Read more
§

fn subscribe_candles<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, tf: Timeframe, opts: SubscribeOpts, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Starts a stream of the latest bar of tf for one instrument. Read more
§

impl OrderBookSource for MarketDataConnector

§

fn order_book<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, depth: u32, ) -> Pin<Box<dyn Future<Output = Result<OrderBook, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Reads the order book once, to depth levels per side. Read more
§

fn subscribe_order_book<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 InstrumentId, opts: SubscribeOpts, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, MarketDataConnector: 'async_trait,

Starts a stream of the order book of one instrument. Read more
§

fn unsubscribe<'life0, 'async_trait>( &'life0 self, sub: SubscriptionId, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, MarketDataConnector: 'async_trait,

Stops a subscription of this connector. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more