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

binance-history

Downloading and parsing historical data from the Binance exchange

2 unstable releases

new 0.2.0 Dec 21, 2024
0.1.0 Mar 8, 2023

#6 in #binance

Download history 7/week @ 2024-09-21 3/week @ 2024-11-02

86 downloads per month

MIT/Apache

29KB
618 lines

Binance history

Crates.io Documentation

A Rust library for downloading and parsing historical data from Binance, supporting spot trades, futures, and candlestick data.


Additional Resources

Features

  • Download spot trades, futures trades, and candlestick data (klines).
  • Save data locally in CSV format.
  • Define custom data structures for advanced use cases.

Installation

Add the following line to your Cargo.toml file:

binance-history = "0.2.0"

Usage

Basic usage

use binance_history as bh;
use binance_history::{COINMKline, SpotTrade, USDMAggTrade};

fn main() -> bh::Result<()> {
    // Path for saving CSV files
    let path = "csv";

    // Time range for fetching data
    let from = "2024-12-01 00:00:00Z".parse().expect("Invalid start date format");
    let to = "2024-12-01 23:59:59Z".parse().expect("Invalid end date format");

    // Fetching spot trades
    let trades: Vec<SpotTrade> = bh::get_trades("BTCUSDT", from, to, path)?;
    println!("First spot trade: {:?}", trades.first());

    // Fetching aggregated trades for USD-M futures
    let agg_trades: Vec<USDMAggTrade> = bh::get_trades("BTCUSDT", from, to, path)?;
    println!("First aggregated trade: {:?}", agg_trades.first());

    // Fetching candlesticks (klines) for COIN-M futures
    let klines: Vec<COINMKline> = bh::get_klines("BTCUSD_PERP", "1h", from, to, path)?;
    println!("First kline: {:?}", klines.first());

    Ok(())
}

Using custom struct

You can define a custom structure to parse data specific to your application's requirements.

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

// Custom structure for representing price data
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct AggregatedPrice {
    time: i64,
    price: f64,
}

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

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

fn main() -> bh::Result<()> {
    // Path for saving CSV files
    let path = "csv";

    // Time range for fetching data
    let from = "2024-12-01 00:00:00Z".parse().expect("Invalid start date format");
    let to = "2024-12-01 23:59:59Z".parse().expect("Invalid end date format");

    // Fetching aggregated trades for USD-M futures
    let agg_trades: Vec<AggregatedPrice> = bh::get_trades("ETHUSDT", from, to, path)?;
    println!("First aggregated trade: {:?}", agg_trades.first());

    Ok(())
}

License

This project is dual-licensed under MIT and Apache 2.0.

Dependencies

~12–24MB
~341K SLoC