huenique / binance_historical_data

python PYPI package to dump all needed crypto historical data from binance just by 2 lines of code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

binance_historical_data

GitHub last commit GitHub license<space><space> PyPI PyPI - Python Version

binance_historical_data is a Python package designed to simplify the download of historical cryptocurrency data (prices and volumes) from the Binance server. Notably, you can download all historical data without needing an account on binance.com.

Data is sourced from Binance Data Vision, where it is dumped locally and unzipped, providing you with a ready-to-use local copy.

Using this package, you can access comprehensive historical data on prices and volumes with just three lines of Python code. Additionally, updating already downloaded data requires only another three lines of code.

Limitations: Data from the previous day is only available on the Binance server a few minutes after 0:00 AM UTC, which introduces a delay in data availability.

pip install binance_historical_data
from binance_historical_data import BinanceDataDumper

data_dumper = BinanceDataDumper(
    path_dir_where_to_dump=".",
    asset_class="spot",  # spot, um, cm
    data_type="klines",  # aggTrades, klines, trades
    data_frequency="1m",
)

Arguments:

  1. path_dir_where_to_dump:
    (string) Path to folder where to dump the data
  2. asset_class:
    (string) Source of data: [spot, um, cm] um: usd(t) margined futures, cm: coin margined futures
  3. data_type="klines":
    (string) data type to dump:
    [aggTrades, klines, trades] for spot
    [aggTrades, klines, trades, indexPriceKlines, markPriceKlines, premiumIndexKlines, metrics] for futures (metrics only supported for um)
    Refer to binance doc for additional info: https://github.com/binance/binance-public-data
  4. str_data_frequency:
    (string) One of [1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h]
    Frequency of price-volume data candles to get
data_dumper.dump_data(
    tickers=None,
    date_start=None,
    date_end=None,
    is_to_update_existing=False,
    tickers_to_exclude=["UST"],
)

Arguments:

  1. tickers=None:
    (list) Trading pairs for which to dump data
    if equals to None - all USDT pairs will be used
  2. date_start=None:
    (datetime.date) The date from which to start dump
    if equals to None - every trading pair will be dumped from the early begining (the earliest is 2017-01-01)
  3. date_end=True=None:
    (datetime.date) The last date for which to dump data
    if equals to None - Today's date will be used
  4. is_to_update_existing=False:
    (bool) Flag if you want to update the data if it's already exist
  5. tickers_to_exclude=None:
    (list) Tickers to exclude from dump

Deleta all daily data for which full month monthly data was already dumped

data_dumper.delete_outdated_daily_results()
"Open time" - Timestamp
"Open"
"High"
"Low"
"Close"
"Volume"
"Close time" - Timestamp
"Quote asset volume"
"Number of trades"
"Taker buy base asset volume"
"Taker buy quote asset volume"
"Ignore"

Please be advised that the first data dump for all trading pairs might take some time (~40 minutes)

data_dumper.dump_data()
It's as easy as running the exactly same method dump_data once again
The data_dumper will find all the dates for which data already exists
and will try to dump only the new data
data_dumper.dump_data()
data_dumper.dump_data(
    date_start=datetime.date(year=2021, month=1, day=1),
    date_end=datetime.date(year=2022, month=1, day=1),
    is_to_update_existing=True
)
print(data_dumper.get_list_all_trading_pairs())
print(data_dumper.get_min_start_date_for_ticker())
print(
    data_dumper.get_all_tickers_with_data(timeperiod_per_file="daily")
)
print(
    data_dumper.get_all_dates_with_data_for_ticker(
        ticker,
        timeperiod_per_file="monthly"
    )
)
print(
    data_dumper.get_local_dir_to_data(
        ticker,
        timeperiod_per_file,
    )
)
print(
    data_dumper.create_filename(
        ticker,
        date_obj,
        timeperiod_per_file="monthly",
    )
)

This project is licensed under the MIT License.

About

python PYPI package to dump all needed crypto historical data from binance just by 2 lines of code

License:MIT License


Languages

Language:Python 100.0%