/test/integration/test_bittrex_order_book_tracker.py
https://github.com/CoinAlpha/hummingbot · Python · 105 lines · 79 code · 17 blank · 9 comment · 9 complexity · 3285d58a3cbd6a2104d1fe8c18ffe6a6 MD5 · raw file
- #!/usr/bin/env python
- import math
- import time
- from os.path import join, realpath
- import sys
- from hummingbot.core.event.event_logger import EventLogger
- from hummingbot.core.event.events import OrderBookEvent, OrderBookTradeEvent, TradeType
- import asyncio
- import logging
- from typing import Dict, Optional, List
- import unittest
- from hummingbot.connector.exchange.bittrex.bittrex_order_book_tracker import BittrexOrderBookTracker
- from hummingbot.core.data_type.order_book import OrderBook
- from hummingbot.core.utils.async_utils import safe_ensure_future
- sys.path.insert(0, realpath(join(__file__, "../../../")))
- class BittrexOrderBookTrackerUnitTest(unittest.TestCase):
- order_book_tracker: Optional[BittrexOrderBookTracker] = None
- events: List[OrderBookEvent] = [
- OrderBookEvent.TradeEvent
- ]
- # TODO: Update trading pair format to V3 WebSocket API
- trading_pairs: List[str] = [ # Trading Pair in v1.1 format(Quote-Base)
- "LTC-BTC",
- "LTC-ETH"
- ]
- @classmethod
- def setUpClass(cls):
- cls.ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop()
- cls.order_book_tracker: BittrexOrderBookTracker = BittrexOrderBookTracker(trading_pairs=cls.trading_pairs)
- cls.order_book_tracker_task: asyncio.Task = safe_ensure_future(cls.order_book_tracker.start())
- cls.ev_loop.run_until_complete(cls.wait_til_tracker_ready())
- @classmethod
- async def wait_til_tracker_ready(cls):
- while True:
- if len(cls.order_book_tracker.order_books) > 0:
- print("Initialized real-time order books.")
- return
- await asyncio.sleep(1)
- async def run_parallel_async(self, *tasks, timeout=None):
- future: asyncio.Future = asyncio.ensure_future(asyncio.gather(*tasks))
- timer = 0
- while not future.done():
- if timeout and timer > timeout:
- raise Exception("Timeout running parallel async tasks in tests")
- timer += 1
- now = time.time()
- _next_iteration = now // 1.0 + 1 # noqa: F841
- await asyncio.sleep(1.0)
- return future.result()
- def run_parallel(self, *tasks):
- return self.ev_loop.run_until_complete(self.run_parallel_async(*tasks))
- def setUp(self):
- self.event_logger = EventLogger()
- for event_tag in self.events:
- for trading_pair, order_book in self.order_book_tracker.order_books.items():
- order_book.add_listener(event_tag, self.event_logger)
- def test_order_book_trade_event_emission(self):
- """
- Tests if the order book tracker is able to retrieve order book trade message from exchange and emit order book
- trade events after correctly parsing the trade messages
- """
- self.run_parallel(self.event_logger.wait_for(OrderBookTradeEvent))
- for ob_trade_event in self.event_logger.event_log:
- self.assertTrue(type(ob_trade_event) == OrderBookTradeEvent)
- self.assertTrue(ob_trade_event.trading_pair in self.trading_pairs)
- self.assertTrue(type(ob_trade_event.timestamp) in [float, int])
- self.assertTrue(type(ob_trade_event.amount) == float)
- self.assertTrue(type(ob_trade_event.price) == float)
- self.assertTrue(type(ob_trade_event.type) == TradeType)
- # Bittrex datetime is in epoch milliseconds
- self.assertTrue(math.ceil(math.log10(ob_trade_event.timestamp)) == 13)
- self.assertTrue(ob_trade_event.amount > 0)
- self.assertTrue(ob_trade_event.price > 0)
- def test_tracker_integrity(self):
- # Wait 5 seconds to process some diffs.
- self.ev_loop.run_until_complete(asyncio.sleep(5.0))
- order_books: Dict[str, OrderBook] = self.order_book_tracker.order_books
- ltcbtc_book: OrderBook = order_books["LTC-BTC"]
- # print(ltcbtc_book)
- self.assertGreaterEqual(ltcbtc_book.get_price_for_volume(True, 10).result_price,
- ltcbtc_book.get_price(True))
- self.assertLessEqual(ltcbtc_book.get_price_for_volume(False, 10).result_price,
- ltcbtc_book.get_price(False))
- def main():
- logging.basicConfig(level=logging.INFO)
- unittest.main()
- if __name__ == "__main__":
- main()