Example #1
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))

        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
Example #2
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))

        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
Example #3
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)
Example #4
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)
Example #5
0
    def addBar(self, instrument, bar, frequency):
        instrument = normalize_instrument(instrument)
        instrumentId = self.__getOrCreateInstrument(instrument)
        timeStamp = dt.datetime_to_timestamp(bar.getDateTime())

        try:
            sql = "insert into bar (instrument_id, frequency, timestamp, open, high, low, close, volume, adj_close) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
            params = [
                instrumentId, frequency, timeStamp,
                bar.getOpen(),
                bar.getHigh(),
                bar.getLow(),
                bar.getClose(),
                bar.getVolume(),
                bar.getAdjClose()
            ]
            self.__connection.execute(sql, params)
        except sqlite3.IntegrityError:
            sql = "update bar set open = ?, high = ?, low = ?, close = ?, volume = ?, adj_close = ?" \
                " where instrument_id = ? and frequency = ? and timestamp = ?"
            params = [
                bar.getOpen(),
                bar.getHigh(),
                bar.getLow(),
                bar.getClose(),
                bar.getVolume(),
                bar.getAdjClose(), instrumentId, frequency, timeStamp
            ]
            self.__connection.execute(sql, params)
Example #6
0
 def onNewValue(self, dateTime, value):
     technical.EventWindow.onNewValue(self, dateTime, value)
     if value is not None:
         timestamp = dt.datetime_to_timestamp(dateTime)
         if len(self.__timestamps):
             assert (timestamp > self.__timestamps[-1])
         self.__timestamps.append(timestamp)
Example #7
0
 def onNewValue(self, dateTime, value):
     technical.EventWindow.onNewValue(self, dateTime, value)
     if value is not None:
         timestamp = dt.datetime_to_timestamp(dateTime)
         if len(self.__timestamps):
             assert(timestamp > self.__timestamps[-1])
         self.__timestamps.append(timestamp)
Example #8
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)
Example #9
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)
Example #10
0
    def addBar(self, instrument, bar, frequency):
        instrument = normalize_instrument(instrument)
        instrumentId = self.__getOrCreateInstrument(instrument)
        timeStamp = dt.datetime_to_timestamp(bar.getDateTime())

        try:
            sql = "insert into bar (instrument_id, frequency, timestamp, open, high, low, close, volume, adj_close) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
            params = [instrumentId, frequency, timeStamp, bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume(), bar.getAdjClose()]
            self.__connection.execute(sql, params)
        except sqlite3.IntegrityError:
            sql = "update bar set open = ?, high = ?, low = ?, close = ?, volume = ?, adj_close = ?" \
                " where instrument_id = ? and frequency = ? and timestamp = ?"
            params = [bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume(), bar.getAdjClose(), instrumentId, frequency, timeStamp]
            self.__connection.execute(sql, params)
Example #11
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)
Example #12
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)
Example #13
0
 def getValueAt(self, dateTime):
     return self.__getValueAtImpl(dt.datetime_to_timestamp(dateTime))
Example #14
0
 def getValueAt(self, dateTime):
     return self.__getValueAtImpl(dt.datetime_to_timestamp(dateTime))