Example #1
0
    def test_keeps_book_intact_when_delete_on_empty_book(self):
        empty_book = Book()
        delete_bid = MBLUpdate.create_delete_bid_level(18.1)

        mbl = MBL(5)
        updated_book = mbl.update(empty_book, delete_bid)

        expected_book = Book()
        self.assertEqual(expected_book, updated_book)
Example #2
0
def btfxwss():

    log = logging.getLogger(__name__)

    fh = logging.FileHandler('test.log')
    fh.setLevel(logging.DEBUG)
    sh = logging.StreamHandler(sys.stdout)
    sh.setLevel(logging.DEBUG)

    log.addHandler(sh)
    log.addHandler(fh)
    logging.basicConfig(level=logging.DEBUG, handlers=[fh, sh])

    wss = BtfxWss()
    wss.start()

    while not wss.conn.connected.is_set():
        time.sleep(1)

    # Subscribe to some channels
    wss.subscribe_to_ticker('BTCUSD')
    wss.subscribe_to_order_book('BTCUSD')
    wss.subscribe_to_order_book('ETHUSD')

    # Wait for subscription...
    time.sleep(2)

    mbl = MBL()
    book = Book()
    recorder = Recorder(open("output.txt", mode='w'))

    # Accessing data stored in BtfxWss:
    books_queue = wss.books('ETHUSD')  # returns a Queue object for the pair.

    while True:

        book_update = books_queue.get()[0][0]

        if len(book_update) > 3:
            print("snapshot")
            mbl_snapshot = MBLSnapshot(book_update)
            book = mbl.from_snapshot(mbl_snapshot)
            recorder.on_mbl(book)

        elif len(book_update) == 3:
            print("update: " + str(book_update))
            update = MBLUpdate(book_update[0], book_update[1], book_update[2])
            book = mbl.update(book, update)
            recorder.on_mbl(book)

    # Unsubscribing from channels:
    wss.unsubscribe_from_ticker('BTCUSD')
    wss.unsubscribe_from_order_book('BTCUSD')

    # Shutting down the client:
    wss.stop()
Example #3
0
    def test_adds_bid_to_empty_book_when_update_on_bid(self):
        book = Book()
        bid_update = MBLUpdate(13.1, 4, 126)

        mbl = MBL(5)
        updated_book = mbl.update(book, bid_update)

        expected_book = Book()
        expected_book.add_bid(Level(13.1, 4, 126))
        self.assertEqual(expected_book, updated_book)
Example #4
0
    def test_keeps_book_intact_when_delete_missing_level(self):
        book = Book()
        book.add_bid(Level(13.1, 1, 2))
        book.add_bid(Level(15.1, 1, 2))
        delete_bid = MBLUpdate.create_delete_bid_level(18.1)

        mbl = MBL(5)
        updated_book = mbl.update(book, delete_bid)

        expected_book = Book([Level(13.1, 1, 2), Level(15.1, 1, 2)], [])
        self.assertEqual(expected_book, updated_book)
Example #5
0
    def test_deletes_existing_level_when_update_on_bid(self):
        book = Book()
        book.add_bid(Level(13.1, 1, 2))
        book.add_bid(Level(15.1, 1, 2))
        delete_bid = MBLUpdate.create_delete_bid_level(13.1)

        mbl = MBL(5)
        updated_book = mbl.update(book, delete_bid)

        expected_book = Book([Level(15.1, 1, 2)], [])
        self.assertEqual(expected_book, updated_book)
Example #6
0
    def test_cut_extra_levels_when_update_on_full_bid(self):
        book = Book()
        book.add_bid(Level(15.1, 1, 2))
        book.add_bid(Level(13.1, 1, 2))
        bid_update = MBLUpdate(17.1, 1, 2)

        mbl = MBL(length=2)
        updated_book = mbl.update(book, bid_update)

        expected_book = Book([Level(17.1, 1, 2), Level(15.1, 1, 2)], [])
        self.assertEqual(expected_book, updated_book)
