def testFeedWithQuandl(self): class RowFilter(csvfeed.RowFilter): def includeRow(self, dateTime, values): return dateTime.year == 2013 feed = csvfeed.Feed("Date", "%Y-%m-%d", maxLen=40, timezone=marketsession.USEquities.timezone) feed.setRowFilter(RowFilter()) feed.setTimeDelta(datetime.timedelta(hours=23, minutes=59, seconds=59)) feed.addValuesFromCSV(common.get_data_file_path("quandl_gold_2.csv")) for col in ["USD", "GBP", "EUR"]: self.assertEqual(len(feed[col]), 0) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() for col in ["USD", "GBP", "EUR"]: self.assertEqual(len(feed[col]), 39) self.assertEqual(feed["USD"][-1], 1333.0) self.assertEqual(feed["GBP"][-1], 831.203) self.assertEqual(feed["EUR"][-1], 986.75) self.assertFalse(dt.datetime_is_naive(feed["USD"].getDateTimes()[-1])) self.assertEqual( feed["USD"].getDateTimes()[-1], dt.localize(datetime.datetime(2013, 9, 29, 23, 59, 59), marketsession.USEquities.timezone) )
def testWithoutTimezone(self): barFeed = self.__loadIntradayBarFeed(None) ds = barFeed.getDataSeries() for i, currentBar in enumerate(ds): # Datetime must be set to UTC. self.assertFalse(dt.datetime_is_naive(currentBar.getDateTime())) self.assertEqual(ds[i].getDateTime(), ds.getDateTimes()[i])
def testWithTimezone(self): timeZone = marketsession.USEquities.getTimezone() barFeed = self.__loadIntradayBarFeed(timeZone) ds = barFeed.getDataSeries() for i, currentBar in enumerate(ds): self.assertFalse(dt.datetime_is_naive(currentBar.getDateTime())) self.assertEqual(ds[i].getDateTime(), ds.getDateTimes()[i])
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 __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 __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 XigniteGlobalRealTime_GetBar(token, identifier, identifierType, endDateTime, precision, period, secureRequest=None): if dt.datetime_is_naive(endDateTime): raise Exception("endDateTime must have a timezone") # Parse the exchange from the identifier. instrument, exchange = parse_instrument_exchange(identifier) if secureRequest is None: secureRequest = USE_SECURE_REQUESTS if secureRequest: scheme = "https" else: scheme = "http" # print datetime_to_string(endDateTime, exchange) params = { "_Token": token, "Identifier": identifier, "IdentifierType": identifierType, "EndTime": datetime_to_string(endDateTime, exchange), "Precision": precision, "Period": period, } parts = (scheme, "globalrealtime.xignite.com", "v3/xGlobalRealTime.json/GetBar", urllib.urlencode(params), "") url = urlparse.urlunsplit(parts) ret = json_http_request(url) if ret.get("Outcome") != "Success": msg = ret.get("Message") if msg is None: msg = "Error %s" % (ret.get("Outcome")) raise XigniteError(msg, ret) return ret
def to_utc_if_naive(dateTime): if dateTime is not None and dt.datetime_is_naive(dateTime): dateTime = dt.as_utc(dateTime) return dateTime