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 interface allows for easy & quick setup (see example below!).
  • 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;
use barter_data::{
    exchange::{
        binance::{futures::BinanceFuturesUsd, spot::BinanceSpot},
        coinbase::Coinbase,
        okx::Okx,
    },
    streams::Streams,
    subscription::trade::PublicTrades,
};
use barter_integration::model::InstrumentKind;
use futures::StreamExt;

#[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", InstrumentKind::Spot, PublicTrades),
            (BinanceSpot::default(), "eth", "usdt", InstrumentKind::Spot, PublicTrades),
        ])
        .subscribe([
            (BinanceFuturesUsd::default(), "btc", "usdt", InstrumentKind::FuturePerpetual, PublicTrades),
            (BinanceFuturesUsd::default(), "eth", "usdt", InstrumentKind::FuturePerpetual, PublicTrades),
        ])
        .subscribe([
            (Coinbase, "btc", "usd", InstrumentKind::Spot, PublicTrades),
            (Coinbase, "eth", "usd", InstrumentKind::Spot, PublicTrades),
        ])
        .subscribe([
            (GateioSpot::default(), "btc", "usdt", InstrumentKind::Spot, PublicTrades),
            (GateioSpot::default(), "eth", "usdt", InstrumentKind::Spot, PublicTrades),
        ])
        .subscribe([
            (Okx, "btc", "usdt", InstrumentKind::Spot, PublicTrades),
            (Okx, "eth", "usdt", InstrumentKind::Spot, PublicTrades),
            (Okx, "btc", "usdt", InstrumentKind::FuturePerpetual, PublicTrades),
            (Okx, "eth", "usdt", InstrumentKind::FuturePerpetual, PublicTrades),
       ])
        .init()
        .await
        .unwrap();

    // Join all exchange PublicTrades streams into a single tokio_stream::StreamMap
    // Notes:
    //  - Use `streams.select(ExchangeId)` to interact with the individual exchange streams!
    //  - Use `streams.join()` to join all exchange streams into a single mpsc::UnboundedReceiver!
    let mut joined_stream = streams.join_map().await;

    while let Some((exchange, trade)) = joined_stream.next().await {
        println!("Exchange: {exchange}, Market<PublicTrade>: {trade:?}");
    }
}

Modules

All Errors generated in Barter-Data.
Defines the generic MarketEvent<T> used in every MarketStream.
Connector implementations for each exchange.
High-level API types used for building MarketStreams from collections of Barter Subscriptions.
Subscriber, SubscriptionMapper and SubscriptionValidator traits that define how a Connector will subscribe to exchange MarketStreams.
Types that communicate the type of each MarketStream to initialise, and what normalised Barter output type the exchange will be transformed into.
Generic ExchangeTransformer implementations used by MarketStreams to translate exchange specific types to normalised Barter types.

Traits

Defines a generic identification type for the implementor.
[Stream] that yields Market<Kind> events. The type of Market<Kind> depends on the provided SubKind of the passed Subscriptions.

Functions

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

Type Definitions

Convenient type alias for an ExchangeStream utilising a tungstenite WebSocket.