Example #7
0
    def test_updates_existing_bid_level_when_update_on_bid(self):
        book = Book()
        book.add_bid(Level(13.1, 2, 7))
        bid_update = MBLUpdate(13.1, 4, 126)

        mbl = MBL(5)
        updated_book = mbl.update(book, bid_update)

        expected_book = Book()
        expected_book.add_bid(Level(13.1, 4, 126))
        self.assertEqual(expected_book, updated_book)
Example #8
0
    def test_updates_existing_bid(self):
        book = Book()
        book.add_bid(Level(11705.0, 1, 0.381988))
        book.add_bid(Level(11704.0, 4, 0.955999))
        book.add_bid(Level(11703.0, 1, 0.165141))

        print(book)

        mbl = MBL(3)
        bid_update = MBLUpdate(11704.0, 3, 0.94745498)
        updated_book = mbl.update(book, bid_update)
        print(updated_book)

        expected_book = Book()
        expected_book.add_bid(Level(11705.0, 1, 0.381988))
        expected_book.add_bid(Level(11704.0, 3, 0.94745498))
        expected_book.add_bid(Level(11703.0, 1, 0.165141))
        self.assertEqual(expected_book, updated_book)
Example #9
0
async def home_made_websocket(in_queue):
    async with websockets.connect("wss://api.bitfinex.com/ws/2") as ws:
        result = await ws.recv()
        result = json.loads(result)
        print("Connection established to Bitfinex api version %s " %
              result['version'])

        # Subscribe
        await ws.send(
            json.dumps({
                "event": "subscribe",
                "channel": "book",
                "symbol": "tBTCUSD",
                "prec": "P0",
                "freq": "F0",
                "len": "25"
            }))

        # Subscribe
        await ws.send(
            json.dumps({
                "event": "subscribe",
                "channel": "trades",
                "symbol": "tBTCUSD"
            }))

        mbl = MBL()
        book = Book()
        trades = Trades()
        channel_ids = {}

        try:

            while True:
                result = await ws.recv()
                result = json.loads(result)
                fct_to_call = None
                decoded_msg = Book()
                if 'event' in result and result['event'] == 'subscribed':
                    print("Subscribed to %s channel for %s" %
                          (result['channel'], result['pair']))
                    if result['channel'] == 'book':
                        channel_ids[result['chanId']] = 'b'
                    elif result['channel'] == 'trades':
                        channel_ids[result['chanId']] = 't'
                elif result[0] in channel_ids and channel_ids[
                        result[0]] == 't':
                    if result[1] == 'tu':
                        pass
                    elif result[1] == 'hb':
                        pass
                    elif len(result[1]) > 3:
                        trades = Trades(result[1], length=3)
                        decoded_msg = trades
                        fct_to_call = 'on_trade'
                    elif result[1] == 'te':
                        trades.add_trade(
                            Trade(result[2][1], result[2][2], result[2][3]))
                        decoded_msg = trades
                        fct_to_call = 'on_trade'
                elif result[0] in channel_ids and channel_ids[
                        result[0]] == 'b':
                    if len(result[1]) > 3:
                        mbl_snapshot = MBLSnapshot(result[1])
                        book = mbl.from_snapshot(mbl_snapshot)
                        decoded_msg = book
                        fct_to_call = 'on_mbl'
                    elif result[1] == 'hb':
                        pass
                    elif len(result[1]) == 3:
                        # print("update: " + str(result[1]))
                        update = MBLUpdate(result[1][0], result[1][1],
                                           result[1][2])
                        book = mbl.update(book, update)
                        decoded_msg = book
                        fct_to_call = 'on_mbl'

                if fct_to_call is not None:
                    in_queue.put({
                        'event_name': fct_to_call,
                        'data': decoded_msg
                    })

        except KeyboardInterrupt:
            print("Keyboard interruption in websocket")

        finally:
            print("Cleaning websocket")