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_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 #3
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 #4
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)