S-razmi / DeepLOB

DeepLOB Implementation on Bitcoin Perpetual Data
MIT License
9 stars 1 forks source link

Download data or live data ? #1

Open nPokemon opened 9 months ago

nPokemon commented 9 months ago

Could you help to download data for it or live data for different crypto please ?

S-razmi commented 9 months ago

Sure! a sample Dataset is available in my Kaggle with the notebook to run the code. https://www.kaggle.com/datasets/siavashraz/bitcoin-perpetualbtcusdtp-limit-order-book-data/data

nPokemon commented 9 months ago

Sorry, I mean , how can I download different data instead of BTCUSDT, because different tickers may have different MM to run right ? Like ETHUSDT ? SOLUSDT ? Thank you for helpin.g

S-razmi commented 9 months ago

You can get the data through different exchanges by their API. Binance, coinex and kucoin provide this data.

On Wed, Sep 27, 2023, 6:54 PM nPokemon @.***> wrote:

Sorry, I mean , how can I download different data instead of BTCUSDT, because different tickers may have different MM to run right ? Like ETHUSDT ? SOLUSDT ? Thank you for helpin.g

— Reply to this email directly, view it on GitHub https://github.com/S-razmi/DeepLOB/issues/1#issuecomment-1737621545, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKVCY73DOPECZN2YTA6OKSLX4RAKHANCNFSM6AAAAAA5JOSZSI . You are receiving this because you commented.Message ID: @.***>

nPokemon commented 9 months ago

Yes, Thank you, But I saw there are 40 columns of that can you help me to get the right data ? This is my code as Binance API documents, but did not see the correct format as your:

import json import time import csv import os from binance.client import Client from binance.exceptions import BinanceAPIException

Load config.json

with open('config.json', 'r') as f: config = json.load(f)

Initialize Binance Client

client = Client(config['BINANCE_KEY_API'], config['BINANCE_SECRET_API'])

def download_data(trading_pair): try:

Interact with Binance API to get the order book depth

    depth = client.get_order_book(symbol=trading_pair, limit=20)  # 20 bid and 20 ask levels

    ### Extracting timestamp, bids, and asks from the depth data
    timestamp = depth['lastUpdateId']
    datetime_str = time.strftime('%d/%m/%Y %H:%M', time.gmtime(time.time()))
    bids = depth['bids']
    asks = depth['asks']

    ### Include an additional unnamed index column at the start of each row
    formatted_data = [None, timestamp, datetime_str]  # None represents the unnamed index column

    for bid, ask in zip(bids, asks):
        ### Append bid price, bid quantity, ask price, ask quantity alternately
        formatted_data.extend([bid[0], bid[1], ask[0], ask[1]])

    ### Define the file name based on trading_pair and current date/time
    file_name = f"{trading_pair}_LOB_{time.strftime('%Y%m%d%H%M%S')}.csv"

    ### Check if file exists to determine whether to write headers
    file_exists = os.path.isfile(file_name)

    ### Open the file in append mode, if file does not exist, it will be created
    with open(file_name, 'a', newline='') as file:
        writer = csv.writer(file)

        ### Write headers if file is being created
        if not file_exists:
            headers = ['Timestamp', 'Datetime']
            for i in range(1, 21):
                headers.extend([f'Bid_Price_{i}', f'Bid_Quantity_{i}', f'Ask_Price_{i}', f'Ask_Quantity_{i}'])
            writer.writerow(headers)

        ### Write the formatted data to the file
        writer.writerow(formatted_data)

except BinanceAPIException as e:
    print(f"Error occurred for {trading_pair}: {e.message}")
except Exception as e:
    print(f"An unexpected error occurred for {trading_pair}: {str(e)}")

def main(): while True:

Handle the continuous downloading of data.

    for trading_pair in config['TRADING_PAIRS']:
        download_data(trading_pair)
    time.sleep(config['REFRESH_TIME'])  ### Sleep for the specified interval before downloading the next set of data.

if name == "main": main()