def setUp(self): # Object creation self.transaction_id = TransactionId(b'a' * 32) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0)) self.proposed_trade = Trade.propose( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), Timestamp(0)) self.tick = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(5, 'BTC'), AssetAmount(5, 'MC')), Timeout(0), Timestamp(00), True) self.tick2 = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(500, 'BTC'), AssetAmount(5, 'MC')), Timeout(0), Timestamp(0), True) self.order_timestamp = Timestamp.now() self.order = Order( OrderId(TraderId(b'0' * 20), OrderNumber(3)), AssetPair(AssetAmount(50, 'BTC'), AssetAmount(40, 'MC')), Timeout(5000), self.order_timestamp, False) self.order.set_verified() self.order2 = Order( OrderId(TraderId(b'0' * 20), OrderNumber(4)), AssetPair(AssetAmount(50, 'BTC'), AssetAmount(10, 'MC')), Timeout(5), Timestamp(int(time.time() * 1000) - 1000 * 1000), True) self.order2.set_verified()
def setUp(self): # Object creation self.tick = Tick(OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), True) self.tick2 = Tick(OrderId(TraderId(b'1' * 20), OrderNumber(2)), AssetPair(AssetAmount(120, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), True) self.side = Side()
def test_add_remove_tick(db, order1, order2): """ Test addition, retrieval and deletion of ticks in the database """ ask = Tick.from_order(order1) db.add_tick(ask) bid = Tick.from_order(order2) db.add_tick(bid) assert len(db.get_ticks()) == 2 db.delete_all_ticks() assert not db.get_ticks()
def test_add_remove_tick(self): """ Test addition, retrieval and deletion of ticks in the database """ ask = Tick.from_order(self.order1) self.database.add_tick(ask) bid = Tick.from_order(self.order2) self.database.add_tick(bid) self.assertEqual(len(self.database.get_ticks()), 2) self.database.delete_all_ticks() self.assertEqual(len(self.database.get_ticks()), 0)
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)
async def tick_entry2(price_level): tick = Tick(OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(63400, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), True) tick_entry = TickEntry(tick, price_level) yield tick_entry await tick_entry.shutdown_task_manager()
def get_ticks(self): """ Get all ticks present in the database. """ return [ Tick.from_database(db_tick) for db_tick in self.execute(u"SELECT * FROM ticks") ]
def test_from_order_bid(self): # Test for from order bid = Tick.from_order(self.order_bid) self.assertIsInstance(bid, Bid) self.assertEqual(self.tick2.price, bid.price) self.assertEqual(self.tick2.assets, bid.assets) self.assertEqual(self.tick2.timestamp, bid.timestamp) self.assertEqual(self.tick2.order_id, bid.order_id) self.assertEqual(self.tick2.traded, 0)
def test_from_order_ask(self): # Test for from order ask = Tick.from_order(self.order_ask) self.assertIsInstance(ask, Ask) self.assertEqual(self.tick2.price, ask.price) self.assertEqual(self.tick2.assets, ask.assets) self.assertEqual(self.tick2.timestamp, ask.timestamp) self.assertEqual(self.tick2.order_id, ask.order_id) self.assertEqual(self.tick2.traded, 0)
def setUp(self): # Object creation self.timestamp_now = Timestamp.now() self.tick = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0), True) self.tick2 = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), False) self.order_ask = Order( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), True) self.order_bid = Order( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), False)
class TickTestSuite(unittest.TestCase): """ This class contains tests for the Tick object. """ def setUp(self): # Object creation self.timestamp_now = Timestamp.now() self.tick = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0), True) self.tick2 = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), False) self.order_ask = Order( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), True) self.order_bid = Order( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0), False) def test_is_ask(self): # Test 'is ask' function self.assertTrue(self.tick.is_ask()) self.assertFalse(self.tick2.is_ask()) def test_to_network(self): # Test for to network self.assertEqual( (TraderId(b'0' * 20), self.tick.timestamp, OrderNumber(1), AssetPair(AssetAmount(30, 'BTC'), AssetAmount( 30, 'MB')), self.tick.timeout, 0), self.tick.to_network()) def test_traded_setter(self): # Test for traded setter self.tick.traded = 10 self.assertEqual(10, self.tick.traded) def test_from_order_ask(self): # Test for from order ask = Tick.from_order(self.order_ask) self.assertIsInstance(ask, Ask) self.assertEqual(self.tick2.price, ask.price) self.assertEqual(self.tick2.assets, ask.assets) self.assertEqual(self.tick2.timestamp, ask.timestamp) self.assertEqual(self.tick2.order_id, ask.order_id) self.assertEqual(self.tick2.traded, 0) def test_from_order_bid(self): # Test for from order bid = Tick.from_order(self.order_bid) self.assertIsInstance(bid, Bid) self.assertEqual(self.tick2.price, bid.price) self.assertEqual(self.tick2.assets, bid.assets) self.assertEqual(self.tick2.timestamp, bid.timestamp) self.assertEqual(self.tick2.order_id, bid.order_id) self.assertEqual(self.tick2.traded, 0) def test_to_dictionary(self): """ Test the to dictionary method of a tick """ self.assertDictEqual( self.tick.to_dictionary(), { "trader_id": '30' * 20, "order_number": 1, "assets": self.tick.assets.to_dictionary(), "timeout": 30, "timestamp": 0.0, "traded": 0, "block_hash": hexlify(b'0' * 32).decode('utf-8') })