示例#1
0
 def get_market_open_close_time(self, aDatetime):
     aDate = convert_datetime_to_date(aDatetime)
     if aDate in self._marketOpenCloseTimeRecord:
         opn, close = self._marketOpenCloseTimeRecord[aDate]
         return opn, close
     if self.isTradingDay(aDatetime):
         sch = self._marketCalendar.schedule(start_date=aDatetime,
                                             end_date=aDatetime)
         opn, close = sch.iloc[0]['market_open'], sch.iloc[0][
             'market_close']
         self._marketOpenCloseTimeRecord[aDate] = (opn, close)
         return opn, close
     else:
         return None, None
示例#2
0
 def isEarlyClose(self, aDatetime):
     aDate = convert_datetime_to_date(aDatetime)
     if not self.isTradingDay(
             aDate
     ):  # If it is NOT a trading day, definitely it is not an early close
         return False
     pdSeries = self.get_early_closes(
         aDate, aDate)['market_close']  # Only returns early closes
     aTimestamp = pd.Timestamp(aDate)
     if aTimestamp not in pdSeries.index:
         return False
     # Returned market_close_time does not have timezone. So, localize to UTC and convert to Eastern
     if not pdSeries[aTimestamp].tzinfo:
         return pdSeries[aTimestamp].tz_localize('UTC').tz_convert(
             'US/Eastern').hour == 13  # Early close at 1:00PM
     else:
         return pdSeries[aTimestamp].tz_convert(
             'US/Eastern').hour == 13  # Early close at 1:00PM
示例#3
0
 def isTradingDay(self, aDatetime):
     """
     Check if a datetime is a trading day based on marketName
     output:
         True: It is a trading day.
     """
     # print(__name__ + '::isTradingDay: %s' % (aDatetime,))
     aDate = convert_datetime_to_date(aDatetime)
     if aDate in self._isTradingDaySet:
         return True
     if aDate in self._notTradingDaySet:
         return False
     if np.is_busday(aDate):
         ans = not (np.datetime64(aDate)
                    in self._marketCalendar.holidays().holidays)
     else:
         ans = False
     if ans:
         self._isTradingDaySet.add(aDate)
     else:
         self._notTradingDaySet.add(aDate)
     return ans
示例#4
0
 def nth_trading_day_of_week(self, aDatetime):
     """
     1st trading day of week is 0
     last trading day of week is -1
     @param aDatetime: dt.date
     @result: list [nth trading day in a week, reverse location in a week]
     """
     aDay = convert_datetime_to_date(aDatetime)
     if aDay in self._nthTradingDayOfWeekRecord:
         return self._nthTradingDayOfWeekRecord[aDay]
     tmp = aDay.weekday()
     weekStartDate = aDay - dt.timedelta(days=tmp)
     weekEndDate = weekStartDate + dt.timedelta(days=4)
     ls_validDays = self._get_validDays(weekStartDate, weekEndDate)
     t = pd.Timestamp(aDay).tz_localize(pytz.utc)
     if t in ls_validDays:
         x = ls_validDays.get_loc(t)
         ans = [x, x - len(ls_validDays)]
         self._nthTradingDayOfWeekRecord[aDay] = ans
         return ans
     else:
         self._nthTradingDayOfWeekRecord[aDay] = None
         return None
示例#5
0
    def nth_trading_day_of_month(self, aDatetime):
        """
        1st trading day of month is 0
        last trading day of month is -1
        @param aDatetime: dt.date
        @result: list [nth trading day in a month, reverse location in a month]
        """
        aDay = convert_datetime_to_date(aDatetime)
        if aDay in self._nthTradingDayOfMonthRecord:
            return self._nthTradingDayOfMonthRecord[aDay]
        monthStartDate = aDay.replace(day=1)
        monthEndDate = (aDay +
                        MonthEnd(0)).date()  # change pd.TimeStamp to dt.date
        ls_validDays = self._get_validDays(monthStartDate, monthEndDate)

        t = pd.Timestamp(aDay).tz_localize(pytz.utc)
        if t in ls_validDays:
            x = ls_validDays.get_loc(t)
            ans = [x, x - len(ls_validDays)]
            self._nthTradingDayOfMonthRecord[aDay] = ans
            return ans
        else:
            self._nthTradingDayOfMonthRecord[aDay] = None
            return None