def test_NoDataOnDate(calendar): cal = calendar.schedule.loc[TEST_CALENDAR_START:TEST_CALENDAR_STOP] market_opens = cal.market_open market_closes = cal.market_close market_open_values = market_opens.values.astype('datetime64[m]').astype( np.int64) market_closes_values = market_closes.values.astype('datetime64[m]').astype( np.int64) # 午夜时分 dt_1 = cal.index[0] with pytest.raises(ValueError): find_position_of_minute( market_open_values, market_closes_values, dt_1.value / NANOS_IN_MINUTE, MINUTES_PER_DAY, False, ) # `15:00` 为最后交易分钟 dt_2 = cal.index[-1].replace(hour=7, minute=1) with pytest.raises(ValueError): find_position_of_minute( market_open_values, market_closes_values, dt_2.value / NANOS_IN_MINUTE, MINUTES_PER_DAY, False, )
def _find_position_of_minute(self, minute_dt, adjust_half_day_minutes): """ Internal method that returns the position of the given minute in the list of every trading minute since market open of the first trading day. Adjusts non market minutes to the last close. ex. this method would return 1 for 2002-01-02 9:32 AM Eastern, if 2002-01-02 is the first trading day of the dataset. Parameters ---------- minute_dt: pd.Timestamp The minute whose position should be calculated. adjust_half_day_minutes: boolean Whether or not we want to adjust minutes to early close on half days. Returns ------- int: The position of the given minute in the list of all trading minutes since market open on the first trading day. """ return find_position_of_minute(self._market_open_values, self._market_close_values, minute_dt.value / NANOS_IN_MINUTE, US_EQUITIES_MINUTES_PER_DAY, adjust_half_day_minutes)
def _find_position_of_minute(self, minute_dt): """ Internal method that returns the position of the given minute in the list of every trading minute since market open of the first trading day. Adjusts non market minutes to the last close. ex. this method would return 1 for 2002-01-02 9:32 AM Eastern, if 2002-01-02 is the first trading day of the dataset. Parameters ---------- minute_dt: pd.Timestamp The minute whose position should be calculated. Returns ------- int: The position of the given minute in the list of all trading minutes since market open on the first trading day. """ return find_position_of_minute( self._market_open_values, self._market_close_values, minute_dt.value / NANOS_IN_MINUTE, self._minutes_per_day, )
def _find_position_of_minute(self, minute_dt): """ Internal method that returns the position of the given minute in the list of every trading minute since market open of the first trading day. Adjusts non market minutes to the last close. ex. this method would return 1 for 2002-01-02 9:32 AM Eastern, if 2002-01-02 is the first trading day of the dataset. Parameters ---------- minute_dt: pd.Timestamp The minute whose position should be calculated. Returns ------- int: The position of the given minute in the list of all trading minutes since market open on the first trading day. """ return find_position_of_minute( self._market_open_values, self._market_close_values, minute_dt.value / NANOS_IN_MINUTE, self._minutes_per_day, False, )
def test_convert_minute(calendar): cal = calendar.schedule.loc[TEST_CALENDAR_START:TEST_CALENDAR_STOP] market_opens = cal.market_open market_closes = cal.market_close market_open_values = market_opens.values.astype('datetime64[m]').astype( np.int64) market_closes_values = market_closes.values.astype('datetime64[m]').astype( np.int64) # 测试期间的所有分钟 all_minutes = calendar.minutes_window(market_opens.iloc[0], MINUTES_PER_DAY * len(cal)) # 首先查找分钟对应位置,然后根据位置找回分钟epoch,转换后测试是否一致 for i in range(len(all_minutes)): minute_dt = all_minutes[i] pos = find_position_of_minute( market_open_values, market_closes_values, minute_dt.value / NANOS_IN_MINUTE, MINUTES_PER_DAY, False, ) minute_epoch = minute_value(market_open_values, pos, MINUTES_PER_DAY) finded = pd.Timestamp(minute_epoch, tz='UTC', unit="m") assert minute_dt == finded
def test_find_position_of_minute(calendar): cal = calendar.schedule.loc[TEST_CALENDAR_START:TEST_CALENDAR_STOP] market_opens = cal.market_open market_closes = cal.market_close market_open_values = market_opens.values.astype('datetime64[m]').astype( np.int64) market_closes_values = market_closes.values.astype('datetime64[m]').astype( np.int64) # 测试期间的所有分钟 all_minutes = calendar.minutes_window(market_opens.iloc[0], MINUTES_PER_DAY * len(cal)) for i in range(len(all_minutes)): minute_dt = all_minutes[i] pos = find_position_of_minute( market_open_values, market_closes_values, minute_dt.value / NANOS_IN_MINUTE, MINUTES_PER_DAY, False, ) assert i == pos
def _find_position_of_minute(self, minute_dt, adjust_half_day_minutes): """ Internal method that returns the position of the given minute in the list of every trading minute since market open of the first trading day. Adjusts non market minutes to the last close. ex. this method would return 1 for 2002-01-02 9:32 AM Eastern, if 2002-01-02 is the first trading day of the dataset. Parameters ---------- minute_dt: pd.Timestamp The minute whose position should be calculated. adjust_half_day_minutes: boolean Whether or not we want to adjust minutes to early close on half days. Returns ------- int: The position of the given minute in the list of all trading minutes since market open on the first trading day. """ return find_position_of_minute( self._market_open_values, self._market_close_values, minute_dt.value / NANOS_IN_MINUTE, US_EQUITIES_MINUTES_PER_DAY, adjust_half_day_minutes )