Пример #1
0
def websocket_to_order_book():
    try:
        coinbase_websocket = yield from websockets.connect(
            "wss://ws-feed.exchange.coinbase.com")
    except gaierror:
        order_book_file_logger.error(
            'socket.gaierror - had a problem connecting to Coinbase feed')
        return

    yield from coinbase_websocket.send(
        '{"type": "subscribe", "product_id": "BTC-USD"}')

    messages = []
    while True:
        message = yield from coinbase_websocket.recv()
        message = json.loads(message)
        messages += [message]
        if len(messages) > 20:
            break

    order_book.get_level3()

    [
        order_book.process_message(message) for message in messages
        if message['sequence'] > order_book.level3_sequence
    ]
    messages = []
    while True:
        message = yield from coinbase_websocket.recv()
        if message is None:
            order_book_file_logger.error('Websocket message is None.')
            return False
        try:
            message = json.loads(message)
        except TypeError:
            order_book_file_logger.error('JSON did not load, see ' +
                                         str(message))
            return False
        if args.command_line:
            messages += [
                datetime.strptime(
                    message['time'],
                    '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=pytz.UTC)
            ]
            messages = [
                message for message in messages
                if (datetime.now(tzlocal()) - message).seconds < 60
            ]
            if len(messages) > 2:
                diff = numpy.diff(messages)
                diff = [float(sec.microseconds) for sec in diff]
                order_book.average_rate = numpy.mean(diff)
                order_book.fastest_rate = min(diff)
                order_book.slowest_rate = max(diff)
        if not order_book.process_message(message):
            print(pformat(message))
            return False
        if args.trading:
            if 'order_id' in message and message[
                    'order_id'] == open_orders.open_ask_order_id:
                if message['type'] == 'done':
                    open_orders.open_ask_order_id = None
                    open_orders.open_ask_price = None
                    open_orders.open_ask_status = None
                    open_orders.open_ask_rejections = Decimal('0.0')
                    open_orders.open_ask_cancelled = False
                else:
                    open_orders.open_ask_status = message['type']
            elif 'order_id' in message and message[
                    'order_id'] == open_orders.open_bid_order_id:
                if message['type'] == 'done':
                    open_orders.open_bid_order_id = None
                    open_orders.open_bid_price = None
                    open_orders.open_bid_status = None
                    open_orders.open_bid_rejections = Decimal('0.0')
                    open_orders.open_bid_cancelled = False
                else:
                    open_orders.open_bid_status = message['type']
def websocket_to_order_book():
    try:
        coinbase_websocket = yield from websockets.connect("wss://ws-feed.exchange.coinbase.com")
    except gaierror:
        order_book_file_logger.error('socket.gaierror - had a problem connecting to Coinbase feed')
        return

    yield from coinbase_websocket.send('{"type": "subscribe", "product_id": "BTC-USD"}')

    messages = []
    while True:
        message = yield from coinbase_websocket.recv()
        message = json.loads(message)
        messages += [message]
        if len(messages) > 20:
            break

    order_book.get_level3()

    [order_book.process_message(message) for message in messages if message['sequence'] > order_book.level3_sequence]
    messages = []
    while True:
        message = yield from coinbase_websocket.recv()
        if message is None:
            order_book_file_logger.error('Websocket message is None.')
            return False
        try:
            message = json.loads(message)
        except TypeError:
            order_book_file_logger.error('JSON did not load, see ' + str(message))
            return False
        if args.command_line:
            messages += [datetime.strptime(message['time'], '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=pytz.UTC)]
            messages = [message for message in messages if (datetime.now(tzlocal()) - message).seconds < 60]
            if len(messages) > 2:
                diff = numpy.diff(messages)
                diff = [float(sec.microseconds) for sec in diff]
                order_book.average_rate = numpy.mean(diff)
                order_book.fastest_rate = min(diff)
                order_book.slowest_rate = max(diff)
        if not order_book.process_message(message):
            print(pformat(message))
            return False
        if args.trading:
            if 'order_id' in message and message['order_id'] == open_orders.open_ask_order_id:
                if message['type'] == 'done':
                    open_orders.open_ask_order_id = None
                    open_orders.open_ask_price = None
                    open_orders.open_ask_status = None
                    open_orders.open_ask_rejections = Decimal('0.0')
                    open_orders.open_ask_cancelled = False
                else:
                    open_orders.open_ask_status = message['type']
            elif 'order_id' in message and message['order_id'] == open_orders.open_bid_order_id:
                if message['type'] == 'done':
                    open_orders.open_bid_order_id = None
                    open_orders.open_bid_price = None
                    open_orders.open_bid_status = None
                    open_orders.open_bid_rejections = Decimal('0.0')
                    open_orders.open_bid_cancelled = False
                else:
                    open_orders.open_bid_status = message['type']
Пример #3
0
        stream_handler.setLevel(logging.INFO)
        trading_file_logger.addHandler(stream_handler)
        order_book_file_logger.addHandler(stream_handler)
        command_line = True

    loop = asyncio.get_event_loop()
    if args.trading:
        loop.run_in_executor(
            None,
            functools.partial(buyer_strategy, order_book, open_orders,
                              spreads))
        loop.run_in_executor(None, update_balances)
        loop.run_in_executor(None, update_orders)
    if args.command_line:
        loop.run_in_executor(None, monitor)
    n = 0
    while True:
        start_time = loop.time()
        loop.run_until_complete(websocket_to_order_book())
        end_time = loop.time()
        seconds = end_time - start_time
        if seconds < 2:
            n += 1
            sleep_time = (2**n) + (random.randint(0, 1000) / 1000)
            order_book_file_logger.error(
                'Websocket connectivity problem, going to sleep for {0}'.
                format(sleep_time))
            time.sleep(sleep_time)
            if n > 6:
                n = 0
if __name__ == '__main__':
    if args.command_line:
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(logging.Formatter('\n%(asctime)s, %(levelname)s, %(message)s'))
        stream_handler.setLevel(logging.INFO)
        trading_file_logger.addHandler(stream_handler)
        order_book_file_logger.addHandler(stream_handler)
        command_line = True

    loop = asyncio.get_event_loop()
    if args.trading:
        loop.run_in_executor(None, functools.partial(buyer_strategy, order_book, open_orders, spreads))
        loop.run_in_executor(None, update_balances)
        loop.run_in_executor(None, update_orders)
    if args.command_line:
        loop.run_in_executor(None, monitor)
    n = 0
    while True:
        start_time = loop.time()
        loop.run_until_complete(websocket_to_order_book())
        end_time = loop.time()
        seconds = end_time - start_time
        if seconds < 2:
            n += 1
            sleep_time = (2 ** n) + (random.randint(0, 1000) / 1000)
            order_book_file_logger.error('Websocket connectivity problem, going to sleep for {0}'.format(sleep_time))
            time.sleep(sleep_time)
            if n > 6:
                n = 0