def test_equal_messages_have_equal_hash(self):
        content = self._snapshot_rest_example()
        message = BitmartOrderBookMessage(
            message_type=OrderBookMessageType.SNAPSHOT,
            content=content,
            timestamp=10000000)
        equal_message = BitmartOrderBookMessage(
            message_type=OrderBookMessageType.SNAPSHOT,
            content=content,
            timestamp=10000000)

        self.assertEqual(hash(message), hash(equal_message))
Exemplo n.º 2
0
    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: BitmartOrderBookMessage
        """

        if metadata:
            msg.update(metadata)

        msg.update({
            "exchange_order_id": str(msg.get("s_t")),
            "trade_type": msg.get("side"),
            "price": msg.get("price"),
            "amount": msg.get("size"),
        })

        return BitmartOrderBookMessage(
            message_type=OrderBookMessageType.TRADE,
            content=msg,
            timestamp=timestamp
        )
    def test_instance_creation(self):
        content = self._example_json()
        message = BitmartOrderBookMessage(
            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(1527777538000, bids[0].update_id)
 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: BitmartOrderBookMessage
     """
     return BitmartOrderBookMessage(message_type=OrderBookMessageType.DIFF,
                                    content=record.json,
                                    timestamp=record.timestamp)
    def test_equality_based_on_type_and_timestamp(self):
        message = BitmartOrderBookMessage(
            message_type=OrderBookMessageType.SNAPSHOT,
            content={},
            timestamp=10000000)
        equal_message = BitmartOrderBookMessage(
            message_type=OrderBookMessageType.SNAPSHOT,
            content={},
            timestamp=10000000)
        message_with_different_type = BitmartOrderBookMessage(
            message_type=OrderBookMessageType.DIFF,
            content={},
            timestamp=10000000)
        message_with_different_timestamp = BitmartOrderBookMessage(
            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)
    def test_instance_creation(self):
        content = self._snapshot_rest_example()
        message = BitmartOrderBookMessage(
            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(1527777538000, 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(1527777538000, asks[0].update_id)

        content = self._snapshot_ws_example()
        message = BitmartOrderBookMessage(
            message_type=OrderBookMessageType.SNAPSHOT,
            content=content,
            timestamp=content["ms_t"])
        bids = message.bids

        self.assertEqual(1, len(bids))
        self.assertEqual(161.94, bids[0].price)
        self.assertEqual(4.552355, bids[0].amount)
        self.assertEqual(1542337219120, bids[0].update_id)

        asks = message.asks
        self.assertEqual(1, len(asks))
        self.assertEqual(161.96, asks[0].price)
        self.assertEqual(7.37567, asks[0].amount)
        self.assertEqual(1542337219120, asks[0].update_id)

        self.assertEqual(message.trading_pair, "ETH_USDT")
    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: BitmartOrderBookMessage
        """

        if metadata:
            msg.update(metadata)

        return BitmartOrderBookMessage(message_type=OrderBookMessageType.DIFF,
                                       content=msg,
                                       timestamp=timestamp)
 def test_init_error(self):
     with self.assertRaises(ValueError) as context:
         _ = BitmartOrderBookMessage(OrderBookMessageType.SNAPSHOT, {})
     self.assertEqual(
         'timestamp must not be None when initializing snapshot messages.',
         str(context.exception))