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 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( os.path.join("samples", "data", "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 __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 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 testWithoutTimezone(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) for dateTime, bars in barFeed: bar = bars.getBar(FeedTestCase.TestInstrument) self.assertTrue(dt.datetime_is_naive(bar.getDateTime()))
def testWithDefaultTimezone(self): barFeed = yahoofeed.Feed( timezone=marketsession.USEquities.getTimezone()) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) for dateTime, bars in barFeed: bar = bars.getBar(FeedTestCase.TestInstrument) self.assertFalse(dt.datetime_is_naive(bar.getDateTime()))
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 to_utc_if_naive(dateTime): if dateTime is not None and dt.datetime_is_naive(dateTime): dateTime = dt.as_utc(dateTime) return dateTime