Пример #1
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
def test_market_order_buy_can_handle_trade():
    instrument_id = "AAPL"
    order_direction = OrderDirection.buy
    quantity = 100
    market_order = MarketOrder(instrument_id=instrument_id,
                               order_direction=order_direction,
                               quantity=quantity)
    trade = Trade(datetime=np.datetime64("2020-01-01"), price=10, quantity=10)
    market_order.update_on_trade(trade)
    assert market_order.quantity == quantity, "Test failed, incorrect quantity"
    assert market_order.order_direction == order_direction, "Test failed, incorrect quantity"
    assert market_order.price == float('inf'), "Test failed, incorrect price"
    assert market_order.instrument_id == instrument_id, "Test failed, incorrect instrument_id"
    assert market_order.order_type == OrderType.market, "Test failed, incorrect order type"
    assert market_order.fill_info, "Test failed, no fill info"
    assert market_order.unfilled_quantity == quantity - \
        10, "Test failed, incorrect unfilled quantity"
    pass
def test_market_order_incorrect_order_direction():
    with pytest.raises(InvalidOrderDirectionException):
        instrument_id = "AAPL"
        order_direction = OrderDirection.test
        quantity = 100
        MarketOrder(instrument_id=instrument_id,
                    order_direction=order_direction,
                    quantity=quantity)
    pass
Пример #4
0
def get_data(n):
    instrument_ids = ["AAPL", "MSFT", "TSLA", "FB", "NFLX"]
    quantity = 100
    price = 40
    m = n // len(instrument_ids)
    output = []
    for instr in instrument_ids:
        buy_limits = [
            LimitOrder(instrument_id=instr,
                       order_direction=OrderDirection.buy,
                       quantity=quantity + random.uniform(-50, 50),
                       price=price + random.uniform(-2.5, 2.5))
            for i in range(m // 4)
        ]
        sell_limits = [
            LimitOrder(instrument_id=instr,
                       order_direction=OrderDirection.sell,
                       quantity=quantity + random.uniform(-50, 50),
                       price=price + random.uniform(-2.5, 2.5))
            for i in range(m // 4)
        ]

        buy_markets = [
            MarketOrder(instrument_id=instr,
                        order_direction=OrderDirection.buy,
                        quantity=quantity + random.uniform(-50, 50))
            for i in range(m // 4)
        ]

        sell_markets = [
            MarketOrder(instrument_id=instr,
                        order_direction=OrderDirection.sell,
                        quantity=quantity + random.uniform(-50, 50))
            for i in range(m // 4)
        ]
        output += buy_limits + sell_limits + buy_markets + sell_markets
        random.shuffle(output)
    return output
def test_market_order_sell():
    instrument_id = "AAPL"
    order_direction = OrderDirection.sell
    quantity = 100
    market_order = MarketOrder(instrument_id=instrument_id,
                               order_direction=order_direction,
                               quantity=quantity)

    assert market_order.quantity == quantity, "Test failed, incorrect quantity"
    assert market_order.order_direction == order_direction, "Test failed, incorrect quantity"
    assert market_order.price == 0, "Test failed, incorrect price for market order"
    assert market_order.instrument_id == instrument_id, "Test failed, incorrect instrument_id"
    assert market_order.order_type == OrderType.market, "Test failed, incorrect order type"
    assert market_order.unfilled_quantity == quantity, "Test failed, incorrect unfilled quantity"
    pass
def test_market_order_sell_can_handle_multiple_trades_to_completion():
    instrument_id = "AAPL"
    order_direction = OrderDirection.sell
    quantity = 100
    market_order = MarketOrder(instrument_id=instrument_id,
                               order_direction=order_direction,
                               quantity=quantity)
    trade = Trade(datetime=np.datetime64("2020-01-01"), price=10, quantity=10)
    market_order.update_on_trade(trade)
    trade = Trade(datetime=np.datetime64("2020-01-01"), price=10, quantity=90)
    market_order.update_on_trade(trade)
    assert market_order.quantity == quantity, "Test failed, incorrect quantity"
    assert market_order.order_direction == order_direction, "Test failed, incorrect quantity"
    assert market_order.price == 0, "Test failed, incorrect price for market order"
    assert market_order.instrument_id == instrument_id, "Test failed, incorrect instrument_id"
    assert market_order.order_type == OrderType.market, "Test failed, incorrect order type"
    assert market_order.fill_info, "Test failed, no fill info"
    assert market_order.unfilled_quantity == 0, "Test failed, incorrect unfilled quantity"
    assert market_order.status == OrderStatus.filled, "Test failed, incorrect status"
    pass