예제 #1
0
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)

    storages = []
    is_database_defined = False

    if args.sqlite:
        storage = SqliteStorage()
        storage.connect(path=args.sqlitepath)
        storages.append(storage)
        is_database_defined = True

    if args.mysql:
        storage = MysqlStorage()
        mysqldest = args.mysqldest
        logon_credential = mysqldest.split('@')[0]
        connection = mysqldest.split('@')[1]

        storage.connect(host=connection.split(':')[0],
                        port=int(connection.split(':')[1]),
                        user=logon_credential.split(':')[0],
                        pwd=logon_credential.split(':')[1],
                        schema=args.mysqlschema)

        storages.append(storage)
        is_database_defined = True

    if args.csv:
        if args.csvpath != '':
            storage = FileStorage(dir=args.csvpath)
        else:
            storage = FileStorage()

        storages.append(storage)
        is_database_defined = True

    if args.kdb:
        storage = KdbPlusStorage()
        storage.connect(host=args.kdbdest.split(':')[0],
                        port=int(args.kdbdest.split(':')[1]))
        storages.append(storage)
        is_database_defined = True

    if args.zmq:
        storage = ZmqStorage()
        storage.connect(addr=args.zmqdest)
        storages.append(storage)
        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(storages)
    Logger.info(__name__, '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(__name__, log_str)

    gateways = []
    # gateways.append(ExchGwBtccSpot(storages))
    # gateways.append(ExchGwBtccFuture(storages))
    # gateways.append(ExchGwBitmex(storages))
    # gateways.append(ExchGwBitfinex(storages))
    # gateways.append(ExchGwOkCoin(storages))
    # gateways.append(ExchGwKraken(storages))
    # gateways.append(ExchGwGdax(storages))
    gateways.append(BitstampGateway(storages))
    # gateways.append(ExchGwGatecoin(storages))
    # gateways.append(ExchGwQuoine(storages))
    # gateways.append(ExchGwPoloniex(storages))
    # gateways.append(ExchGwBittrex(storages))
    # gateways.append(ExchGwYunbi(storages))
    # gateways.append(ExchGwLiqui(storages))
    gateways.append(BinanceGateway(storages))
    # gateways.append(ExchGwCryptopia(storages))

    threads = []

    for item in gateways:
        for instmt in subscription_instmts:
            if instmt.get_exchange_name() == item.get_exchange_name():
                Logger.info(__name__, "Starting instrument %s-%s..." % \
                    (instmt.get_exchange_name(), instmt.get_instmt_name()))
                threads += item.start(instmt)
예제 #2
0
        :return List of threads
        """
        instmt.set_l2_depth(L2Depth(5))
        instmt.set_prev_l2_depth(L2Depth(5))
        instmt.set_instmt_snapshot_table_name(
            self.get_instmt_snapshot_table_name(instmt.get_exchange_name(),
                                                instmt.get_instmt_name()))
        self.init_instmt_snapshot_table(instmt)
        instmt.set_recovered(False)
        t1 = threading.Thread(
            target=partial(self.get_order_book_worker, instmt))
        t2 = threading.Thread(target=partial(self.get_trades_worker, instmt))
        t1.start()
        t2.start()
        return [t1, t2]


if __name__ == '__main__':
    Logger.init_log()
    exchange_name = 'Poloniex'
    instmt_name = 'BTCETH'
    instmt_code = 'BTC_ETH'
    instmt = Instrument(exchange_name, instmt_name, instmt_code)
    db_storage = SqlStorageTemplate()
    exch = ExchGwPoloniex([db_storage])
    instmt.set_l2_depth(L2Depth(5))
    instmt.set_prev_l2_depth(L2Depth(5))
    instmt.set_recovered(False)
    # exch.get_order_book_worker(instmt)
    exch.get_trades_worker(instmt)
예제 #3
0
 def setUpClass(cls):
     Logger.init_log()
     cls.storage = FileStorage(dir=path)
     cls.storage.connect()