def wait_for_next_market_opening(self): """ Sleep until the next market opening. Takes into account weekends and bank holidays in UK """ seconds = Utils.get_seconds_to_market_opening(dt.datetime.now()) logging.info("Market is closed! Wait for {0:.2f} hours...".format( seconds / 3600)) time.sleep(seconds)
def test_get_seconds_to_market_opening(): now = datetime.now() seconds = Utils.get_seconds_to_market_opening(now) assert seconds > 0 assert seconds is not None opening = now + timedelta(seconds=seconds) assert 0 <= opening.weekday() < 5 expected = opening.replace(hour=8, minute=0, second=2, microsecond=0) diff = expected - opening assert diff.seconds < 10 # Test function if called after midnight but before market opening mock = datetime.now().replace(hour=3, minute=30, second=0, microsecond=0) seconds = Utils.get_seconds_to_market_opening(mock) assert seconds > 0 assert seconds is not None opening = mock + timedelta(seconds=seconds) # assert opening.day == mock.day assert opening.hour == 8 assert opening.minute == 0
def get_seconds_to_next_spin(self): """ Calculate the amount of seconds to wait for between each strategy spin """ # Run this strategy at market opening return Utils.get_seconds_to_market_opening(datetime.now())