def download_data(self, pair, start, end): """Download trade data and store as .csv file. Args: pair (str): Currency pair. start (int): Start UNIX of trade data to download. end (int): End UNIX of trade data to download. """ dataio = DataIO(savedir=self._savedir, fieldnames=self.FIELDNAMES) if dataio.csv_check(pair): newest_t = float(dataio.csv_get_last(pair)['time']) else: newest_t = self.__find_start_trade_time(pair, start) while newest_t < end: # old -> new r = self.__get_slice(pair, newest_t + 1e-4) # list to dict r = self.__to_dict(r) # save to file dataio.csv_append(pair, r) # break condition if len(r) < self.__MAX_LIMIT: break # prepare next iteration newest_t = float(r[-1]['time']) print('Kraken\t| {} : {}'.format(timeutil.unix_to_iso(newest_t), pair)) print('Kraken\t| Download complete : {}'.format(pair))
def get_trades(self, pair, start, end): """Get trade data from .csv file. Args: pair (str): Currency pair. start (int): Start UNIX of trade data to fetch. end (int): End UNIX of trade data to fetch. Returns: List of trade events, from old to new data. """ dataio = DataIO(savedir=self._savedir, fieldnames=self.FIELDNAMES) if dataio.csv_check(pair): data = dataio.csv_get(pair) else: raise ValueError( 'Bitmex\t| No trades downloaded: {}'.format(pair)) # look for start and end row indices start_index = 0 end_index = len(data['time']) - 1 for i, timestamp in enumerate(data['time']): if float(timestamp) <= float(start): start_index = i if float(timestamp) <= float(end): end_index = i # filter by requested date range new_data = {} for col_name in data: new_data[col_name] = data[col_name][start_index: (end_index + 1)] return new_data
def download_data(self, pair, start, end): """Download trade data and store as .csv file. Args: pair (str): Currency pair. start (int): Start UNIX of trade data to download. end (int): End UNIX of trade data to download. """ dataio = DataIO(savedir=self._savedir, fieldnames=self.FIELDNAMES) if dataio.csv_check(pair): last_row = dataio.csv_get_last(pair) newest_id = int(last_row['trade_id']) + 1 newest_t = int(last_row['time']) else: newest_id = self.__find_start_trade_id(pair, start) newest_t = 0 while newest_t < end: # new -> old r = self.__get_slice(pair, newest_id) # old -> new, add unix timestamp new_r = [] for row in r: row['time'] = row['T'] // 1000 row['date'] = timeutil.unix_to_iso(row['time']) row['price'] = row['p'] row['size'] = row['q'] row['side'] = 'sell' if row['m'] == True else 'buy' row['best_price_match'] = row['M'] row['trade_id'] = row['a'] row.pop('a', None) row.pop('p', None) row.pop('q', None) row.pop('f', None) row.pop('l', None) row.pop('T', None) row.pop('m', None) row.pop('M', None) new_r.append(row) # save to file dataio.csv_append(pair, new_r) # break condition if len(r) < self.__MAX_LIMIT: break # prepare next iteration newest_id = new_r[-1]['trade_id'] + 1 newest_t = new_r[-1]['time'] print('Binance\t| {} : {}'.format(timeutil.unix_to_iso(newest_t), pair)) print('Binance\t| Download complete : {}'.format(pair))
def download_data(self, pair, start, end): """Download trade data and store as .csv file. Args: pair (str): Currency pair. start (int): Start UNIX of trade data to download. end (int): End UNIX of trade data to download. """ dataio = DataIO(savedir=self._savedir, fieldnames=self.FIELDNAMES) if dataio.csv_check(pair): last_row = dataio.csv_get_last(pair) newest_id = int(last_row['trade_id']) + 1 newest_t = int(last_row['time']) else: newest_id = self.__find_start_trade_id(pair, start) newest_t = 0 last_trade_id = self.__find_last_trade_id(pair) while newest_t < end: # new -> old r = self.__get_slice(pair, newest_id + self.__MAX_LIMIT) # break condition to_break = False # old -> new, add unix timestamp new_r = [] for row in reversed(r): if row['trade_id'] > newest_id: row['date'] = row['time'] row['time'] = timeutil.iso_to_unix(row['time']) new_r.append(row) if row['trade_id'] == last_trade_id: to_break = True # save to file dataio.csv_append(pair, new_r) # break condition if to_break: break # prepare next iteration newest_id = new_r[-1]['trade_id'] newest_t = new_r[-1]['time'] print('GDAX\t| {} : {}'.format(timeutil.unix_to_iso(newest_t), pair)) print('GDAX\t| Download complete : {}'.format(pair))
def download_data(self, pair, start, end): """Download trade data and store as .csv file. Args: pair (str): Currency pair. start (int): Start UNIX of trade data to download. end (int): End UNIX of trade data to download. """ dataio = DataIO(savedir=self._savedir, fieldnames=self.FIELDNAMES) if dataio.csv_check(pair): last_row = dataio.csv_get_last(pair) newest_id = int(last_row['trade_id']) + 1 newest_t = int(last_row['time']) else: newest_id = self.__find_start_trade_id(pair, start) newest_t = 0 while newest_t < end: # new -> old r = self.__get_slice(pair, newest_id) # old -> new, add unix timestamp new_r = [] for i, row in enumerate(r): row['time'] = timeutil.iso_to_unix(row['timestamp']) row['date'] = row['timestamp'] row['trade_id'] = newest_id + i row['side'] = row['side'].lower() row.pop('timestamp', None) row.pop('symbol', None) new_r.append(row) # save to file dataio.csv_append(pair, new_r) # break condition if len(r) < self.__MAX_LIMIT: break # prepare next iteration newest_id = new_r[-1]['trade_id'] + 1 newest_t = new_r[-1]['time'] print('Bitmex\t| {} : {}'.format( timeutil.unix_to_iso(newest_t), pair)) print('Bitmex\t| Download complete : {}'.format(pair))
def download_data(self, pair, start, end): """Download trade data and store as .csv file. Args: pair (str): Currency pair. start (int): Start UNIX of trade data to download. end (int): End UNIX of trade data to download. """ dataio = DataIO(savedir=self._savedir, fieldnames=self.FIELDNAMES) last_row = None if dataio.csv_check(pair): last_row = dataio.csv_get_last(pair) newest_t = int(last_row['time']) else: newest_t = self.__find_start_trade_time(pair, start) - 1 # break condition last_trade_time = self.__find_last_trade_time(pair) while newest_t < end: # new -> old r = self.__get_slice(pair, newest_t) # old -> new; remove duplicate data by trade ID new_r = [] for row in reversed(r): if last_row is not None: if int(last_row['tradeID']) >= row['tradeID']: continue # remove duplicates last_row = row row['time'] = timeutil.iso_to_unix(row['date']) new_r.append(row) if newest_t > last_trade_time: break # save to file dataio.csv_append(pair, new_r) # prepare next iteration newest_t += self.__MAX_RANGE print('Poloniex| {} : {}'.format( timeutil.unix_to_iso(newest_t), pair)) print('Poloniex| Download complete : {}'.format(pair))
import numpy from dataio import DataIO if __name__ == '__main__': filename = 'test' fieldnames = ['time', 'low', 'high'] csvio = DataIO('test', fieldnames) csvio.csv_newfile(filename) test_row1 = {'time': 1.0, 'low': 100, 'high': 101} test_row2 = {'time': 2.0, 'low': 101, 'high': 102} test_rows = [{'time': 3.0, 'low': 102, 'high': 103}, {'time': 4.0, 'low': 103, 'high': 104}] exists = csvio.csv_check(filename) print(exists) print(len(numpy.shape(test_row1))) print(len(numpy.shape(test_rows))) csvio.csv_append(filename, test_row1) csvio.csv_append(filename, test_row2) csvio.csv_append(filename, test_rows) data = csvio.csv_get(filename) print(data) csvio.csv_rename(filename, 'test2')