def snap(self, market_ids: list = None) -> list: market_books = [] for cache in list(self._caches.values()): if market_ids and cache.market_id not in market_ids: continue # if market has closed send regardless if cache.market_definition["status"] != "CLOSED": if self._listener.inplay: if not cache.market_definition["inPlay"]: continue elif self._listener.seconds_to_start: _now = datetime.datetime.utcfromtimestamp(cache.publish_time / 1e3) _market_time = BaseResource.strip_datetime( cache.market_definition["marketTime"] ) seconds_to_start = (_market_time - _now).total_seconds() if seconds_to_start > self._listener.seconds_to_start: continue if self._listener.inplay is False: if cache.market_definition["inPlay"]: continue market_books.append( cache.create_resource(self.unique_id, None, self._lightweight) ) return market_books
def test_init(self): assert self.unmatched_order.bet_id == 1 assert self.unmatched_order.price == 2 assert self.unmatched_order.size == 3 assert self.unmatched_order.side == "L" assert self.unmatched_order.status == "E" assert self.unmatched_order.persistence_type == "L" assert self.unmatched_order.order_type == "L" assert self.unmatched_order.placed_date == BaseResource.strip_datetime(8) assert self.unmatched_order.size_matched == 9 assert self.unmatched_order.size_remaining == 10 assert self.unmatched_order.size_lapsed == 11 assert self.unmatched_order.size_cancelled == 12 assert self.unmatched_order.size_voided == 13 assert self.unmatched_order.reference_order == 14 assert self.unmatched_order.reference_strategy == 15 assert self.unmatched_order.lapsed_date == BaseResource.strip_datetime(16) assert self.unmatched_order.lapse_status_reason_code == 17
def test_strip_datetime(self): base_resource = BaseResource() for string in [ "2100-06-01T10:10:00.000Z", "2100-06-01T10:10:00.00Z", "2100-06-01T10:10:00.0Z", ]: stripped = base_resource.strip_datetime(string) assert type(stripped) == datetime.datetime stripped = base_resource.strip_datetime(None) assert not stripped stripped = base_resource.strip_datetime("45") assert not stripped integer = 1465631675000 stripped = base_resource.strip_datetime(integer) assert type(stripped) == datetime.datetime stripped = base_resource.strip_datetime(None) assert not stripped stripped = base_resource.strip_datetime("45") assert not stripped
def snap(self, market_ids: list = None) -> list: market_books = [] for cache in list(self._caches.values()): if market_ids and cache.market_id not in market_ids: continue # if market is not open (closed/suspended) send regardless if cache._definition_status == "OPEN": if self._listener.inplay: if not cache._definition_in_play: continue elif self._listener.seconds_to_start: _now = datetime.datetime.utcfromtimestamp( cache.publish_time / 1e3) _market_time = BaseResource.strip_datetime( cache.market_definition["marketTime"]) seconds_to_start = (_market_time - _now).total_seconds() if seconds_to_start > self._listener.seconds_to_start: continue if self._listener.inplay is False: if cache._definition_in_play: continue market_books.append( cache.create_resource(self.unique_id, snap=True)) return market_books
def test_strip_datetime(self): base_resource = BaseResource() for string in ['2100-06-01T10:10:00.000Z', '2100-06-01T10:10:00.00Z', '2100-06-01T10:10:00.0Z']: stripped = base_resource.strip_datetime(string) assert type(stripped) == datetime.datetime stripped = base_resource.strip_datetime(None) assert not stripped stripped = base_resource.strip_datetime('45') assert not stripped integer = 1465631675000 stripped = base_resource.strip_datetime(integer) assert type(stripped) == datetime.datetime stripped = base_resource.strip_datetime(None) assert not stripped stripped = base_resource.strip_datetime('45') assert not stripped stripped = base_resource.strip_datetime(-1230000000345446) assert not stripped
def test_strip_datetime(self): base_resource = BaseResource() for string in ['2100-06-01T10:10:00.000Z', '2100-06-01T10:10:00.00Z', '2100-06-01T10:10:00.0Z']: stripped = base_resource.strip_datetime(string) assert type(stripped) == datetime.datetime stripped = base_resource.strip_datetime(None) assert not stripped stripped = base_resource.strip_datetime('45') assert not stripped integer = 1465631675000 stripped = base_resource.strip_datetime(integer) assert type(stripped) == datetime.datetime stripped = base_resource.strip_datetime(None) assert not stripped stripped = base_resource.strip_datetime('45') assert not stripped
def test_matched_date_string(self): now = BaseResource.strip_datetime(4) assert self.unmatched_order.matched_date_string == now.strftime( "%Y-%m-%dT%H:%M:%S.%fZ" )
def test_placed_date_string(self): now = BaseResource.strip_datetime(8) assert self.unmatched_order.placed_date_string == now.strftime( '%Y-%m-%dT%H:%M:%S.%fZ')
def _process(self, data: list, publish_time: int) -> bool: active = False for market_book in data: if "id" not in market_book: continue market_id = market_book["id"] full_image = market_book.get("img", False) market_book_cache = self._caches.get(market_id) if (full_image or market_book_cache is None): # historic data does not contain img if "marketDefinition" not in market_book: logger.warning( "[%s: %s]: Missing marketDefinition on market %s resulting " "in potential missing data in the MarketBook (make sure " "EX_MARKET_DEF is requested)" % (self, self.unique_id, market_id)) market_book_cache = MarketBookCache( market_id, publish_time, self._lightweight, self._calculate_market_tv, self._cumulative_runner_tv, ) self._caches[market_id] = market_book_cache logger.info( "[%s: %s]: %s added, %s markets in cache" % (self, self.unique_id, market_id, len(self._caches))) # listener_kwargs filtering active = True if "marketDefinition" in market_book: _definition_status = market_book["marketDefinition"].get( "status") _definition_in_play = market_book["marketDefinition"].get( "inPlay") _definition_market_time = market_book["marketDefinition"].get( "marketTime") else: _definition_status = market_book_cache._definition_status _definition_in_play = market_book_cache._definition_in_play _definition_market_time = market_book_cache.market_definition[ "marketTime"] # if market is not open (closed/suspended) process regardless if _definition_status == "OPEN": if self._listener.inplay: if not _definition_in_play: active = False elif self._listener.seconds_to_start: _now = datetime.datetime.utcfromtimestamp(publish_time / 1e3) _market_time = BaseResource.strip_datetime( _definition_market_time) seconds_to_start = (_market_time - _now).total_seconds() if seconds_to_start > self._listener.seconds_to_start: active = False if self._listener.inplay is False: if _definition_in_play: active = False # check if refresh required if active and not market_book_cache.active: market_book_cache.refresh_cache() market_book_cache.update_cache(market_book, publish_time, active=active) self._updates_processed += 1 return active