Skip to content

Instantly share code, notes, and snippets.

@veeenu
Last active September 24, 2018 14:00
Show Gist options
  • Save veeenu/4f12c80acd307146333619fcbefa4611 to your computer and use it in GitHub Desktop.
Save veeenu/4f12c80acd307146333619fcbefa4611 to your computer and use it in GitHub Desktop.
import sys
import time
import tqdm
import cbpro
import pandas as pd
if __name__ == '__main__':
if len(sys.argv) < 3:
print(f'Usage: {sys.argv[0]} start_date end_date')
exit()
cl = cbpro.PublicClient()
dr = pd.date_range(*sys.argv[1:3], freq='5min')[::-300].sort_values()
couples = list((lambda x: zip(x[:-1], x[1:]))(dr.tolist()))
tm = time.time()
for i in tqdm.tqdm(couples):
lst = cl.get_product_historic_rates('BTC-EUR', i[0].isoformat(), i[1].isoformat(), granularity=300)
df = pd.DataFrame(lst, columns='date open high low close volume'.split(' ')).set_index('date')
df = df.set_index(df.index.astype('datetime64[s]'))
df.to_csv(f'files/{i[0].strftime("%Y%m%d%H%M%S")}.csv')
ntm = time.time()
if ntm - tm < .75:
time.sleep(.75 - ntm + tm)
tm = time.time()
from glob import glob
from tqdm import tqdm
import pandas as pd
files = glob('files/*.csv')
df = None
for i in tqdm(files):
dfi = pd.read_csv(i).set_index('date')
df = pd.concat([df, dfi]) if df is not None else dfi
df = df.set_index(df.index.astype('datetime64')).sort_index()
df['open_interest'] = pd.Series(index=df.index)
df.columns = pd.MultiIndex.from_product([['BTC-EUR'], df.columns])
df.to_csv('all.csv')
df.astype('float64').to_hdf('crypto.h5', 'table')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment