def main2(**kwargs): output = kwargs['output'] mysqldest = kwargs['mysqldest'] mysql = kwargs['mysql'] mysqlschema = kwargs['mysqlschema'] instmts = kwargs['instmts'] exchtime = kwargs['exchtime'] Logger.init_log(output) db_clients = [] is_database_defined = False if mysql: db_client = MysqlClient() logon_credential = mysqldest.split('@')[0] connection = mysqldest.split('@')[1] db_client.connect(host=connection.split(':')[0], port=int(connection.split(':')[1]), user=logon_credential.split(':')[0], pwd=logon_credential.split(':')[1], schema=mysqlschema) db_clients.append(db_client) is_database_defined = True if not is_database_defined: print('Error: Please define which database is used.') sys.exit(1) # Subscription instruments if instmts is None or len(instmts) == 0: print( 'Error: Please define the instrument subscription list. You can refer to subscriptions.ini.' ) sys.exit(1) # Use exchange timestamp rather than local timestamp if exchtime: ExchangeGateway.is_local_timestamp = False # Initialize subscriptions subscription_instmts = SubscriptionManager(instmts).get_subscriptions() if len(subscription_instmts) == 0: print( 'Error: No instrument is found in the subscription file. ' + 'Please check the file path and the content of the subscription file.' ) sys.exit(1) # Initialize snapshot destination ExchangeGateway.init_snapshot_table(db_clients) Logger.info('[main]', 'Subscription file = %s' % instmts) log_str = 'Exchange/Instrument/InstrumentCode:\n' for instmt in subscription_instmts: log_str += '%s/%s/%s\n' % (instmt.exchange_name, instmt.instmt_name, instmt.instmt_code) Logger.info('[main]', log_str) exch_gws = [] exch_gws.append(ExchGwBitfinex(db_clients)) threads = [] for exch in exch_gws: for instmt in subscription_instmts: if instmt.get_exchange_name() == exch.get_exchange_name(): Logger.info("[main]", "Starting instrument %s-%s..." % \ (instmt.get_exchange_name(), instmt.get_instmt_name())) threads += exch.start(instmt)
Start the exchange gateway :param instmt: Instrument :return List of threads """ instmt.set_l2_depth(L2Depth(20)) instmt.set_prev_l2_depth(L2Depth(20)) instmt.set_order_book_table_name(self.get_order_book_table_name(instmt.get_exchange_name(), instmt.get_instmt_name())) instmt.set_trades_table_name(self.get_trades_table_name(instmt.get_exchange_name(), instmt.get_instmt_name())) instmt.set_order_book_id(self.get_order_book_init(instmt)) trade_id, last_exch_trade_id = self.get_trades_init(instmt) instmt.set_trade_id(trade_id) instmt.set_exch_trade_id(last_exch_trade_id) return [self.api_socket.connect(self.api_socket.get_link(), on_message_handler=partial(self.on_message_handler, instmt), on_open_handler=partial(self.on_open_handler, instmt), on_close_handler=partial(self.on_close_handler, instmt))] if __name__ == '__main__': exchange_name = 'Bitstamp' instmt_name = 'BTCUSD' instmt_code = '' instmt = Instrument(exchange_name, instmt_name, instmt_code) db_client = SqlClientTemplate() Logger.init_log() exch = ExchGwBitstamp(db_client) td = exch.start(instmt)
'-dbpwd', action='store', dest='dbpwd', help='Database password. Supported for database with connection') parser.add_argument( '-dbschema', action='store', dest='dbschema', help='Database schema. Supported for database with connection') parser.add_argument('-output', action='store', dest='output', help='Verbose output file path') args = parser.parse_args() Logger.init_log(args.output) if args.sqlite: db_client = SqliteClient() db_client.connect(path=args.dbpath) elif args.mysql: db_client = MysqlClient() db_client.connect(host=args.dbaddr, port=int(args.dbport), user=args.dbuser, pwd=args.dbpwd, schema=args.dbschema) elif args.csv: if args.dbdir != '': db_client = FileClient(dir=args.dbdir) else:
def setUpClass(cls): Logger.init_log() cls.db_client = FileClient(dir=path) cls.db_client.connect()
def main(): parser = argparse.ArgumentParser(description='Bitcoin exchange market data feed handler.') parser.add_argument('-instmts', action='store', help='Instrument subscription file.', default='subscriptions.ini') parser.add_argument('-exchtime', action='store_true', help='Use exchange timestamp.') parser.add_argument('-kdb', action='store_true', help='Use Kdb+ as database.') parser.add_argument('-csv', action='store_true', help='Use csv file as database.') parser.add_argument('-sqlite', action='store_true', help='Use SQLite database.') parser.add_argument('-mysql', action='store_true', help='Use MySQL.') parser.add_argument('-zmq', action='store_true', help='Use zmq publisher.') parser.add_argument('-mysqldest', action='store', dest='mysqldest', help='MySQL destination. Formatted as <name:pwd@host:port>', default='') parser.add_argument('-mysqlschema', action='store', dest='mysqlschema', help='MySQL schema.', default='') parser.add_argument('-kdbdest', action='store', dest='kdbdest', help='Kdb+ destination. Formatted as <host:port>', default='') parser.add_argument('-zmqdest', action='store', dest='zmqdest', help='Zmq destination. For example \"tcp://127.0.0.1:3306\"', default='') parser.add_argument('-sqlitepath', action='store', dest='sqlitepath', help='SQLite database path', default='') parser.add_argument('-csvpath', action='store', dest='csvpath', help='Csv file path', default='') parser.add_argument('-output', action='store', dest='output', help='Verbose output file path') args = parser.parse_args() Logger.init_log(args.output) db_clients = [] is_database_defined = False if args.sqlite: db_client = SqliteClient() db_client.connect(path=args.sqlitepath) db_clients.append(db_client) is_database_defined = True if args.mysql: db_client = MysqlClient() mysqldest = args.mysqldest logon_credential = mysqldest.split('@')[0] connection = mysqldest.split('@')[1] db_client.connect(host=connection.split(':')[0], port=int(connection.split(':')[1]), user=logon_credential.split(':')[0], pwd=logon_credential.split(':')[1], schema=args.mysqlschema) db_clients.append(db_client) is_database_defined = True if args.csv: if args.csvpath != '': db_client = FileClient(dir=args.csvpath) else: db_client = FileClient() db_clients.append(db_client) is_database_defined = True if args.kdb: db_client = KdbPlusClient() db_client.connect(host=args.kdbdest.split(':')[0], port=int(args.kdbdest.split(':')[1])) db_clients.append(db_client) is_database_defined = True if args.zmq: db_client = ZmqClient() db_client.connect(addr=args.zmqdest) db_clients.append(db_client) is_database_defined = True if not is_database_defined: print('Error: Please define which database is used.') parser.print_help() sys.exit(1) # Subscription instruments if args.instmts is None or len(args.instmts) == 0: print('Error: Please define the instrument subscription list. You can refer to subscriptions.ini.') parser.print_help() sys.exit(1) # Use exchange timestamp rather than local timestamp if args.exchtime: ExchangeGateway.is_local_timestamp = False # Initialize subscriptions subscription_instmts = SubscriptionManager(args.instmts).get_subscriptions() if len(subscription_instmts) == 0: print('Error: No instrument is found in the subscription file. ' + 'Please check the file path and the content of the subscription file.') parser.print_help() sys.exit(1) # Initialize snapshot destination ExchangeGateway.init_snapshot_table(db_clients) ExchangeGateway.init_detail_snapshot_table(db_clients) Logger.info('[main]', 'Subscription file = %s' % args.instmts) log_str = 'Exchange/Instrument/InstrumentCode:\n' for instmt in subscription_instmts: log_str += '%s/%s/%s\n' % (instmt.exchange_name, instmt.instmt_name, instmt.instmt_code) Logger.info('[main]', log_str) exch_gws = [] ##exch_gws.append(ExchGwBtccSpot(db_clients)) ##exch_gws.append(ExchGwBtccFuture(db_clients)) ##exch_gws.append(ExchGwBitmex(db_clients)) ##exch_gws.append(ExchGwBitfinex(db_clients)) ##exch_gws.append(ExchGwOkCoin(db_clients)) exch_gws.append(ExchGwKraken(db_clients)) exch_gws.append(ExchGwGdax(db_clients)) exch_gws.append(ExchGwBitstamp(db_clients)) exch_gws.append(ExchGwBitflyer(db_clients)) ##exch_gws.append(ExchGwHuoBi(db_clients)) exch_gws.append(ExchGwCoincheck(db_clients)) exch_gws.append(ExchGwCoinOne(db_clients)) ##exch_gws.append(ExchGwGatecoin(db_clients)) ##exch_gws.append(ExchGwQuoine(db_clients)) ##exch_gws.append(ExchGwPoloniex(db_clients)) exch_gws.append(ExchGwBittrex(db_clients)) ##exch_gws.append(ExchGwYunbi(db_clients)) # ##exch_gws.append(ExchGwLiqui(db_clients)) # ##exch_gws.append(ExchGwBinance(db_clients)) # exch_gws.append(ExchGwCryptopia(db_clients)) ##exch_gws.append(ExchGwOkex(db_clients)) #exch_gws.append(ExchGwWex(db_clients)) # error threads = [] for exch in exch_gws: for instmt in subscription_instmts: if instmt.get_exchange_name() == exch.get_exchange_name(): Logger.info("[main]", "Starting instrument %s-%s..." % \ (instmt.get_exchange_name(), instmt.get_instmt_name())) threads += exch.start(instmt)
# Delete a row from the table self.db_client.delete( table_name, "k=2") # Fetch the whole table row = self.db_client.select(table=table_name) self.assertEqual(len(row), 2) self.assertEqual(row[0][0], 1) self.assertEqual(row[0][1], "20161026") self.assertEqual(row[0][2], "10:00:00.000000") self.assertEqual(row[0][3], 'AbC') self.assertEqual(row[0][4], 10.3) self.assertEqual(row[1][0], 3) self.assertEqual(row[1][1], "20161026") self.assertEqual(row[1][2], "10:00:02.000000") self.assertEqual(row[1][3], 'Efgh') self.assertEqual(row[1][4], 10.5) # Delete remaining rows from the table self.db_client.delete(table_name) # Fetch the whole table row = self.db_client.select(table=table_name) self.assertEqual(len(row), 0) if __name__ == '__main__': Logger.init_log() unittest.main()