A high-performance WebSocket integration library for streaming public market data. Used as a key dependency of the `barter-rs` project.

Related tags

Command-line barter
Overview

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!

See: Barter, Barter-Integration, Barter-Execution & Barter-Macro

Crates.io MIT licensed Build Status Discord chat

API Documentation | Chat

Overview

Barter-Data is a high-performance WebSocket integration library for streaming public market data from leading cryptocurrency exchanges. It presents an easy-to-use and extensible set of interfaces that can deliver normalised exchange data in real-time.

From a user perspective, the major component is the StreamBuilder structures that assists in initialising an arbitrary number of exchange MarketStreams using input Subscriptions. Simply build your dream set of MarketStreams and Barter-Data will do the rest!

Supported Exchange Subscriptions

Exchange Constructor Code InstrumentKinds SubKinds
BinanceSpot BinanceSpot::default() Spot PublicTrades
OrderBooksL1
OrderBooksL2
BinanceFuturesUsd BinanceFuturesUsd::default() FuturePerpetual PublicTrades
OrderBooksL1
OrderBooksL2
Bitfinex Bitfinex Spot PublicTrades
Coinbase Coinbase Spot PublicTrades
GateioSpot GateioSpot::default() Spot PublicTrades
GateioFuturesUsd GateioFuturesUsd::default() FuturePerpetual PublicTrades
GateioFuturesBtc GateioFuturesBtc::default() FuturePerpetual PublicTrades
Kraken Kraken Spot PublicTrades
OrderBooksL1
Okx Okx Spot
FuturePerpetual
PublicTrades

Examples

See barter-data-rs/examples for a more comprehensive selection of examples!

Multi Exchange Public Trades

use barter_data::{
    exchange::{
        binance::{futures::BinanceFuturesUsd, spot::BinanceSpot},
        coinbase::Coinbase,
        gateio::spot::GateioSpot,
        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::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:?}");
    }
}

Getting Help

Firstly, see if the answer to your question can be found in the API Documentation. If the answer is not there, I'd be happy to help via Chat and try answer your question via Discord.

Contributing

Thanks in advance for helping to develop the Barter ecosystem! Please do get hesitate to get touch via the Discord Chat to discuss development, new features, and the future roadmap.

Adding A New Exchange Connector

  1. Add a new Connector trait implementation in src/exchange/<exchange_name>.mod.rs (eg/ see exchange::okx::Okx).
  2. Follow on from "Adding A New Subscription Kind For An Existing Exchange Connector" below!

Adding A New Subscription Kind For An Existing Exchange Connector

  1. Add a new SubKind trait implementation in src/subscription/<sub_kind_name>.rs (eg/ see subscription::trade::PublicTrades).
  2. Define the SubKind::Event data model (eg/ see subscription::trade::PublicTrade).
  3. Define the MarketStream type the exchange Connector will initialise for the new SubKind:
    ie/ impl StreamSelector<SubKind> for <ExistingExchangeConnector> { ... }
  4. Try to compile and follow the remaining steps!
  5. Add a barter-data-rs/examples/<sub_kind_name>_streams.rs example in the standard format :)

Related Projects

In addition to the Barter-Execution crate, the Barter project also maintains:

  • Barter: High-performance, extensible & modular trading components with batteries-included. Contains a pre-built trading Engine that can serve as a live-trading or backtesting system.
  • Barter-Integration: High-performance, low-level framework for composing flexible web integrations.
  • Barter-Execution: High-performance WebSocket integration library for streaming public market data from leading cryptocurrency exchanges.
  • Barter-Macro: Barter ecosystem macros.

Roadmap

  • Add support for more exchanges (easy to help with!)
  • Add support for more subscription kinds (easy to help with!)

Licence

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Barter-Data by you, shall be licensed as MIT, without any additional terms or conditions.

