Esempio n. 1
0
 def test_order_book_add_order(self):
     order_book = OrderBook()
     order = Order((1, "Limit", "Buy", 100, 100, 0))
     order_book.add(order)
     self.assertEqual(
         order_book.get_state(),
         '{"buyOrders": [{"id": 1, "price": 100, "quantity": 100}], "sellOrders": []}'
     )
Esempio n. 2
0
 def test_order_book_cancel_order(self):
     order_book = OrderBook()
     order = Order((1, "Limit", "Buy", 100, 100, 0))
     order_book.add(order)
     order_book.cancel(order)
     self.assertEqual(order_book.get_state(),
                      '{"buyOrders": [], "sellOrders": []}')
Esempio n. 3
0
def create_order_book_from_orders_list(orders: list[Order],
                                       print_output: bool = False
                                       ) -> OrderBook:
    """
    :param orders:                  list of orders
    :param print_output:            shows single transactions and order book states
    :return:                        order book object with realized transactions according to given orders

    Creates an order book object from a list of order objects
    and performs transactions for the orders if possible.
    """

    order_book = OrderBook(store_transactions=print_output)
    for order in orders:
        order_book.add(order)
        if print_output:
            print(">> " + order.__str__(simple=False))
            print(order_book)

    return order_book
Esempio n. 4
0
    def test_order_book_limit_transactions(self):
        order_book = OrderBook(store_transactions=True)
        order_book.add(Order((1, "Limit", "Buy", 100, 100, 0)))
        self.assertEqual(order_book.last_transactions, [])

        order_book.add(Order((2, "Limit", "Sell", 80, 50, 0)))
        self.assertEqual(order_book.last_transactions, [
            '{"buyOrderId": 1, "sellOrderId": 2, "price": 100, "quantity": 50}'
        ])

        order_book.add(Order((3, "Limit", "Sell", 120, 40, 0)))
        self.assertEqual(order_book.last_transactions, [])

        self.assertEqual(
            order_book.get_state(),
            '{"buyOrders": [{"id": 1, "price": 100, "quantity": 50}],'
            ' "sellOrders": [{"id": 3, "price": 120, "quantity": 40}]}')
Esempio n. 5
0
    def test_order_book_not_storing_transactions(self):
        order_book = OrderBook(store_transactions=False)
        order_book.add(Order((1, "Limit", "Buy", 100, 100, 0)))
        self.assertEqual(order_book.last_transactions, [])

        order_book.add(Order((2, "Limit", "Sell", 100, 100, 0)))
        self.assertEqual(order_book.last_transactions, [])

        self.assertEqual(order_book.get_state(),
                         '{"buyOrders": [], "sellOrders": []}')
Esempio n. 6
0
    def test_order_book_iceberg_transactions(self):
        order_book = OrderBook(store_transactions=True)
        orders = [
            '{"type": "Iceberg", "order": {"direction": "Sell", "id": 1, "price": 100, "quantity": 200, "peak": 100}}',
            '{"type": "Iceberg", "order": {"direction": "Sell", "id": 2, "price": 100, "quantity": 300, "peak": 100}}',
            '{"type": "Iceberg", "order": {"direction": "Sell", "id": 3, "price": 100, "quantity": 200, "peak": 100}}',
            '{"type": "Iceberg", "order": {"direction": "Buy",  "id": 4, "price": 100, "quantity": 500, "peak": 100}}'
        ]

        expected_states = [
            '{"buyOrders": [], "sellOrders": [{"id": 1, "price": 100, "quantity": 100}]}',
            '{"buyOrders": [], "sellOrders":'
            ' [{"id": 1, "price": 100, "quantity": 100}, {"id": 2, "price": 100, "quantity": 100}]}',
            '{"buyOrders": [], "sellOrders":'
            ' [{"id": 1, "price": 100, "quantity": 100}, {"id": 2, "price": 100, "quantity": 100},'
            ' {"id": 3, "price": 100, "quantity": 100}]}',
            '{"buyOrders": [], "sellOrders":'
            ' [{"id": 3, "price": 100, "quantity": 100}, {"id": 2, "price": 100, "quantity": 100}]}'
        ]

        expected_transactions = [
            [], [], [],
            [
                '{"buyOrderId": 4, "sellOrderId": 1, "price": 100, "quantity": 100}',
                '{"buyOrderId": 4, "sellOrderId": 2, "price": 100, "quantity": 100}',
                '{"buyOrderId": 4, "sellOrderId": 3, "price": 100, "quantity": 100}',
                '{"buyOrderId": 4, "sellOrderId": 1, "price": 100, "quantity": 100}',
                '{"buyOrderId": 4, "sellOrderId": 2, "price": 100, "quantity": 100}'
            ]
        ]

        for i in range(4):
            order_book.add(Order(orders[i]))
            self.assertEqual(order_book.get_state(), expected_states[i])
            self.assertEqual(order_book.last_transactions,
                             expected_transactions[i])
Esempio n. 7
0
    delta_time = timer()
    print("Loaded {0} in {1} seconds.".format(input_file, delta_time))

    number_of_transactions = len(orders)
    order_book = create_order_book_from_orders_list(orders,
                                                    print_output=show_details)

    delta_time = timer()
    print("\nFinal order book state:\n" + order_book.get_state())
    print("\nElapsed time: {0} seconds.".format(delta_time))
    print("{:.2f} orders per second.".format(number_of_transactions /
                                             delta_time))
    sys.exit(0)

# run the main program if no special branch has been activated
order_book = OrderBook(store_transactions=True)
print(program_description + """
To add an order to the order book, insert a JSON line.
Type 'exit' to quit the program.\n""")

try:
    for line in sys.stdin:
        if bool(line and not line.isspace()):
            if "exit" in line:
                break

            order = Order(line)

            # we assume that id cannot be negative
            if order.id >= 0:
                order_book.add(order)
Esempio n. 8
0
 def test_order_book_cancel_not_present_order(self):
     order_book = OrderBook()
     order = Order((1, "Limit", "Buy", 100, 100, 0))
     order_book.add(order)
     order_book.cancel(order)
     self.assertRaises(ValueError, order_book.cancel, order)