def test_insert(self): print("Test Insert") bid_levels = orderbook.Levels(orderbook.BUY) bid_levels.insert(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) self.assertEqual(bid_levels.prices, [10]) self.assertEqual(len(bid_levels.levels), 1) print(bid_levels) bid_levels.insert(orderbook.Order("trader1", 1, orderbook.BUY, 10, 7)) self.assertEqual(bid_levels.prices, [10]) self.assertEqual(len(bid_levels.levels), 1) print(bid_levels) ask_levels = orderbook.Levels(orderbook.SELL) ask_levels.insert(orderbook.Order("trader1", 0, orderbook.SELL, 10, 5)) self.assertEqual(ask_levels.prices, [10]) self.assertEqual(len(ask_levels.levels), 1) print(ask_levels) ask_levels.insert(orderbook.Order("trader1", 1, orderbook.SELL, 10, 7)) self.assertEqual(ask_levels.prices, [10]) self.assertEqual(len(ask_levels.levels), 1) print(ask_levels)
def test_insert(self): print("Test Insert") level = orderbook.Level(10, orderbook.BUY) order = orderbook.Order("trader1", 0, orderbook.BUY, 10, 5) level.insert(order) self.assertEqual(level.volume, 5) self.assertEqual(len(level.level), 1) print(level) order = orderbook.Order("trader1", 3, orderbook.BUY, 10, 10) level.insert(order) self.assertEqual(level.volume, 15) self.assertEqual(len(level.level), 2) print(level) bad_order = orderbook.Order("trader1", 3, orderbook.SELL, 10, 10) self.assertRaises(AssertionError, lambda: level.insert(bad_order)) bad_order = orderbook.Order("trader1", 3, orderbook.BUY, 9, 10) self.assertRaises(AssertionError, lambda: level.insert(bad_order))
def test_cancel(self): print("Test Cancel") bid_levels = orderbook.Levels(orderbook.BUY) self.assertEqual(bid_levels.cancel(4), False) bid_levels.insert(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) self.assertEqual(bid_levels.prices, [10]) self.assertEqual(len(bid_levels.levels), 1) print(bid_levels) self.assertEqual(bid_levels.cancel(0), True) self.assertEqual(bid_levels.prices, []) self.assertEqual(bid_levels.levels, {}) self.assertEqual(len(bid_levels.levels), 0) print(bid_levels) bid_levels.insert(orderbook.Order("trader1", 1, orderbook.BUY, 10, 5)) bid_levels.insert(orderbook.Order("trader1", 2, orderbook.BUY, 12, 7)) bid_levels.insert(orderbook.Order("trader1", 3, orderbook.BUY, 13, 10)) self.assertEqual(bid_levels.prices, [13, 12, 10]) self.assertEqual(len(bid_levels.levels), 3) print(bid_levels) self.assertEqual(bid_levels.cancel(2), True) self.assertEqual(bid_levels.prices, [13, 10]) self.assertEqual(len(bid_levels.levels), 2) print(bid_levels)
def test_insert_order(self): "Test Orderbook Insert Order" saved_trades = [] def trade_handler(trades, order_id): saved_trades.extend(trades) ob = orderbook.OrderBook(trade_handler) ob.insert_order(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) ob.insert_order(orderbook.Order("trader1", 1, orderbook.BUY, 10, 3)) self.assertEqual(ob.bid_levels.prices, [10]) self.assertEqual(saved_trades, []) ob.insert_order(orderbook.Order("trader1", 2, orderbook.SELL, 11, 7)) ob.insert_order(orderbook.Order("trader1", 3, orderbook.SELL, 12, 4)) self.assertEqual(ob.ask_levels.prices, [11, 12]) self.assertEqual(saved_trades, []) print(ob) ob.insert_order(orderbook.Order("trader2", 4, orderbook.SELL, 10, 10)) self.assertEqual(ob.bid_levels.prices, []) self.assertEqual(len(saved_trades), 2) print(ob)
def test_match(self): level = self.set_level() self.assertEqual(level.volume, 27) self.assertEqual(len(level.level), 3) print("Test Match") print(level) order = orderbook.Order("trader2", 4, orderbook.SELL, 10, 2) trades, matched = level.match(order) print(",".join(map(str, trades))) print(level) self.assertEqual(matched, 2) self.assertEqual(len(trades), 1) self.assertEqual(level.volume, 25) self.assertEqual(len(level.level), 3) order = orderbook.Order("trader2", 5, orderbook.SELL, 10, 3) trades, matched = level.match(order) print(",".join(map(str, trades))) print(level) self.assertEqual(matched, 3) self.assertEqual(len(trades), 1) self.assertEqual(level.volume, 22) self.assertEqual(len(level.level), 2) order = orderbook.Order("trader2", 5, orderbook.SELL, 10, 12) trades, matched = level.match(order) print(",".join(map(str, trades))) print(level) self.assertEqual(matched, 12) self.assertEqual(len(trades), 2) self.assertEqual(level.volume, 10) self.assertEqual(len(level.level), 1) # TODO: Orders that take out the whole level should be left in order = orderbook.Order("trader2", 5, orderbook.SELL, 10, 15) trades, matched = level.match(order) print(",".join(map(str, trades))) print(level)
def test_cancel_order(self): "Test Orderbook Insert Order" ob = orderbook.OrderBook() ob.insert_order(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) ob.insert_order(orderbook.Order("trader1", 1, orderbook.BUY, 10, 3)) self.assertEqual(ob.bid_levels.prices, [10]) ob.insert_order(orderbook.Order("trader1", 2, orderbook.SELL, 11, 7)) ob.insert_order(orderbook.Order("trader1", 3, orderbook.SELL, 12, 4)) self.assertEqual(ob.ask_levels.prices, [11, 12]) print(ob) self.assertEqual(ob.cancel_order(2), True) self.assertEqual(ob.bid_levels.prices, [10]) self.assertEqual(ob.ask_levels.prices, [12]) print(ob)
def test_insert_order(self): "Test Orderbook Insert Order Hit Through" saved_trades = [] def trade_handler(trades, order_id): saved_trades.extend(trades) ob = orderbook.OrderBook(trade_handler) ob.insert_order(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) ob.insert_order(orderbook.Order("trader1", 1, orderbook.BUY, 11, 5)) ob.insert_order(orderbook.Order("trader1", 2, orderbook.BUY, 12, 5)) self.assertEqual(ob.bid_levels.prices, [12, 11, 10]) ob.insert_order(orderbook.Order("trader1", 3, orderbook.SELL, 10, 8)) self.assertEqual(len(saved_trades), 2) self.assertEqual(saved_trades[0].volume, 5) self.assertEqual(saved_trades[0].price, 12) self.assertEqual(saved_trades[1].volume, 3) self.assertEqual(saved_trades[1].price, 11)
def test_match(self): print("Test Match") bid_levels = orderbook.Levels(orderbook.BUY) bid_levels.insert(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) bid_levels.insert(orderbook.Order("trader1", 1, orderbook.BUY, 10, 3)) bid_levels.insert(orderbook.Order("trader1", 2, orderbook.BUY, 12, 7)) bid_levels.insert(orderbook.Order("trader1", 3, orderbook.BUY, 13, 10)) self.assertEqual(bid_levels.prices, [13, 12, 10]) self.assertEqual(len(bid_levels.levels), 3) print(bid_levels) aggressing_order = orderbook.Order("trader2", 5, orderbook.SELL, 14, 1) trades = bid_levels.match(aggressing_order) self.assertEqual(trades, []) aggressing_order = orderbook.Order("trader2", 5, orderbook.SELL, 13, 1) trades = bid_levels.match(aggressing_order) self.assertEqual(len(trades), 1) self.assertEqual(bid_levels.prices, [13, 12, 10]) self.assertEqual(len(bid_levels.levels), 3) print(", ".join(map(str, trades))) aggressing_order = orderbook.Order("trader2", 5, orderbook.SELL, 12, 1) trades = bid_levels.match(aggressing_order) self.assertEqual(trades[0].price, 13) print(", ".join(map(str, trades))) aggressing_order = orderbook.Order("trader2", 5, orderbook.SELL, 12, 10) trades = bid_levels.match(aggressing_order) self.assertEqual(len(trades), 2) self.assertEqual(bid_levels.prices, [12, 10]) self.assertEqual(trades[0].price, 13) self.assertEqual(trades[1].price, 12) print(bid_levels) print(", ".join(map(str, trades)))
def set_level(self): level = orderbook.Level(10, orderbook.BUY) level.insert(orderbook.Order("trader1", 0, orderbook.BUY, 10, 5)) level.insert(orderbook.Order("trader1", 1, orderbook.BUY, 10, 10)) level.insert(orderbook.Order("trader1", 2, orderbook.BUY, 10, 12)) return level