Exemplo n.º 1
0
def test_order_book_cannot_match_non_crossing_orders():
    instrument_id = "AAPL"
    quantity = 100
    price = 10
    limit_orders = [
        LimitOrder(instrument_id=instrument_id,
                   order_direction=OrderDirection.buy if i %
                   2 else OrderDirection.sell,
                   quantity=quantity,
                   price=price + (-i if i % 2 else i)) for i in range(10)
    ]

    order_book = OrderBook()

    for order in limit_orders:
        order_book.add_order(order)
    order_book.match()
    assert len(order_book.bids) == 4, "Test Failed: There should be 5 bids"
    assert len(order_book.asks) == 4, "Test Failed: There should be 5 asks"
    assert order_book.best_bid is not None, "Test Failed: best_bid should not be empty"
    assert order_book.best_ask is not None, "Test Failed: best_ask should not be empty"
    assert order_book.best_bid.price <= order_book.best_ask.price, "Test Failed: best prices are not aligned"
    assert all(order_book.bids[i].price <= order_book.asks[i].price for i in range(4)), \
        "Test Failed: prices are not aligned"
    assert not order_book.trades, "Test Failed: trades should be empty"
    assert not order_book.complete_orders, "Test Failed: complete_orders should be empty"
    assert not order_book.attempt_match, "Test Failed: attempt_match should be False"
    pass
Exemplo n.º 2
0
def test_order_book_can_match_orders():
    instrument_id = "AAPL"
    quantity = 100
    price = 10
    limit_orders = [
        LimitOrder(instrument_id=instrument_id,
                   order_direction=OrderDirection.buy if i %
                   2 else OrderDirection.sell,
                   quantity=quantity,
                   price=price + (i if i % 2 else -i)) for i in range(10)
    ]

    order_book = OrderBook()

    for order in limit_orders:
        order_book.add_order(order)
    order_book.match()
    assert not order_book.bids, "Test Failed: There should be no bids after complete matching"
    assert not order_book.asks, "Test Failed: There should be no asks after complete matching"
    assert order_book.best_bid is None, "Test Failed: best_bid should be empty"
    assert order_book.best_ask is None, "Test Failed: best_ask should be empty"
    assert len(
        order_book.trades) == 5, "Test Failed: trades should have 5 orders"
    assert len(order_book.complete_orders
               ) == 10, "Test Failed: complete_orders should have all orders"
    assert not order_book.attempt_match, "Test Failed: attempt_match should be False"
    pass
Exemplo n.º 3
0
def test_order_book_can_handle_limit_and_market_orders_together():
    """ Here there are more asks than bids, so the bids will fill"""
    instrument_id = "AAPL"
    quantity = 100
    price = 10
    limit_orders = [
        LimitOrder(instrument_id=instrument_id,
                   order_direction=OrderDirection.buy,
                   quantity=quantity - 10 * i,
                   price=price + i) for i in range(5)
    ]

    market_order = MarketOrder(instrument_id=instrument_id,
                               order_direction=OrderDirection.sell,
                               quantity=400)

    order_book = OrderBook()

    for order in limit_orders:
        order_book.add_order(order)
    order_book.add_order(market_order)
    order_book.match()

    assert not order_book.asks, "Test Failed: There should be no asks after this matching"
    assert order_book.best_ask is None, "Test Failed: best_ask should be empty"
    assert not order_book.bids, "Test Failed: There should be  no bids after this matching"
    assert order_book.best_bid is None, "Test Failed: best_bid should be empty"
    assert len(order_book.trades) == 5, "Test Failed: there should be 5 trades"
    assert len(order_book.complete_orders
               ) == 6, "Test Failed: complete_orders should have 6 orders"
    assert not order_book.attempt_match, "Test Failed: attempt_match should be False"
    pass
Exemplo n.º 4
0
def test_order_book_can_match_incomplete_more_bids():
    """ Here there are more asks than bids, so the bids will fill"""
    instrument_id = "AAPL"
    quantity = 100
    price = 10
    limit_orders = [
        LimitOrder(instrument_id=instrument_id,
                   order_direction=OrderDirection.buy
                   if not i % 2 else OrderDirection.sell,
                   quantity=quantity - 10 * i,
                   price=price + (i if not i % 2 else -i)) for i in range(10)
    ]

    order_book = OrderBook()

    for order in limit_orders:
        order_book.add_order(order)
    order_book.match()

    assert not order_book.asks, "Test Failed: There should be no asks after this matching"
    assert order_book.best_ask is None, "Test Failed: best_ask should be empty"
    assert not order_book.bids, "Test Failed: There should be  no bids after this matching"
    assert order_book.best_bid is not None, "Test Failed: best_bid should not be empty"
    assert len(
        order_book.trades) > 5, "Test Failed: trades should more than 5 trades"
    assert len(
        order_book.complete_orders
    ) < 10, "Test Failed: complete_orders should have fewer than 10 orders"
    assert not order_book.attempt_match, "Test Failed: attempt_match should be False"
    pass
Exemplo n.º 5
0
def test_order_book_can_generate_execution_plot():
    instrument_id = "AAPL"
    quantity = 100
    price = 10
    limit_orders = [
        LimitOrder(instrument_id=instrument_id,
                   order_direction=OrderDirection.buy
                   if not i % 2 else OrderDirection.sell,
                   quantity=quantity - 10 * i,
                   price=price + (i if not i % 2 else -2 * i))
        for i in range(10)
    ]

    order_book = OrderBook()

    for order in limit_orders:
        order_book.add_order(order)
    order_book.match()
    order_book.plot_executions()
    pass
Exemplo n.º 6
0
def test_order_book_can_cancel_sell():
    """ Here there are more asks than bids, so the bids will fill"""
    instrument_id = "AAPL"
    quantity = 100
    price = 10
    limit_orders = [
        LimitOrder(instrument_id=instrument_id,
                   order_direction=OrderDirection.buy if i %
                   2 else OrderDirection.sell,
                   quantity=quantity - 10 * i,
                   price=price + (i if i % 2 else -i)) for i in range(10)
    ]
    for i, l in enumerate(limit_orders):
        l.order_id = i
        limit_orders[i] = l

    order_book = OrderBook()

    for order in limit_orders:
        order_book.add_order(order)
    cancel_order = CancelOrder(instrument_id=instrument_id,
                               order_id=0,
                               order_direction=OrderDirection.sell)
    order_book.match()
    order_book.add_order(cancel_order)

    assert not order_book.asks, "Test Failed: There should be no asks after this matching"
    assert order_book.best_ask is None, "Test Failed: best_ask should be empty"
    assert not order_book.bids, "Test Failed: There should be  no bids after this matching"
    assert order_book.best_bid is None, "Test Failed: best_bid should be empty"
    assert len(
        order_book.trades) > 5, "Test Failed: trades should more than 5 trades"
    assert len(order_book.complete_orders
               ) == 10, "Test Failed: complete_orders should have 10 orders"
    assert cancel_order.cancel_success, "Test Failed: cancel should succeed"
    pass