Ejemplo n.º 1
0
    def test_processOrder_ask_LIM03(self):
        exchange = Exchange()
        exchange.asks = Orderbook_half('Ask', 1)
        exchange.bids = Orderbook_half('Bid', 100)
        order01 = Order('T02', 'Bid', 13, 1, 1, 0, 'LIM')
        order02 = Order('T01', 'Bid', 14, 3, 1, 0, 'LIM')
        order03 = Order('T03', 'Bid', 15, 2, 1, 0, 'LIM')
        exchange.add_order(order01, False)
        exchange.add_order(order03, False)
        exchange.add_order(order02, False)
        order02 = Order('T01', 'Ask', 6, 10, 1, 0, 'LIM')
        transac, actual = exchange.process_order2(12, order02, False)
        self.assertEqual(exchange.asks.best_qty, 7)
        self.assertEqual(exchange.asks.best_price, 6)

        self.assertEqual(exchange.bids.best_price, 14)
        self.assertEqual(exchange.bids.best_qty, 3)
        self.assertEqual(exchange.asks.n_orders, 1)
        self.assertEqual(exchange.bids.n_orders, 1)

        self.assertEqual(transac[0]['party1'], 'T03')
        self.assertEqual(transac[0]['qty'], 2)
        self.assertEqual(transac[1]['party1'], 'T02')
        self.assertEqual(transac[1]['qty'], 1)
        self.assertEqual(actual, 3)