Comments
  • Confusing variable name and inappropriate value in subscription::OrderBook::mid_price

    Confusing variable name and inappropriate value in subscription::OrderBook::mid_price

    Hi, dear maintainers. Thanks for the great project! I think I have found some problem in the implementation of mid_price and volume_weighed_mid_price, but I am new to rust, please correct me if I am wrong.

      pub fn mid_price(&self) -> f64 {
            match (self.bids.levels.first(), self.asks.levels.first()) {
                (Some(best_bid), Some(best_ask)) => mid_price(best_bid.price, best_ask.price),
                (Some(best_bid), None) => best_bid.price,
                (None, Some(best_bid)) => best_bid.price,
                (None, None) => 0.0,
            }
    

    Firstly, IIUC, the third arm in the match block, (None, Some(best_bid)), is intended to match the case where only best ask is available and no bids. The matched value should be named best_ask instead of best_bid. The whole arm should be (None, Some(best_ask)) => best_ask.price,

    Secondly, for the (None, None) case. It may not be a good idea to use 0.0 as a default value. price = 0.0 has its meaning in trading: the instrument is for free. In practical use cases, the mid price of order book is typically used to indicate the "fair price" for the associated instrument. There are no quoting does not necessarily mean the instrument has zero price. Moreover, it is dangerous to set an instrument's price as 0.0, especially when some selling signal is generated based on this price, which results in extremely low price as the order price, and may lead to trading loss.

    From my experience, I would rather use nan as mid price in the empty order book case. But I don't know whether rust supports nan. Another option is to use None, but that requires changing the return type to Option<f64>

    opened by HereticSK 4
  • Build error

    Build error

    I'm trying to build the example https://github.com/barter-rs/barter-rs#example and I'm getting a build error:

       Compiling barter-data v0.5.7
    error[E0599]: no variant named `Serde` found for enum `SocketError`
      --> /Users/dave/.cargo/registry/src/github.com-1ecc6299db9ec823/barter-data-0.5.7/src/exchange/kraken/model.rs:65:43
       |
    65 |             .map_err(|error| SocketError::Serde {
       |                                           ^^^^^ variant not found in `SocketError`
    
    For more information about this error, try `rustc --explain E0599`.
    error: could not compile `barter-data` due to previous error
    warning: build failed, waiting for other jobs to finish...```
    opened by dclausen 1
  • Add Kraken L1 support

    Add Kraken L1 support

    • Includes new example at examples/order_books_l1_streams_multi_exchange.rs (also added to main lib docs)
    • Refactored Kraken messages to use generic enum
    • Update README
    opened by rdong8 0
  • Feature/kucoin ob

    Feature/kucoin ob

    This depends on feature/kucoin_spot

    Changes:

    • Change SubKind::Orderbook to SubKind::L2OrderBookSnapshot(SnapshotDepth)
    • Implement the orderbook snapshots for kucoin (https://docs.kucoin.com/#level2-5-best-ask-bid-orders)
    pr::feat 
    opened by Shayansalesi 0
  • Add Liquidation subscription support for Binance

    Add Liquidation subscription support for Binance

    This PR adds support for the user to subscribe to the liquidation websocket stream on Binance and also add a base for adding liquidation support to other exchanges

    pr::feat 
    opened by Crauzer 0
  • Exchange_time and received_time are the same

    Exchange_time and received_time are the same

    Hi! I am running this example: https://github.com/barter-rs/barter-data-rs/blob/develop/examples/public_trades_streams.rs

    Unfortunately I have rather doubtful results

    MarketEvent<OrderBookL1>: MarketEvent { exchange_time: 2023-02-06T17:52:17.056798Z, received_time: 2023-02-06T17:52:17.056798Z, exchange: binance_futures_usd, instrument: Instrument { base: btc, quote: usdt, kind: FuturePerpetual }, kind: OrderBookL1 { last_update_time: 2023-02-06T17:52:17.056798Z, best_bid: Level { price: 23041.9, amount: 0.205 }, best_ask: Level { price: 23042.0, amount: 17.663 } } }

    Exchange and receive time are the same, I doubt that it is due to hyper fast benefits of this lib, any suggestions? Is this a bug?

    opened by yolonir 0
  • Metatrader Exchange API

    Metatrader Exchange API

    It'd be neat to be able to use Metatrader, because they are broker independant. They already have a ton of implementations for other exchanges, which would make this very useful.

    Backtrader already implemented this here by using the MQL5-JSON-API.

    opened by not-matthias 3
  • `u64` or `DateTime<Utc>` for the data type of timestamps

    `u64` or `DateTime` for the data type of timestamps

    Would it be better to use u64 datatype for timestamps because of better performance when serde deserializes it?

    Candle uses DateTime<Utc> for start_time and end_time for now.

    https://github.com/barter-rs/barter-data-rs/blob/f1afec3308e3351e3c155946c9f357be158fd0a5/src/model/mod.rs#L42-L53

    opened by seongs1024 2
  • Can China Futures CTP interface be supported

    Can China Futures CTP interface be supported

    Similar implementations include vnpy, which supports many exchanges, as well as China's futures market and stock market. Unfortunately, the efficiency of the Python based framework is not very satisfactory. I have been looking for a Rust based framework. I was lucky to find Barter, but only support crypto exchanges.

    Development Documentation:CTP Client Development Guide.pdf CTP_Client_Development_Guide.pdf

    c++ code & dll is:v6.6.8_traderapi.zip (I found the trust encapsulation of the ctp interface, but I don't know how to combine it with barter:https://github.com/WiSaGaN/ctp-rs)

    test account is :https://www.simnow.com.cn/ (MarketPrice not need account)

    issue::question 
    opened by kingmo888 3
Owner
Barter
Open-source Rust algorithmic trading framework.
Barter
Databento Binary Encoding (DBZ) - Fast message encoding and storage format for market data

dbz A library (dbz-lib) and CLI tool (dbz-cli) for working with Databento Binary Encoding (DBZ) files. Python bindings for dbz-lib are provided in the

Databento, Inc. 15 Nov 4, 2022
A web-based streaming service with improved privacy, performance, and simplicity.

Majiix Majiix is a web-based open-source streaming service. The backend that handles the core streaming logic is written in Rust and makes use of cutt

Majgix 3 Nov 21, 2023
A modern high-performance open source file analysis library for automating localization tasks

?? Filecount Filecount is a modern high-performance open source file analysis library for automating localization tasks. It enables you to add file an

Babblebase 4 Nov 11, 2022
A high-performance Rust library designed to seamlessly integrate with the Discord API.

Rucord - Rust Library for Discord API Interactions Note: This library is currently under development and is not yet recommended for production use. Ov

Coders' Collab 4 Feb 26, 2023
High-performance Javascript color gradient library powered by Rust + WebAssembly

colorgrad-js High-performance Javascript color gradient library powered by Rust + WebAssembly. No dependencies. Faster than d3-scale, chroma-js, culor

Nor Khasyatillah 168 Apr 25, 2023
Schemars is a high-performance Python serialization library, leveraging Rust and PyO3 for efficient handling of complex objects

Schemars Introduction Schemars is a Python package, written in Rust and leveraging PyO3, designed for efficient and flexible serialization of Python c

Michael Gendy 7 Nov 21, 2023
A lightweight, zero-dependency struct diffing library which allows changed fields to be collected and applied

structdiff A lightweight, zero-dependency struct diffing library which allows changed fields to be collected and applied. Derive Difference on a struc

null 7 Dec 25, 2022
zero-dependency 3d math library in rust

dualquat A lightweight, zero-dependency 3d math library for use in Dual Quaternion based physics simulation. Capable of representing and transforming

null 4 Nov 11, 2022
Dynamic dependency injection library for rust.

DDI (dynamic dependency injection) This library provides a generic dependency injection container that can be easily integrated into any application a

EYHN 34 Feb 21, 2023
A template for starting a dioxus project to be used with dioxus-cli

A template for starting a dioxus project to be used with dioxus-cli

Dioxus 6 Nov 25, 2022
A simple demo to bind a TCP port with k8s exec channel.k8s.io websocket API.

k8s-webterm-connector Tired with web terminals? Let's use it with CLI! This is just a simple demo to bind a TCP port with k8s exec websocket API, whic

Chielo 4 Aug 11, 2023
High-performance and normalised trading interface capable of executing across many financial venues

High-performance and normalised trading interface capable of executing across many financial venues. Also provides a feature rich simulated exchange to assist with backtesting and dry-trading.

Barter 7 Dec 28, 2022
High-performance asynchronous computation framework for system simulation

Asynchronix A high-performance asynchronous computation framework for system simulation. What is this? Warning: this page is at the moment mostly addr

Asynchronics 7 Dec 7, 2022
High-performance, low-level framework for composing flexible web integrations

High-performance, low-level framework for composing flexible web integrations. Used mainly as a dependency of `barter-rs` project

Barter 8 Dec 28, 2022
High performance wlroots screen recording, featuring hardware encoding

wl-screenrec High performance wlroots based screen recorder. Uses dma-buf transfers to get surface, and uses the GPU to do both the pixel format conve

Russell Greene 32 Jan 21, 2023
The Amp programming language: a language designed for building high performance systems.

A language designed for building high performance systems. Platform Support x86_64-pc-windows ✅ x86_64-unknown-linux ⚠️ untested x86_64-unknown-darwin

The Amp Programming Language 5 Mar 17, 2023
Rust in Anger: high-performance web applications

Rust in Anger: Book demo This is the code repository that accompanies the Rust in Anger blog post. The following folders each come with their own buil

EqualTo 26 Apr 9, 2023
Local-first high performance codebase index engine designed for AI

CodeIndex CodeIndex is a local-first high performance codebase index engine designed for AI. It helps your LLM understand the structure and semantics

Jipiti AI 9 Aug 30, 2023