def __init__(self, dateTime): super(MonthRange, self).__init__() self.__begin = datetime.datetime(dateTime.year, dateTime.month, 1) # Calculate the ending date. if dateTime.month == 12: self.__end = datetime.datetime(dateTime.year + 1, 1, 1) else: self.__end = datetime.datetime(dateTime.year, dateTime.month + 1, 1) if not dt.datetime_is_naive(dateTime): self.__begin = dt.localize(self.__begin, dateTime.tzinfo) self.__end = dt.localize(self.__end, dateTime.tzinfo)
def __init__(self, dateTime): super(DayRange, self).__init__() self.__begin = datetime.datetime(dateTime.year, dateTime.month, dateTime.day) if not dt.datetime_is_naive(dateTime): self.__begin = dt.localize(self.__begin, dateTime.tzinfo) self.__end = self.__begin + datetime.timedelta(days=1)
def _parseDate(self, dateString): ret = datetime.datetime.strptime(dateString, self.__dateTimeFormat) if self.__dailyBarTime is not None: ret = datetime.datetime.combine(ret, self.__dailyBarTime) # Localize the datetime if a timezone was given. if self.__timezone: ret = dt.localize(ret, self.__timezone) return ret
def __parseDate(self, dateString): ret = parse_date(dateString) # Time on Yahoo! Finance CSV files is empty. If told to set one, do it. if self.__dailyBarTime is not None: ret = datetime.datetime.combine(ret, self.__dailyBarTime) # Localize the datetime if a timezone was given. if self.__timezone: ret = dt.localize(ret, self.__timezone) return ret
def __init__(self, dateTime, frequency): super(IntraDayRange, self).__init__() assert isinstance(frequency, int) assert frequency > 1 assert frequency < bar.Frequency.DAY ts = int(dt.datetime_to_timestamp(dateTime)) slot = int(ts / frequency) slotTs = slot * frequency self.__begin = dt.timestamp_to_datetime( slotTs, not dt.datetime_is_naive(dateTime)) if not dt.datetime_is_naive(dateTime): self.__begin = dt.localize(self.__begin, dateTime.tzinfo) self.__end = self.__begin + datetime.timedelta(seconds=frequency)
def parseRow(self, csvRowDict): dateTime = datetime.datetime.strptime( csvRowDict[self.__dateTimeColumn], self.__dateTimeFormat) # Localize the datetime if a timezone was given. if self.__timezone is not None: if self.__timeDelta is not None: dateTime += self.__timeDelta dateTime = dt.localize(dateTime, self.__timezone) # Convert the values values = {} for key, value in csvRowDict.items(): if key != self.__dateTimeColumn: values[key] = self.__converter(key, value) return (dateTime, values)
def includeBar(self, bar_): ret = super(USEquitiesRTH, self).includeBar(bar_) if ret: # Check day of week barDay = bar_.getDateTime().weekday() if barDay > 4: return False # Check time barTime = dt.localize(bar_.getDateTime(), USEquitiesRTH.timezone).time() if barTime < self.__fromTime: return False if barTime > self.__toTime: return False return ret
def __parseDate(self, dateString): ret = parse_date(dateString) # Localize the datetime if a timezone was given. if self.__timezone: ret = dt.localize(ret, self.__timezone) return ret
def to_market_datetime(dateTime): timezone = pytz.timezone('Asia/Shanghai') return dt.localize(dateTime, timezone)