Crate barter_data

Source
Expand description

§Barter-Data

A high-performance WebSocket integration library for streaming public market data from leading cryptocurrency exchanges - batteries included. It is:

  • Easy: Barter-Data’s simple StreamBuilder and DynamicStreams interface allows for easy & quick setup (see example below and /examples!).
  • Normalised: Barter-Data’s unified interface for consuming public WebSocket data means every Exchange returns a normalised data model.
  • Real-Time: Barter-Data utilises real-time WebSocket integrations enabling the consumption of normalised tick-by-tick data.
  • Extensible: Barter-Data is highly extensible, and therefore easy to contribute to with coding new integrations!

§User API

§Examples

For a comprehensive collection of examples, see the /examples directory.

§Multi Exchange Public Trades

use barter_data::{
    exchange::{
        gateio::spot::GateioSpot,
        binance::{futures::BinanceFuturesUsd, spot::BinanceSpot},
        coinbase::Coinbase,
        okx::Okx,
    },
    streams::{Streams, reconnect::stream::ReconnectingStream},
    subscription::trade::PublicTrades,
};
use barter_instrument::instrument::market_data::kind::MarketDataInstrumentKind;
use futures::StreamExt;
use tracing::warn;

#[tokio::main]
async fn main() {
    // Initialise PublicTrades Streams for various exchanges
    // '--> each call to StreamBuilder::subscribe() initialises a separate WebSocket connection

    let streams = Streams::<PublicTrades>::builder()
        .subscribe([
            (BinanceSpot::default(), "btc", "usdt", MarketDataInstrumentKind::Spot, PublicTrades),
            (BinanceSpot::default(), "eth", "usdt", MarketDataInstrumentKind::Spot, PublicTrades),
        ])
        .subscribe([
            (BinanceFuturesUsd::default(), "btc", "usdt", MarketDataInstrumentKind::Perpetual, PublicTrades),
            (BinanceFuturesUsd::default(), "eth", "usdt", MarketDataInstrumentKind::Perpetual, PublicTrades),
        ])
        .subscribe([
            (Coinbase, "btc", "usd", MarketDataInstrumentKind::Spot, PublicTrades),
            (Coinbase, "eth", "usd", MarketDataInstrumentKind::Spot, PublicTrades),
        ])
        .subscribe([
            (GateioSpot::default(), "btc", "usdt", MarketDataInstrumentKind::Spot, PublicTrades),
            (GateioSpot::default(), "eth", "usdt", MarketDataInstrumentKind::Spot, PublicTrades),
        ])
        .subscribe([
            (Okx, "btc", "usdt", MarketDataInstrumentKind::Spot, PublicTrades),
            (Okx, "eth", "usdt", MarketDataInstrumentKind::Spot, PublicTrades),
            (Okx, "btc", "usdt", MarketDataInstrumentKind::Perpetual, PublicTrades),
            (Okx, "eth", "usdt", MarketDataInstrumentKind::Perpetual, PublicTrades),
       ])
        .init()
        .await
        .unwrap();

    // Select and merge every exchange Stream using futures_util::stream::select_all
    // Note: use `Streams.select(ExchangeId)` to interact with individual exchange streams!
    let mut joined_stream = streams
        .select_all()
        .with_error_handler(|error| warn!(?error, "MarketStream generated error"));

    while let Some(event) = joined_stream.next().await {
        println!("{event:?}");
    }
}

Modules§

books
OrderBook related types, and utilities for initialising and maintaining a collection of sorted local Instrument OrderBooks
error
All Errors generated in Barter-Data.
event
Defines the generic MarketEvent<T> used in every MarketStream.
exchange
Connector implementations for each exchange.
instrument
InstrumentData trait for instrument describing data.
streams
High-level API types used for building MarketStreams from collections of Barter Subscriptions.
subscriber
Subscriber, SubscriptionMapper and SubscriptionValidator traits that define how a Connector will subscribe to exchange MarketStreams.
subscription
Types that communicate the type of each MarketStream to initialise, and what normalised Barter output type the exchange will be transformed into.
test_utils
transformer
Generic ExchangeTransformer implementations used by MarketStreams to translate exchange specific types to normalised Barter types.

Structs§

NoInitialSnapshots
Implementation of SnapshotFetcher that does not fetch any initial market data snapshots. Often used for stateless MarketStreams, such as public trades.

Traits§

Identifier
Defines a generic identification type for the implementor.
MarketStream
Stream that yields Market<Kind> events. The type of Market<Kind> depends on the provided SubscriptionKind of the passed Subscriptions.
SnapshotFetcher
Defines how to fetch market data snapshots for a collection of Subscriptions.

Functions§

distribute_messages_to_exchange
Transmit WsMessages sent from the ExchangeTransformer to the exchange via the WsSink.
process_buffered_events
schedule_pings_to_exchange
Schedule the sending of custom application-level ping WsMessages to the exchange using the provided PingInterval.

Type Aliases§

ExchangeWsStream
Convenient type alias for an ExchangeStream utilising a tungstenite WebSocket.