Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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))

        ret = []
        sql += " ORDER by bar.timestamp ASC"

        cursor = self.__connection.cursor()
        cursor.execute(sql, args)

        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
Пример #4
0
 def __init__(self, barDict, frequency):
     self.__DateTimeLocal = liveUtils.timestamp_to_DateTimeLocal(
         barDict["Timestamp"])
     super(liveBar,
           self).__init__(dt.timestamp_to_datetime(barDict["Timestamp"]),
                          barDict["Open"], barDict["High"], barDict["Low"],
                          barDict["Close"], barDict["Volume"], None,
                          frequency)
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
 def getDateTime(self):
     return dt.timestamp_to_datetime(int(self.__obj['finished-at']) / 1000)