#exchange #trading #cryptocurrency #binance #api-bindings #historical-data

binance-history

Downloading and parsing historical data from the Binance exchange

1 unstable release

0.1.0 Mar 8, 2023

#20 in #binance

MIT/Apache

26KB
677 lines

Binance history

Downloading and parsing historical data from the Binance exchange

Installation

Add the following line to your Cargo.toml file:

binance-history = "0.1.0"

Or for the latest github version:

binance-history = { git = "https://github.com/K0nstantini/binance-history" }

Usage

use binance_history::{COINMKlines, SpotTrades, USDMAggTrades};

#[tokio::main]
async fn main() {
    let symbol = "BTCUSDT";
    let path = "csv";
    let from = "2023-02-01 00:00:00";
    let to = "2023-02-02 23:59:59";

    let spot_trades: Vec<SpotTrades> = binance_history::get(symbol, None, from, to, path).await.unwrap();
    let usdm_agg_trades: Vec<USDMAggTrades> = binance_history::get(symbol, None, from, to, path).await.unwrap();
    let coinm_klines: Vec<COINMKlines> = binance_history::get("BTCUSD_PERP", Some("1h"), from, to, path).await.unwrap();

    assert!(!spot_trades.is_empty());
    assert!(!usdm_agg_trades.is_empty());
    assert!(!coinm_klines.is_empty());
}

Using custom struct

use binance_history::{BinanceData, DataType, MarketType};
use serde::Deserialize;

#[derive(Deserialize)]
struct Prices {
    time: i64,
    price: f64,
}

impl BinanceData for Prices {
    fn types() -> (MarketType, DataType) {
        (MarketType::USDM, DataType::Trades)
    }

    fn time(&self) -> i64 {
        self.time
    }
}

#[tokio::main]
async fn main() {
    let symbol = "ETHUSDT";
    let path = "csv";
    let from = "2023-02-01 00:00:00";
    let to = "2023-02-02 23:59:59";

    let prices: Vec<Prices> = binance_history::get(symbol, None, from, to, path).await.unwrap();
    assert!(!prices.is_empty());
}

Dependencies

~13–28MB
~410K SLoC