Git Product home page Git Product logo

binance_connect's Introduction

Roadmap

  • USD-M Futures WebSocket โœ…
  • USD-M Futures Rest Api
  • SpotMarket WebSocket
  • SpotMarket Rest Api
  • Coin-M Futures WebSocket
  • Coin-M Futures Rest Api

binance_connect::futures_usd

Features

  • Connects to the Binance USD-M Futures WebSocket to receive real-time market and account updates via an event consumer.
  • Events that are consumed contain a (sanitized) struct representation of the returned Binance data.
  • ListenKey creation and keep-alive is managed by the library.
  • WebSocket connection drops are caught and managed by the library. This because Binance forcefully drops connections after the 24h mark. This can be configured in the FuturesWebSocketConfig using the reconnect(bool) setter (default setting is true).

Getting Started

Without config

Create an instance of the FuturesUsdStream and add the desired streams for real-time updates. In this example, we're subscribing to the book depth for the BTC/USDT trading pair and all book tickers.

let fus: FuturesUsdStream = FuturesUsdStream::default()
    .with_book_depth("btcusdt", BookDepthUpdateSpeed::Millis500)
    .with_book_tickers()
    .start();

With config

Set up your Binance API authentication using the ApiAuth struct. This is only required when requesting User Data Streams.

let api_auth: ApiAuth = ApiAuth::new(
    "YOUR_API_KEY".to_string(),
    "YOUR_API_SECRET".to_string(),
);

A FuturesWebsocketConfig can be created to pass configuration options to the FuturesUsdStream. For example; You can specify whether to use the Binance testnet or the live environment. When an authenticated connection is required the FuturesWebsocketConfig is mandatory.

let config: FuturesWebSocketConfig = FuturesWebSocketConfig::with_config(config)
    .with_api_auth(api_auth)
    .use_testnet(); // Use the testnet environment (remove for live trading)

Create an instance of the FuturesUsdStream using the with_config() builder and add the desired streams for real-time updates. In this example, we're subscribing to the book depth for the BTC/USDT trading pair, all book tickers and because the with_api_auth method is called on the FuturesWebsocketConfig all User Data Streams.

let fus: FuturesUsdStream = FuturesUsdStream::with_config(config)
    .with_book_depth("btcusdt", BookDepthUpdateSpeed::Millis500)
    .with_book_tickers()
    .start();

Consuming Events

Start consuming events from the FuturesUsdStream using the consume() method and handle them as needed.

for event in fus.consume() {
    match event {
        BookDepthEvent(book_depth) => {
            println!("{:?}", book_depth)
        }
        BookTickersEvent(book_tickers) => {
            for book_ticker in book_tickers.data {
                println!("{:?}", book_ticker)
            }
        }
       // Only available with `with_api_auth(api_auth)` called on `FuturesWebsocketConfig` 
        AccountUpdateEvent(account_update) => {
            println!("{:?}", account_update)
        }
        _ => {}
    }
}

The structs that are returned when consuming Events follow a predictable property name convention as opposed to the single letter convention used by Binance. Property values can be an Enum. See src/futures_usd/enums/binance.rs

BookTicker response struct as example reference. See src/futures_usd/response.rs

pub struct BookTicker {
    pub event_type: EventType,
    pub event_time: u64,
    pub symbol: String,
    pub update_id: u64,
    pub bid_price: f64,
    pub bid_quantity: f64,
    pub ask_price: f64,
    pub ask_quantity: f64,
    pub transaction_time: u64,
}

Events

 /* MARKET_DATA */
 BookTickerEvent(BookTicker),
 BookTickersEvent(BookTickers),
 AggTradeEvent(AggTrade),
 MarkPriceUpdateEvent(MarkPriceUpdate),
 MarkPriceUpdatesEvent(MarkPriceUpdates),
 KlineEvent(Kline),
 ContinuousKlineEvent(ContinuousKline),
 MiniTickerEvent(MiniTicker),
 MiniTickersEvent(MiniTickers),
 TickerEvent(Ticker),
 TickersEvent(Tickers),
 ForceOrderEvent(ForceOrder),
 BookDepthEvent(BookDepth),
 CompositeIndexEvent(CompositeIndex),
 ContractInfoEvent(ContractInfo),
 AssetIndexUpdateEvent(AssetIndexUpdate),
 AssetIndexUpdatesEvent(AssetIndexUpdates),
 /* USER_DATA */
 OrderTradeUpdateEvent(OrderTradeUpdate),
 AccountUpdateEvent(AccountUpdate),
 MarginCallEvent(MarginCall),
 AccountConfigUpdateEvent(AccountConfigUpdate),
 StrategyUpdateEvent(StrategyUpdate),
 GridUpdateEvent(GridUpdate),
 ConditionalOrderTriggerRejectEvent(ConditionalOrderTriggerReject),
 /* SYSTEM */
 SubscribeResponseEvent,

Errors

All errors are propagated to a BinanceConnectError. See src/error.rs

License

This project is licensed under the MIT License. See the LICENSE file for details.

binance_connect's People

Contributors

erik404 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.