Example #1
0
def test_orderbook_deletion():
    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook = Orderbook("TEST")

    orderbook.new_order(order)

    orderbook.delete_order("NEWORDER_1")

    assert len(orderbook.bids[23.54]) == 0
Example #2
0
def test_orderbook_duplicate_order_id():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.59, 120, "S", 2, "TESTSESSION_2_NEWORDER_2",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    order = Order("TEST", 23.49, 130, "B", 2, "TESTSESSION_2_NEWORDER_2",
                  "TESTSESSION_2")

    assert orderbook.new_order(order) == "[Internal] duplicate order ID sent"
Example #3
0
def test_orderbook_replace_price_and_size():
    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook = Orderbook("TEST")

    orderbook.new_order(order)

    order = Order("TEST", 23.55, 120, "B", 2, "NEWORDER_2", "TESTSESSION")

    orderbook.replace_order("NEWORDER_1", order)

    assert orderbook.bids[23.55][0].quantity == 120
    assert orderbook.bids[23.55][0].price == 23.55
Example #4
0
def test_orderbook_market_order_no_match():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.52, 50, "B", 1, "NEWORDER_2", "TESTSESSION")
    orderbook.new_order(order)

    assert orderbook.trades.qsize() == 0
    assert orderbook.bids[23.54][0].quantity == 100
    assert orderbook.bbo() == (23.54, float("inf"))
Example #5
0
def test_orderbook_bbo():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 150, "B", 2, "TESTSESSION_1_NEWORDER_1",
                  "TESTSESSION_1")
    orderbook.new_order(order)

    order = Order("TEST", 23.55, 130, "S", 2, "TESTSESSION_2_NEWORDER_1",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    assert orderbook.bbo() == (23.54, 23.55)
Example #6
0
def test_orderbook_insertion():
    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook = Orderbook("TEST")

    orderbook.new_order(order)

    inserted_order = orderbook.bids[23.54][0]

    assert inserted_order.symbol == "TEST"
    assert inserted_order.price == 23.54
    assert inserted_order.quantity == 100
    assert inserted_order.side == "b"
    assert inserted_order.order_id == "NEWORDER_1"
Example #7
0
def test_orderbook_replace_size():
    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook = Orderbook("TEST")

    orderbook.new_order(order)

    print(orderbook._show_orderbook())

    order = Order("TEST", 23.54, 120, "B", 2, "NEWORDER_2", "TESTSESSION")

    orderbook.replace_order("NEWORDER_1", order)

    print(orderbook._show_orderbook())

    assert orderbook.bids[23.54][0].quantity == 120
Example #8
0
def test_orderbook_full_execution():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.54, 100, "S", 2, "NEWORDER_2", "TESTSESSION")
    orderbook.new_order(order)

    while not orderbook.trades.empty():
        trade = orderbook.trades.get()

        assert trade.price == 23.54
        assert trade.quantity == 100
        assert trade.exec_id == "TEST_TEST000001"
Example #9
0
def test_orderbook_reject():
    order = Order("HYG", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")

    orderbook = Orderbook("TEST")

    assert orderbook.new_order(
        order) == "[Internal] incorrect orderbook assignment"
Example #10
0
def test_orderbook_partial_aggressive_execution():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 50, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.54, 100, "S", 2, "NEWORDER_2", "TESTSESSION")
    orderbook.new_order(order)

    while not orderbook.trades.empty():
        trade = orderbook.trades.get()

        assert trade.price == 23.54
        assert trade.quantity == 50
        assert trade.exec_id == "TEST_TEST000001"

    assert orderbook.asks[23.54][0].quantity == 50
    assert orderbook.asks[23.54][0].order_id == "NEWORDER_2"
    assert len(orderbook.bids[23.54]) == 0
Example #11
0
def test_orderbook_market_order_execution():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 100, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.59, 50, "S", 1, "NEWORDER_2", "TESTSESSION")
    orderbook.new_order(order)

    assert orderbook.trades.qsize() == 2

    while not orderbook.trades.empty():
        trade = orderbook.trades.get()

        assert trade.price == 23.54
        assert trade.quantity == 50
        assert trade.exec_id == "TEST_TEST000001"

    assert orderbook.bids[23.54][0].quantity == 50
Example #12
0
def run(size: int, prob: float) -> None:
    with Timer() as timer:
        orders = generate(size)
    click.secho(f"Generated {size} orders in {timer.time * 1e6:.3f} µs",
                fg="cyan")
    print()

    # for order in orders:
    #     print(order)

    orderbook = Orderbook("TEST")

    order_add_stats = []
    order_delete_stats = []
    live_orders = []

    while orders:
        if random.uniform(0, 1) < prob:
            order = random.choice(orders)
            with Timer() as timer:
                orderbook.new_order(order)
            order_add_stats.append(timer.time)
            live_orders.append(order.order_id)
            orders.remove(order)
        else:
            if live_orders:
                order_id = random.choice(live_orders)
                with Timer() as timer:
                    orderbook.delete_order(order_id)
                order_delete_stats.append(timer.time)
                live_orders.remove(order_id)

    if order_add_stats:
        click.secho("Order add stats:-", fg="green")
        stats(order_add_stats)

    if order_delete_stats:
        click.secho("Order delete stats:-", fg="green")
        stats(order_delete_stats)