Ejemplo n.º 2
0
 def test_empty_processorder(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('asks', 1)
     exchange.bids = Orderbook_half('bids', 100)
     order01 = Order('T01', 'Bid', 3, 1, 1, 0, 'MKT')
     transac, qty = exchange.process_order2(3, order01, False)
     self.assertEqual(qty, 0)
     self.assertEqual(exchange.bids.lob, collections.OrderedDict())
     self.assertEqual(exchange.asks.lob, collections.OrderedDict())
Ejemplo n.º 3
0
 def test_add_and_del_order02(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('asks', 1)
     exchange.bids = Orderbook_half('bids', 100)
     order01 = Order('T01', 'Bid', 3, 1, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     self.assertEqual(exchange.bids.n_orders, 1)
     exchange.del_order(2, order01, False)
     self.assertEqual(exchange.bids.n_orders, 0)
Ejemplo n.º 4
0
 def test_empty_po_025(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('asks', 1)
     exchange.bids = Orderbook_half('bids', 100)
     order00 = Order('T01', 'Ask', 10, 1, 1, 0, 'LIM')
     order01 = Order('T01', 'Bid', 3, 2, 1, 0, 'MKT')
     exchange.add_order(order00, False)
     exchange.process_order2(3, order01, False)
     self.assertEqual(exchange.bids.lob, collections.OrderedDict())
     self.assertEqual(exchange.asks.lob, collections.OrderedDict())
Ejemplo n.º 5
0
 def test_dec_bid(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order01 = Order('T01', 'Bid', 3, 5, 1, 0, 'LIM')
     order03 = Order('T02', 'Bid', 5, 2, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     exchange.add_order(order03, False)
     exchange.bids.decrement_order(3, 'T01', 5)
     self.assertEqual(exchange.bids.n_orders, 1)
     self.assertEqual(exchange.bids.best_qty, 2)
Ejemplo n.º 6
0
 def test_processOrder_exchange_empty01(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order02 = Order('T03', 'Bid', 6, 10, 1, 0, 'LIM')
     transac, actual = exchange.process_order2(12, order02, False)
     self.assertEqual(exchange.bids.best_qty, 10)
     self.assertEqual(exchange.bids.best_price, 6)
     self.assertEqual(exchange.bids.n_orders, 1)
     self.assertEqual(len(transac), 0)
     self.assertEqual(actual, 0)
Ejemplo n.º 7
0
 def test_processOrder_bid_wrong_price(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order01 = Order('T02', 'Ask', 13, 1, 1, 0, 'LIM')
     order02 = Order('T01', 'Ask', 14, 3, 1, 0, 'LIM')
     order03 = Order('T03', 'Ask', 15, 2, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     exchange.add_order(order03, False)
     exchange.add_order(order02, False)
     order02 = Order('T01', 'Bid', 10, 6, 1, 0, 'LIM')
     transac, actual = exchange.process_order2(12, order02, False)
     self.assertEqual(len(transac), 0)
Ejemplo n.º 8
0
 def test_delete_best_bid03(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order01 = Order('T02', 'Bid', 3, 2, 1, 0, 'LIM')
     order03 = Order('T01', 'Bid', 5, 10, 1, 0, 'LIM')
     order02 = Order('T01', 'Ask', 2, 3, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     exchange.add_order(order03, False)
     best_price_counterparty, order_del_qid, del_in_trader, trade_price, quantity_decremented = exchange.bids.delete_best(
         order02.tid, order02.qty)
     self.assertEqual(quantity_decremented, 2)
     self.assertEqual(exchange.bids.n_orders, 1)
     self.assertEqual(trade_price, 3)
Ejemplo n.º 9
0
 def test_dec_ask02(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order01 = Order('T01', 'Ask', 3, 1, 1, 0, 'LIM')
     order03 = Order('T02', 'Ask', 5, 2, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     exchange.add_order(order03, False)
     exchange.asks.decrement_order(3, 'T01', 1)
     self.assertEqual(exchange.asks.n_orders, 1)
     self.assertEqual(
         exchange.asks.lob,
         collections.OrderedDict([(5, [2, [[1, 2, 'T02', 1]]])]))
     self.assertEqual(exchange.asks.best_qty, 2)
Ejemplo n.º 10
0
 def test_adding_exchange_bid(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('asks', 1)
     exchange.bids = Orderbook_half('bids', 100)
     order01 = Order('T01', 'Bid', 3, 1, 1, 0, 'LIM')
     order03 = Order('T01', 'Bid', 5, 2, 1, 0, 'LIM')
     exchange.bids.book_add(order01)
     exchange.bids.book_add(order03)
     self.assertEqual(exchange.bids.n_orders, 1)
     self.assertEqual(
         exchange.bids.lob,
         collections.OrderedDict([(5, [2, [[1, 2, 'T01', 0]]])]))
     order02 = Order('T01', 'Bid', 6, 3, 1, 0, 'LIM')
     exchange.bids.book_add(order02)
     self.assertEqual(exchange.bids.n_orders, 1)
Ejemplo n.º 11
0
 def test_dec_bid(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order01 = Order('T01', 'Bid', 3, 5, 1, 0, 'LIM')
     order03 = Order('T02', 'Bid', 5, 10, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     exchange.add_order(order03, False)
     exchange.bids.decrement_order(5, 'T02', 1)
     self.assertEqual(exchange.bids.n_orders, 2)
     self.assertEqual(exchange.bids.best_qty, 9)
     self.assertEqual(
         exchange.bids.lob,
         collections.OrderedDict([(5, [9, [[1, 9, 'T02', 1]]]),
                                  (3, [5, [[1, 5, 'T01', 0]]])]))
Ejemplo n.º 12
0
 def test_processOrder_exchange_MKT06(self):
     exchange = Exchange()
     exchange.asks = Orderbook_half('Ask', 1)
     exchange.bids = Orderbook_half('Bid', 100)
     order01 = Order('T01', 'Ask', 3, 3, 1, 0, 'LIM')
     order03 = Order('T02', 'Ask', 5, 2, 1, 0, 'LIM')
     exchange.add_order(order01, False)
     exchange.add_order(order03, False)
     order02 = Order('T03', 'Bid', 6, 2, 1, 0, 'MKT')
     transac, actual = exchange.process_order2(12, order02, False)
     self.assertEqual(exchange.bids.best_qty, 0)
     self.assertEqual(exchange.bids.n_orders, 0)
     self.assertEqual(exchange.asks.n_orders, 2)
     self.assertEqual(transac[0]['party1'], 'T01')
     self.assertEqual(transac[0]['qty'], 2)
     self.assertEqual(actual, 2)
Ejemplo n.º 13
0
    def test_processOrder_bid_wp_mkt(self):
        exchange = Exchange()
        exchange.asks = Orderbook_half('Ask', 1)
        exchange.bids = Orderbook_half('Bid', 100)
        order01 = Order('T02', 'Ask', 13, 1, 1, 0, 'LIM')
        order02 = Order('T01', 'Ask', 14, 3, 1, 0, 'LIM')
        order03 = Order('T03', 'Ask', 15, 2, 1, 0, 'LIM')
        exchange.add_order(order01, False)
        exchange.add_order(order03, False)
        exchange.add_order(order02, False)
        order02 = Order('T04', 'Bid', 5, 10, 1, 0, 'MKT')
        transac, actual = exchange.process_order2(12, order02, False)
        self.assertEqual(exchange.asks.n_orders, 0)
        self.assertEqual(exchange.bids.n_orders, 0)

        self.assertEqual(transac[0]['party1'], 'T02')
        self.assertEqual(transac[0]['qty'], 1)
        self.assertEqual(transac[1]['party1'], 'T01')
        self.assertEqual(transac[1]['qty'], 3)
        self.assertEqual(transac[2]['party1'], 'T03')
        self.assertEqual(transac[2]['qty'], 2)

        self.assertEqual(actual, 6)