def testTimeStampConversions(self): dateTime = datetime.datetime(2000, 1, 1) self.assertEqual( dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), False), dateTime) dateTime = dt.as_utc(datetime.datetime(2000, 1, 1, 1, 1)) self.assertEqual( dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), True), dateTime)
def testTimeStampConversionsWithMicroseconds(self): dateTime = datetime.datetime(2000, 1, 1, 1, 1, 1, microsecond=10) self.assertEqual( dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), False), dateTime) dateTime = dt.as_utc( datetime.datetime(2000, 1, 1, 1, 1, 1, microsecond=10)) self.assertEqual( dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), True), dateTime)
def getBars(self, instrument, frequency, timezone=None, fromDateTime=None, toDateTime=None): instrument = normalize_instrument(instrument) sql = "select bar.timestamp, bar.open, bar.high, bar.low, bar.close, bar.volume, bar.adj_close, bar.frequency" \ " from bar join instrument on (bar.instrument_id = instrument.instrument_id)" \ " where instrument.name = ? and bar.frequency = ?" args = [instrument, frequency] if fromDateTime is not None: sql += " and bar.timestamp >= ?" args.append(dt.datetime_to_timestamp(fromDateTime)) if toDateTime is not None: sql += " and bar.timestamp <= ?" args.append(dt.datetime_to_timestamp(toDateTime)) sql += " order by bar.timestamp asc" cursor = self.__connection.cursor() cursor.execute(sql, args) ret = [] for row in cursor: dateTime = dt.timestamp_to_datetime(row[0]) if timezone: dateTime = dt.localize(dateTime, timezone) ret.append( bar.BasicBar(dateTime, row[1], row[2], row[3], row[4], row[5], row[6], row[7])) cursor.close() return ret
def parseBar(self, csvRowDict): unixTime = int(csvRowDict["unixtime"]) price = float(csvRowDict["price"]) amount = float(csvRowDict["amount"]) dateTime = dt.timestamp_to_datetime(unixTime) dateTime = self.__unixTimeFix.fixDateTime(dateTime) # Localize the datetime if a timezone was given. if self.__timezone: dateTime = dt.localize(dateTime, self.__timezone) return TradeBar(dateTime, price, amount)
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)