Example #13
0
def test_orderbook_replace_execution():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 150, "B", 2, "TESTSESSION_1_NEWORDER_1",
                  "TESTSESSION_1")
    orderbook.new_order(order)

    order = Order("TEST", 23.55, 130, "S", 2, "TESTSESSION_2_NEWORDER_1",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    order = Order("TEST", 23.55, 140, "B", 2, "TESTSESSION_1_NEWORDER_2",
                  "TESTSESSION_1")
    orderbook.replace_order("TESTSESSION_1_NEWORDER_1", order)

    assert orderbook.trades.qsize() == 2

    while not orderbook.trades.empty():
        trade = orderbook.trades.get()

        assert trade.price == 23.55
        assert trade.quantity == 130

    assert orderbook.bbo() == (23.55, float("inf"))
Example #14
0
def test_orderbook_replace_execution_deletion():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 150, "B", 2, "TESTSESSION_1_NEWORDER_1",
                  "TESTSESSION_1")
    orderbook.new_order(order)

    order = Order("TEST", 23.54, 250, "B", 2, "TESTSESSION_3_NEWORDER_1",
                  "TESTSESSION_3")
    orderbook.new_order(order)

    order = Order("TEST", 23.57, 130, "S", 2, "TESTSESSION_2_NEWORDER_1",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    order = Order("TEST", 23.59, 120, "S", 2, "TESTSESSION_2_NEWORDER_2",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    assert orderbook.bbo() == (23.54, 23.57)

    order = Order("TEST", 23.56, 350, "B", 2, "TESTSESSION_3_NEWORDER_2",
                  "TESTSESSION_3")
    orderbook.replace_order("TESTSESSION_3_NEWORDER_1", order)

    assert orderbook.bbo() == (23.56, 23.57)
    # orderbook.delete_order("TESTSESSION_2_NEWORDER_1")

    order = Order("TEST", 23.56, 351, "S", 2, "TESTSESSION_2_NEWORDER_3",
                  "TESTSESSION_2")
    orderbook.replace_order("TESTSESSION_2_NEWORDER_2", order)

    assert orderbook.trades.qsize() == 2

    assert orderbook.bbo() == (23.54, 23.56)

    while not orderbook.trades.empty():
        trade = orderbook.trades.get()

        assert trade.price == 23.56
        assert trade.quantity == 350

    orderbook.delete_order("TESTSESSION_2_NEWORDER_3")

    assert orderbook.bbo() == (23.54, 23.57)
Example #15
0
def test_orderbook_deletion_bbo():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 150, "B", 2, "TESTSESSION_1_NEWORDER_1",
                  "TESTSESSION_1")
    orderbook.new_order(order)

    order = Order("TEST", 23.55, 130, "S", 2, "TESTSESSION_2_NEWORDER_1",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    order = Order("TEST", 23.56, 120, "S", 2, "TESTSESSION_2_NEWORDER_2",
                  "TESTSESSION_2")
    orderbook.new_order(order)

    assert orderbook.bbo() == (23.54, 23.55)

    assert len(orderbook.asks[23.55]) == 1

    orderbook.delete_order("TESTSESSION_2_NEWORDER_1")

    assert len(orderbook.asks[23.55]) == 0

    assert orderbook.bbo() == (23.54, 23.56)
Example #16
0
def test_orderbook_multiple_execution():
    orderbook = Orderbook("TEST")

    order = Order("TEST", 23.54, 50, "B", 2, "NEWORDER_1", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.54, 30, "B", 2, "NEWORDER_2", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.53, 10, "B", 2, "NEWORDER_3", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.53, 20, "B", 2, "NEWORDER_3_1", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.51, 10, "B", 2, "NEWORDER_4", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.55, 100, "S", 2, "NEWORDER_5", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.56, 30, "S", 2, "NEWORDER_6", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.58, 80, "S", 2, "NEWORDER_7", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.60, 90, "S", 2, "NEWORDER_8", "TESTSESSION")
    orderbook.new_order(order)

    order = Order("TEST", 23.53, 150, "S", 2, "NEWORDER_9", "TESTSESSION")
    orderbook.new_order(order)

    assert orderbook.trades.qsize() == 8

    while not orderbook.trades.empty():
        trade = orderbook.trades.get()

        if trade.price == 23.54:
            assert trade.quantity in [50, 30]
            if trade.quantity == 50 and trade.side == "b":
                assert trade.order_id == "NEWORDER_1"
            elif trade.quantity == 30 and trade.side == "b":
                assert trade.order_id == "NEWORDER_2"
            elif trade.side == "s":
                assert trade.order_id == "NEWORDER_9"

        if trade.price == 23.53:
            assert trade.quantity in [10, 20]

    assert orderbook.best_ask == 23.53
    assert orderbook.best_bid == 23.51
    assert orderbook.asks[23.53][0].order_id == "NEWORDER_9"