Example #1
0
async def process_message(websocket, path):
    global messages, i, since_update

    msg = await websocket.recv()
    since_update += 1

    message_type = msg.split('|', maxsplit=1)[0]

    if message_type == 'o':
        await websocket.send("ACK")
        o = Order.deserialize(msg)
        if o.size != 0:
            trades = book.submit_order(o)
            """
            if len(trades) > 0:
                print('\n'.join(map(str, trades)))
            """

    elif message_type == 'bb':
        await websocket.send(f"bb|{book.best_bid():.4f}")

    elif message_type == 'bo':
        await websocket.send(f"bo|{book.best_offer():.4f}")

    elif message_type == 'bbbo':
        await websocket.send(
            f"bbbo|{book.best_bid():.4f}|{book.best_offer():.4f}|{time.time_ns()}"
        )
Example #2
0
    def test_deserialize_order(self):
        o_serialized = "o|ABC|10.0000|10|BUY|123"
        o = Order.deserialize(o_serialized)

        self.assertEqual(o.instrument, "ABC")
        self.assertEqual(o.price, 10.00)
        self.assertEqual(o.size, 10)
        self.assertEqual(o.side, Side.BUY)
        self.assertEqual(o.client_id, 123)

        o_serialized = "o|CBA|12.3330|432|SELL|4242"

        o = Order.deserialize(o_serialized)

        self.assertEqual(o.instrument, "CBA")
        self.assertEqual(o.price, 12.333)
        self.assertEqual(o.size, 432)
        self.assertEqual(o.side, Side.SELL)
        self.assertEqual(o.client_id, 4242)