def test_bid_side_depth(self): # Test for bid side depth self.order_book.insert_bid(self.bid) self.order_book.insert_bid(self.bid2) self.assertEqual(300, self.order_book.bid_side_depth(Price(1, 10, 'MB', 'BTC'))) self.assertEqual([(Price(1, 10, 'MB', 'BTC'), 300), (Price(15, 100, 'MB', 'BTC'), 200)], self.order_book.get_bid_side_depth_profile('MB', 'BTC'))
def test_ask_side_depth(ask, ask2, order_book): # Test for ask side depth order_book.insert_ask(ask) order_book.insert_ask(ask2) assert order_book.ask_side_depth(Price(3, 10, 'MB', 'BTC')) == 100 assert order_book.get_ask_side_depth_profile('MB', 'BTC') == \ [(Price(75, 1000, 'MB', 'BTC'), 400), (Price(3, 10, 'MB', 'BTC'), 100)]
def test_bid_side_depth(bid, bid2, order_book): # Test for bid side depth order_book.insert_bid(bid) order_book.insert_bid(bid2) assert order_book.bid_side_depth(Price(1, 10, 'MB', 'BTC')) == 300 assert order_book.get_bid_side_depth_profile('MB', 'BTC') == \ [(Price(1, 10, 'MB', 'BTC'), 300), (Price(15, 100, 'MB', 'BTC'), 200)]
def test_ask_side_depth(self): # Test for ask side depth self.order_book.insert_ask(self.ask) self.order_book.insert_ask(self.ask2) self.assertEqual(100, self.order_book.ask_side_depth(Price(3, 10, 'MB', 'BTC'))) self.assertEqual([(Price(75, 1000, 'MB', 'BTC'), 400), (Price(3, 10, 'MB', 'BTC'), 100)], self.order_book.get_ask_side_depth_profile('MB', 'BTC'))
def get_bid_ask_spread(self, price_wallet_id, quantity_wallet_id): """ Return the spread between the bid and the ask price :rtype: Price """ spread = self.get_ask_price(price_wallet_id, quantity_wallet_id).frac - \ self.get_bid_price(price_wallet_id, quantity_wallet_id).frac return Price(spread.numerator, spread.denominator, price_wallet_id, quantity_wallet_id)
def test_min_price(self): # Test min price (list) self.assertEqual(None, self.side.get_min_price_list('MB', 'BTC')) self.assertEqual(None, self.side.get_min_price('MB', 'BTC')) self.side.insert_tick(self.tick) self.side.insert_tick(self.tick2) self.assertEqual(Price(1, 4, 'MB', 'BTC'), self.side.get_min_price('MB', 'BTC'))
def setUp(self): # Object creation self.price_level_list = PriceLevelList() self.price_level_list2 = PriceLevelList() self.price = Price(1, 1, 'BTC', 'MB') self.price2 = Price(2, 1, 'BTC', 'MB') self.price3 = Price(3, 1, 'BTC', 'MB') self.price4 = Price(4, 1, 'BTC', 'MB') self.price_level = PriceLevel(self.price) self.price_level2 = PriceLevel(self.price2) self.price_level3 = PriceLevel(self.price3) self.price_level4 = PriceLevel(self.price4) # Fill price level list self.price_level_list.insert(self.price_level) self.price_level_list.insert(self.price_level2) self.price_level_list.insert(self.price_level3) self.price_level_list.insert(self.price_level4)
def test_max_price(self): # Test max price (list) self.assertEquals(None, self.side.get_max_price('MB', 'BTC')) self.assertEquals(None, self.side.get_max_price_list('MB', 'BTC')) self.side.insert_tick(self.tick) self.side.insert_tick(self.tick2) self.assertEquals(Price(1, 2, 'MB', 'BTC'), self.side.get_max_price('MB', 'BTC'))
def setUp(self): yield super(TickEntryTestSuite, self).setUp() # Object creation tick = Tick(OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), True) tick2 = Tick(OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(63400, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), True) self.price_level = PriceLevel(Price(100, 1, 'MB', 'BTC')) self.tick_entry = TickEntry(tick, self.price_level) self.tick_entry2 = TickEntry(tick2, self.price_level)
def setUp(self): # Object creation tick = Tick(OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MC')), Timeout(100), Timestamp.now(), True) tick2 = Tick(OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MC')), Timeout(100), Timestamp.now(), True) self.price_level = PriceLevel(Price(50, 5, 'MC', 'BTC')) self.tick_entry1 = TickEntry(tick, self.price_level) self.tick_entry2 = TickEntry(tick, self.price_level) self.tick_entry3 = TickEntry(tick, self.price_level) self.tick_entry4 = TickEntry(tick, self.price_level) self.tick_entry5 = TickEntry(tick2, self.price_level)
def setUp(self): self.price1 = Price(4, 2, 'MB', 'BTC') self.price2 = Price(3, 1, 'MB', 'BTC') self.price3 = Price(8, 4, 'MB', 'BTC')
def test_priority(bid_order, queue): """ Test the priority mechanism of the queue """ order_id = OrderId(TraderId(b'1' * 20), OrderNumber(1)) other_order_id = OrderId(TraderId(b'2' * 20), OrderNumber(1)) queue.insert(1, Price(1, 1, 'DUM1', 'DUM2'), order_id, other_order_id) queue.insert(0, Price(1, 1, 'DUM1', 'DUM2'), order_id, other_order_id) queue.insert(2, Price(1, 1, 'DUM1', 'DUM2'), order_id, other_order_id) item1 = queue.delete() item2 = queue.delete() item3 = queue.delete() assert item1[0] == 0 assert item2[0] == 1 assert item3[0] == 2 # Same retries, different prices queue.insert(1, Price(1, 1, 'DUM1', 'DUM2'), order_id, other_order_id) queue.insert(1, Price(1, 2, 'DUM1', 'DUM2'), order_id, other_order_id) queue.insert(1, Price(1, 3, 'DUM1', 'DUM2'), order_id, other_order_id) item1 = queue.delete() item2 = queue.delete() item3 = queue.delete() assert item1[1] == Price(1, 1, 'DUM1', 'DUM2') assert item2[1] == Price(1, 2, 'DUM1', 'DUM2') assert item3[1] == Price(1, 3, 'DUM1', 'DUM2') # Test with bid order queue = MatchPriorityQueue(bid_order) queue.insert(1, Price(1, 1, 'DUM1', 'DUM2'), order_id, other_order_id) queue.insert(1, Price(1, 2, 'DUM1', 'DUM2'), order_id, other_order_id) queue.insert(1, Price(1, 3, 'DUM1', 'DUM2'), order_id, other_order_id) item1 = queue.delete() item2 = queue.delete() item3 = queue.delete() assert item1[1] == Price(1, 3, 'DUM1', 'DUM2') assert item2[1] == Price(1, 2, 'DUM1', 'DUM2') assert item3[1] == Price(1, 1, 'DUM1', 'DUM2')
def test_priority(self): """ Test the priority mechanism of the queue """ order_id = OrderId(TraderId(b'1' * 20), OrderNumber(1)) self.queue.insert(1, Price(1, 1, 'DUM1', 'DUM2'), order_id) self.queue.insert(0, Price(1, 1, 'DUM1', 'DUM2'), order_id) self.queue.insert(2, Price(1, 1, 'DUM1', 'DUM2'), order_id) item1 = self.queue.delete() item2 = self.queue.delete() item3 = self.queue.delete() self.assertEqual(item1[0], 0) self.assertEqual(item2[0], 1) self.assertEqual(item3[0], 2) # Same retries, different prices self.queue.insert(1, Price(1, 1, 'DUM1', 'DUM2'), order_id) self.queue.insert(1, Price(1, 2, 'DUM1', 'DUM2'), order_id) self.queue.insert(1, Price(1, 3, 'DUM1', 'DUM2'), order_id) item1 = self.queue.delete() item2 = self.queue.delete() item3 = self.queue.delete() self.assertEqual(item1[1], Price(1, 1, 'DUM1', 'DUM2')) self.assertEqual(item2[1], Price(1, 2, 'DUM1', 'DUM2')) self.assertEqual(item3[1], Price(1, 3, 'DUM1', 'DUM2')) # Test with bid order self.queue = MatchPriorityQueue(self.bid_order) self.queue.insert(1, Price(1, 1, 'DUM1', 'DUM2'), order_id) self.queue.insert(1, Price(1, 2, 'DUM1', 'DUM2'), order_id) self.queue.insert(1, Price(1, 3, 'DUM1', 'DUM2'), order_id) item1 = self.queue.delete() item2 = self.queue.delete() item3 = self.queue.delete() self.assertEqual(item1[1], Price(1, 3, 'DUM1', 'DUM2')) self.assertEqual(item2[1], Price(1, 2, 'DUM1', 'DUM2')) self.assertEqual(item3[1], Price(1, 1, 'DUM1', 'DUM2'))
def test_properties(ask2, bid2, order_book): # Test for properties order_book.insert_ask(ask2) order_book.insert_bid(bid2) assert order_book.get_bid_ask_spread('MB', 'BTC') == Price( -25, 1000, 'MB', 'BTC')
def price(self) -> Price: """ Return a Price object of this asset pair, which expresses the second asset into the first asset. """ return Price(self.second.amount, self.first.amount, self.second.asset_id, self.first.asset_id)
def price_level(): return PriceLevel(Price(100, 1, 'MB', 'BTC'))
def test_properties(self): # Test for properties self.order_book.insert_ask(self.ask2) self.order_book.insert_bid(self.bid2) self.assertEqual(Price(-25, 1000, 'MB', 'BTC'), self.order_book.get_bid_ask_spread('MB', 'BTC'))