示例#1
0
    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)
示例#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
示例#3
0
 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)
示例#4
0
 def __parseDate(self, dateString):
     # print dateString
     ret = parse_date2(dateString)
     if self.__dailyBarTime is not None:
         ret = datetime.datetime.combine(ret, self.__dailyBarTime)
     if self.__timezone:
         ret = dt.localize(ret, self.__timezone)
     return ret
示例#5
0
 def __parseDate(self, dateString):
     ret = parse_date(dateString)
     # Time on Google 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
示例#6
0
文件: csvfeed.py 项目: dxcv/Skywalker
    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
示例#7
0
文件: barfeed.py 项目: dxcv/Skywalker
    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)
示例#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)
示例#9
0
 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)
示例#10
0
文件: csvfeed.py 项目: dxcv/Skywalker
    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
示例#11
0
 def getBars(self,
             instrument,
             frequency,
             timezone=None,
             fromDateTime=None,
             toDateTime=None,
             extra=None):
     instrument = normalize_instrument(instrument)
     sql = "select bar.timestamp, bar.open, bar.high, bar.low, bar.close, bar.volume, bar.adj_close, bar.frequency{extra}" \
           " from bar join instrument on (bar.instrument_id = instrument.instrument_id)" \
           " where instrument.name = %s and bar.frequency = %s"
     extraFieldsString = ''
     if extra is None:
         extra = []
     for i in range(0, len(extra)):
         extraFieldsString = extraFieldsString + ", " + "bar." + extra[i]
     args = [instrument, frequency]
     if fromDateTime is not None:
         sql += " and bar.timestamp >= %s"
         args.append(dt.datetime_to_timestamp(fromDateTime))
     if toDateTime is not None:
         sql += " and bar.timestamp <= %s"
         args.append(dt.datetime_to_timestamp(toDateTime))
     sql += " order by bar.timestamp asc"
     cursor = self.__connection.cursor()
     cursor.execute(sql.format(extra=extraFieldsString), args)
     self.__connection.commit()
     ret = []
     for row in cursor:
         dateTime = dt.timestamp_to_datetime(row[0])
         if timezone:
             dateTime = dt.localize(dateTime, timezone)
         extraRow = {}
         for i in range(0, len(extra)):
             extraRow[extra[i]] = row[8 + i]
         ret.append(
             bar.BasicBar(dateTime,
                          row[1],
                          row[2],
                          row[3],
                          row[4],
                          row[5],
                          row[6],
                          row[7],
                          extra=extraRow))
     cursor.close()
     return ret
示例#12
0
    def __parseDateTime(self, dateTime):
        ret = None
        if self.__frequency == skywalker.bar.Frequency.MINUTE:
            ret = parse_datetime(dateTime)
        elif self.__frequency == skywalker.bar.Frequency.DAY:
            ret = datetime.datetime.strptime(dateTime, "%Y%m%d")
            # Time on CSV files is empty. If told to set one, do it.
            if self.__dailyBarTime is not None:
                ret = datetime.datetime.combine(ret, self.__dailyBarTime)
        else:
            assert (False)

        # According to NinjaTrader documentation the exported data will be in UTC.
        ret = pytz.utc.localize(ret)

        # Localize bars if a market session was set.
        if self.__timezone:
            ret = dt.localize(ret, self.__timezone)
        return ret