Last active
August 20, 2024 09:16
-
-
Save nistrup/1e724d6e450fd1da09a0782e6bfcd41a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IMPORTS | |
import pandas as pd | |
import math | |
import os.path | |
import time | |
from bitmex import bitmex | |
from binance.client import Client | |
from datetime import timedelta, datetime | |
from dateutil import parser | |
from tqdm import tqdm_notebook #(Optional, used for progress-bars) | |
### API | |
bitmex_api_key = '[REDACTED]' #Enter your own API-key here | |
bitmex_api_secret = '[REDACTED]' #Enter your own API-secret here | |
binance_api_key = '[REDACTED]' #Enter your own API-key here | |
binance_api_secret = '[REDACTED]' #Enter your own API-secret here | |
### CONSTANTS | |
binsizes = {"1m": 1, "5m": 5, "1h": 60, "1d": 1440} | |
batch_size = 750 | |
bitmex_client = bitmex(test=False, api_key=bitmex_api_key, api_secret=bitmex_api_secret) | |
binance_client = Client(api_key=binance_api_key, api_secret=binance_api_secret) | |
### FUNCTIONS | |
def minutes_of_new_data(symbol, kline_size, data, source): | |
if len(data) > 0: old = parser.parse(data["timestamp"].iloc[-1]) | |
elif source == "binance": old = datetime.strptime('1 Jan 2017', '%d %b %Y') | |
elif source == "bitmex": old = bitmex_client.Trade.Trade_getBucketed(symbol=symbol, binSize=kline_size, count=1, reverse=False).result()[0][0]['timestamp'] | |
if source == "binance": new = pd.to_datetime(binance_client.get_klines(symbol=symbol, interval=kline_size)[-1][0], unit='ms') | |
if source == "bitmex": new = bitmex_client.Trade.Trade_getBucketed(symbol=symbol, binSize=kline_size, count=1, reverse=True).result()[0][0]['timestamp'] | |
return old, new | |
def get_all_binance(symbol, kline_size, save = False): | |
filename = '%s-%s-data.csv' % (symbol, kline_size) | |
if os.path.isfile(filename): data_df = pd.read_csv(filename) | |
else: data_df = pd.DataFrame() | |
oldest_point, newest_point = minutes_of_new_data(symbol, kline_size, data_df, source = "binance") | |
delta_min = (newest_point - oldest_point).total_seconds()/60 | |
available_data = math.ceil(delta_min/binsizes[kline_size]) | |
if oldest_point == datetime.strptime('1 Jan 2017', '%d %b %Y'): print('Downloading all available %s data for %s. Be patient..!' % (kline_size, symbol)) | |
else: print('Downloading %d minutes of new data available for %s, i.e. %d instances of %s data.' % (delta_min, symbol, available_data, kline_size)) | |
klines = binance_client.get_historical_klines(symbol, kline_size, oldest_point.strftime("%d %b %Y %H:%M:%S"), newest_point.strftime("%d %b %Y %H:%M:%S")) | |
data = pd.DataFrame(klines, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ]) | |
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms') | |
if len(data_df) > 0: | |
temp_df = pd.DataFrame(data) | |
data_df = data_df.append(temp_df) | |
else: data_df = data | |
data_df.set_index('timestamp', inplace=True) | |
if save: data_df.to_csv(filename) | |
print('All caught up..!') | |
return data_df | |
def get_all_bitmex(symbol, kline_size, save = False): | |
filename = '%s-%s-data.csv' % (symbol, kline_size) | |
if os.path.isfile(filename): data_df = pd.read_csv(filename) | |
else: data_df = pd.DataFrame() | |
oldest_point, newest_point = minutes_of_new_data(symbol, kline_size, data_df, source = "bitmex") | |
delta_min = (newest_point - oldest_point).total_seconds()/60 | |
available_data = math.ceil(delta_min/binsizes[kline_size]) | |
rounds = math.ceil(available_data / batch_size) | |
if rounds > 0: | |
print('Downloading %d minutes of new data available for %s, i.e. %d instances of %s data in %d rounds.' % (delta_min, symbol, available_data, kline_size, rounds)) | |
for round_num in tqdm_notebook(range(rounds)): | |
time.sleep(1) | |
new_time = (oldest_point + timedelta(minutes = round_num * batch_size * binsizes[kline_size])) | |
data = bitmex_client.Trade.Trade_getBucketed(symbol=symbol, binSize=kline_size, count=batch_size, startTime = new_time).result()[0] | |
temp_df = pd.DataFrame(data) | |
data_df = data_df.append(temp_df) | |
data_df.set_index('timestamp', inplace=True) | |
if save and rounds > 0: data_df.to_csv(filename) | |
print('All caught up..!') | |
return data_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Bro, its amazing