def test_next_event_indexer(self): events = self.events event_sids = events['sid'].values event_dates = events['event_date'] event_timestamps = events['timestamp'] all_dates = pd.date_range('2014', '2014-01-31', tz='UTC') all_sids = np.unique(event_sids) domain = EquitySessionDomain( all_dates, 'US', time(8, 45, tzinfo=pytz.timezone('US/Eastern')), ) indexer = next_event_indexer( all_dates, domain.data_query_cutoff_for_sessions(all_dates), all_sids, event_dates, event_timestamps, event_sids, ) # Compute expected results without knowledge of null events. for i, sid in enumerate(all_sids): self.check_next_event_indexer( events, all_dates, sid, indexer[:, i], )
def test_next_event_indexer(self): events = self.events event_sids = events['sid'].values event_dates = events['event_date'].values event_timestamps = events['timestamp'].values all_dates = pd.date_range('2014', '2014-01-31', tz='UTC') all_sids = np.unique(event_sids) domain = EquitySessionDomain( all_dates, 'US', time(8, 45, tzinfo=pytz.timezone('US/Eastern')), ) indexer = next_event_indexer( all_dates, domain.data_query_cutoff_for_sessions(all_dates), all_sids, event_dates, event_timestamps, event_sids, ) # Compute expected results without knowledge of null events. for i, sid in enumerate(all_sids): self.check_next_event_indexer( events, all_dates, sid, indexer[:, i], )
def test_previous_event_indexer(self): events = self.events event_sids = events['sid'].values event_dates = events['event_date'].values event_timestamps = events['timestamp'].values all_dates = pd.date_range('2014', '2014-01-31', tz='UTC') all_sids = np.unique(event_sids) domain = EquitySessionDomain( all_dates, 'CN', time(8, 45, tzinfo=pytz.timezone('Asia/Shanghai')), ) indexer = previous_event_indexer( # -> ndarray[datetime64[ns], ndim=1] domain.data_query_cutoff_for_sessions(all_dates).values, all_sids, event_dates, event_timestamps, event_sids, ) # Compute expected results without knowledge of null events. for i, sid in enumerate(all_sids): self.check_previous_event_indexer( events, all_dates, sid, indexer[:, i], )
def test_next_event_indexer(self): events = self.events event_sids = events["sid"].to_numpy() event_dates = events["event_date"].to_numpy() event_timestamps = events["timestamp"].to_numpy() all_dates = pd.date_range("2014", "2014-01-31", tz="UTC") all_sids = np.unique(event_sids) domain = EquitySessionDomain( all_dates, "US", time(8, 45, tzinfo=pytz.timezone("US/Eastern")), ) indexer = next_event_indexer( all_dates, domain.data_query_cutoff_for_sessions(all_dates), all_sids, event_dates, event_timestamps, event_sids, ) # Compute expected results without knowledge of null events. for i, sid in enumerate(all_sids): self.check_next_event_indexer( events, all_dates, sid, indexer[:, i], )
def test_roll_forward(self): # January 2017 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 6 7 # the first three days of the year are holidays on the Tokyo exchange, # so the first trading day should be the fourth assert JP_EQUITIES.roll_forward("2017-01-01") == pd.Timestamp( "2017-01-04", tz="UTC") # in US exchanges, the first trading day after 1/1 is the 3rd assert US_EQUITIES.roll_forward("2017-01-01") == pd.Timestamp( "2017-01-03", tz="UTC") # passing a valid trading day to roll_forward should return that day assert JP_EQUITIES.roll_forward("2017-01-04") == pd.Timestamp( "2017-01-04", tz="UTC") # passing a date before the first session should return the # first session before_first_session = JP_EQUITIES.calendar.first_session - pd.Timedelta( days=20) assert (JP_EQUITIES.roll_forward(before_first_session) == JP_EQUITIES.calendar.first_session) # requesting a session beyond the last session raises an ValueError after_last_session = JP_EQUITIES.calendar.last_session + pd.Timedelta( days=20) expected_msg = ( f"Date {after_last_session.date()} was past the last session " "for domain EquityCalendarDomain('JP', 'XTKS'). The last session for " f"this domain is {JP_EQUITIES.calendar.last_session.date()}.") with pytest.raises(ValueError, match=re.escape(expected_msg)): JP_EQUITIES.roll_forward(after_last_session) # test that a roll_forward works with an EquitySessionDomain, # not just calendar domains sessions = pd.DatetimeIndex( ["2000-01-01", "2000-02-01", "2000-04-01", "2000-06-01"], tz="UTC") session_domain = EquitySessionDomain(sessions, CountryCode.UNITED_STATES) assert session_domain.roll_forward("2000-02-01") == pd.Timestamp( "2000-02-01", tz="UTC") assert session_domain.roll_forward("2000-02-02") == pd.Timestamp( "2000-04-01", tz="UTC")
def test_equity_session_domain(self, parameters): time, date_offset, expected_timedelta = parameters naive_sessions = pd.date_range('2000-01-01', '2000-06-01') utc_sessions = naive_sessions.tz_localize('UTC') domain = EquitySessionDomain( utc_sessions, CountryCode.UNITED_STATES, data_query_time=time, data_query_date_offset=date_offset, ) # Adding and localizing the naive_sessions here because pandas 18 # crashes when adding a tz-aware DatetimeIndex and a # TimedeltaIndex. :sadpanda:. expected = (naive_sessions + expected_timedelta).tz_localize('utc') actual = domain.data_query_cutoff_for_sessions(utc_sessions) assert_equal(expected, actual)
def init_class_fixtures(cls): super(TestDownsampledRowwiseOperation, cls).init_class_fixtures() cls.pipeline_engine = SimplePipelineEngine( get_loader=lambda c: ExplodingObject(), asset_finder=cls.asset_finder, default_domain=EquitySessionDomain( cls.dates, country_code=cls.ASSET_FINDER_COUNTRY_CODE, ), )
def create_domain(sessions, data_query_time=time(0, 0, tzinfo=pytz.utc), data_query_date_offset=0): if sessions.tz is None: sessions = sessions.tz_localize('UTC') return EquitySessionDomain( sessions, country_code='CN', data_query_time=data_query_time, data_query_date_offset=data_query_date_offset, )
def test_roll_forward(self): # January 2017 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 6 7 # the first three days of the year are holidays on the Tokyo exchange, # so the first trading day should be the fourth self.assertEqual( JP_EQUITIES.roll_forward('2017-01-01'), pd.Timestamp('2017-01-04', tz='UTC'), ) # in US exchanges, the first trading day after 1/1 is the 3rd self.assertEqual( US_EQUITIES.roll_forward('2017-01-01'), pd.Timestamp('2017-01-03', tz='UTC'), ) # passing a valid trading day to roll_forward should return that day self.assertEqual( JP_EQUITIES.roll_forward('2017-01-04'), pd.Timestamp('2017-01-04', tz='UTC'), ) # passing a date before the first session should return the # first session before_first_session = \ JP_EQUITIES.calendar.first_session - pd.Timedelta(days=20) self.assertEqual( JP_EQUITIES.roll_forward(before_first_session), JP_EQUITIES.calendar.first_session ) # requesting a session beyond the last session raises an ValueError after_last_session = \ JP_EQUITIES.calendar.last_session + pd.Timedelta(days=20) with self.assertRaises(ValueError) as ve: JP_EQUITIES.roll_forward(after_last_session) self.assertEqual( str(ve.exception), "Date {} was past the last session for domain " "EquityCalendarDomain('JP', 'XTKS'). The last session for " "this domain is {}.".format( after_last_session.date(), JP_EQUITIES.calendar.last_session.date(), ) ) # test that a roll_forward works with an EquitySessionDomain, # not just calendar domains sessions = pd.DatetimeIndex( ['2000-01-01', '2000-02-01', '2000-04-01', '2000-06-01'], tz='UTC' ) session_domain = EquitySessionDomain( sessions, CountryCode.UNITED_STATES ) self.assertEqual( session_domain.roll_forward('2000-02-01'), pd.Timestamp('2000-02-01', tz='UTC'), ) self.assertEqual( session_domain.roll_forward('2000-02-02'), pd.Timestamp('2000-04-01', tz='UTC'), )
def create_simple_domain(start, end, country_code): """Create a new pipeline domain with a simple date_range index. """ return EquitySessionDomain(pd.date_range(start, end), country_code)