Trait crypto_ws_client::WSClient [−][src]
pub trait WSClient<'a> {
fn new(
on_msg: Arc<Mutex<dyn FnMut(String) + Send + 'a>>,
url: Option<&str>
) -> Self;
fn subscribe_trade(&self, pairs: &[String]);
fn subscribe_bbo(&self, pairs: &[String]);
fn subscribe_orderbook(&self, pairs: &[String]);
fn subscribe_orderbook_topk(&self, pairs: &[String]);
fn subscribe_ticker(&self, pairs: &[String]);
fn subscribe_candlestick(&self, symbol_interval_list: &[(String, usize)]);
fn subscribe(&self, raw_channels: &[String]);
fn unsubscribe(&self, raw_channels: &[String]);
fn run(&self, duration: Option<u64>);
fn close(&self);
}
Expand description
The public interface of every WebSocket client.
Required methods
Creates a new client.
Arguments
on_msg
- A callback function to process original JSON messagesurl
- Optional server url, usually you don’t need specify it
fn subscribe_trade(&self, pairs: &[String])
fn subscribe_trade(&self, pairs: &[String])
Subscribes to trade channels.
A trade channel sends tick-by-tick trade data, which is the complete copy of a market’s trading information.
Each exchange has its own pair formats, for example:
- BitMEX
XBTUSD
,XBTM21
- Binance
btcusdt
,btcusd_perp
- OKEx
BTC-USDT
fn subscribe_bbo(&self, pairs: &[String])
fn subscribe_bbo(&self, pairs: &[String])
Subscribes to BBO(best bid & offer) channels.
BBO represents best bid and offer, which is also refered to as level1 data. It is the top 1 bid and ask from the orginal orderbook, so BBO is updated per tick and non-aggregated.
Not all exchanges have the BBO channel, calling this function with these exchanges will panic.
- Binance, BitMEX, Huobi and Kraken have BBO directly.
- Bitfinex uses
book
channel withlen=1
andprec="R0"
to get BBO data.
fn subscribe_orderbook(&self, pairs: &[String])
fn subscribe_orderbook(&self, pairs: &[String])
Subscribes to incremental level2 orderbook channels.
An incremental level2 orderbook channel sends a snapshot followed by tick-by-tick updates.
Level2 orderbook is the raw orderbook(Level3) aggregated by price level, it is also refered to as “market by price level” data.
This function subscribes to exchange specific channels as the following:
- Binance
depth
- Bitfinex
book
channel withprec=P0
,frec=F0
andlen=25
- BitMEX
orderBookL2_25
- Bitstamp
diff_order_book
, top 100 - CoinbasePro
level2
- Huobi
depth.size_20.high_freq
withdata_type=incremental
for contracts,mbp.20
for Spot - Kraken
book
withdepth=25
- MXC
depth
for Swap,symbol
for Spot - OKEx
depth_l2_tbt
, top 100
fn subscribe_orderbook_topk(&self, pairs: &[String])
fn subscribe_orderbook_topk(&self, pairs: &[String])
Subscribes to level2 orderbook snapshot channels.
A level2 orderbook snapshot channel sends a complete snapshot every interval.
This function subscribes to exchange specific channels as the following:
- Binance
depth5
, every 1000ms - Bitfinex has no snapshot channel
- BitMEX
orderBook10
, top 10, every tick - Bitstamp
order_book
, top 10, every 100ms - CoinbasePro has no snapshot channel
- Huobi
depth.step1
anddepth.step7
, top 20, every 1s - Kraken has no snapshot channel
- MXC
depth.full
for Swap, top 20, every 100ms;get.depth
for Spot, full, every 26s - OKEx
depth5
, top 5, every 100ms
fn subscribe_ticker(&self, pairs: &[String])
fn subscribe_ticker(&self, pairs: &[String])
Subscribes to ticker channels.
A ticker channel pushes realtime 24hr rolling window ticker messages, which contains OHLCV information.
Not all exchanges have the ticker channel, for example, BitMEX, Bitstamp, MXC Spot, etc.
Subscribes to candlestick channels.
The candlestick channel sends OHLCV messages at interval.
symbol_interval_list
is a list of symbols and intervals of candlesticks in seconds.
Not all exchanges have candlestick channels, for example, Bitstamp and CoinbasePro.
Subscribes to raw channels, lower level API.
A raw_channel
can be:
- A plain string, supported by Binance, BitMEX, Bitstamp, Bybit, Deribit, Huobi, OKEx, ZBG.
For example, Binance
btcusdt@aggTrade
, BitMEXtrade:XBTUSD,
instrument, Bitstamp
live_trades_btcusd, Huobi
market.btcusdt.trade.detail,
market.overview, OKEx
spot/trade:BTC-USDT`. - channel:pair. For exchanges not supporting plain string channels,
this library will split this kind of
raw_channel
by:
, then compose a JSON string. For example, Bitfinextrades:tBTCUST
, CoinbasePromatches:BTC-USD
, Krakentrade:XBT/USD
- A JSON string, supported by all exchanges. If a
raw_channel
starts with{
, which means it is the final JSON string, thus it will be sent out directly without parsing.
fn unsubscribe(&self, raw_channels: &[String])
fn unsubscribe(&self, raw_channels: &[String])
Unsubscribes from raw channels, lower level API.
Starts the infinite loop until time is up or the server closes the connection.
Arguments
duration
- How many seconds to run, None means infinite.