def test_equal_messages_have_equal_hash(self): content = self._snapshot_example() message = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.SNAPSHOT, content=content, timestamp=10000000) equal_message = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.SNAPSHOT, content=content, timestamp=10000000) self.assertEqual(hash(message), hash(equal_message))
def trade_message_from_exchange(cls, msg: Dict[str, Any], timestamp: Optional[float] = None, metadata: Optional[Dict] = None): """ Convert a trade data into standard OrderBookMessage format :param record: a trade data from the database :return: AltmarketsOrderBookMessage """ if metadata: msg.update(metadata) msg.update({ "trade_id": str(msg.get("tid")), "trade_type": 1.0 if msg.get("taker_type") == "buy" else 2.0 if msg.get("taker_type") == "sell" else None, "price": msg.get("price"), "amount": msg.get("amount"), }) return AltmarketsOrderBookMessage( message_type=OrderBookMessageType.TRADE, content=msg, timestamp=timestamp)
def diff_message_from_db(cls, record: RowProxy, metadata: Optional[Dict] = None): """ *used for backtesting Convert a row of diff data into standard OrderBookMessage format :param record: a row of diff data from the database :return: AltmarketsOrderBookMessage """ return AltmarketsOrderBookMessage( message_type=OrderBookMessageType.DIFF, content=record.json, timestamp=record.timestamp)
def test_equality_based_on_type_and_timestamp(self): message = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.SNAPSHOT, content={}, timestamp=10000000) equal_message = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.SNAPSHOT, content={}, timestamp=10000000) message_with_different_type = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.DIFF, content={}, timestamp=10000000) message_with_different_timestamp = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.SNAPSHOT, content={}, timestamp=90000000) self.assertEqual(message, message) self.assertEqual(message, equal_message) self.assertNotEqual(message, message_with_different_type) self.assertNotEqual(message, message_with_different_timestamp) self.assertTrue(message < message_with_different_type) self.assertTrue(message < message_with_different_timestamp)
def diff_message_from_exchange(cls, msg: Dict[str, any], timestamp: Optional[float] = None, metadata: Optional[Dict] = None): """ Convert json diff data into standard OrderBookMessage format :param msg: json diff data from live web socket stream :param timestamp: timestamp attached to incoming data :return: AltmarketsOrderBookMessage """ if metadata: msg.update(metadata) return AltmarketsOrderBookMessage( message_type=OrderBookMessageType.DIFF, content=msg, timestamp=timestamp)
def test_instance_creation(self): content = self._snapshot_example() message = AltmarketsOrderBookMessage( message_type=OrderBookMessageType.SNAPSHOT, content=content, timestamp=content["timestamp"]) bids = message.bids self.assertEqual(2, len(bids)) self.assertEqual(0.000767, bids[0].price) self.assertEqual(4800.00, bids[0].amount) self.assertEqual(1542337219120 * 1e3, bids[0].update_id) asks = message.asks self.assertEqual(2, len(asks)) self.assertEqual(0.007, asks[0].price) self.assertEqual(100, asks[0].amount) self.assertEqual(1542337219120 * 1e3, asks[0].update_id) self.assertEqual(message.trading_pair, "ETH-USDT")
def test_init_error(self): with self.assertRaises(ValueError) as context: _ = AltmarketsOrderBookMessage(OrderBookMessageType.SNAPSHOT, {}) self.assertEqual( 'timestamp must not be None when initializing snapshot messages.', str(context.exception))