Пример #1
0
    def testLoadFilterFrom(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"),
                            "bitstampUSD",
                            fromDateTime=dt.as_utc(
                                datetime.datetime(2012, 5, 29)))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEqual(len(loaded), 646)

        self.assertEqual(loaded[0][0],
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEqual(loaded[0][1]["bitstampUSD"].getDateTime(),
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEqual(loaded[0][1]["bitstampUSD"].getClose(), 5.07)
        self.assertEqual(loaded[0][1]["bitstampUSD"].getPrice(), 5.07)
        self.assertEqual(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288)

        self.assertEqual(
            loaded[-1][0],
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEqual(
            loaded[-1][1]["bitstampUSD"].getDateTime(),
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEqual(loaded[-1][1]["bitstampUSD"].getClose(), 5.1)
        self.assertEqual(loaded[-1][1]["bitstampUSD"].getPrice(), 5.1)
        self.assertEqual(loaded[-1][1]["bitstampUSD"].getVolume(), 0.39215686)
Пример #2
0
    def testResampleNinjaTraderHour(self):
        with common.TmpDir() as tmp_path:
            # Resample.
            feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
            feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
            resampledBarDS = resampled_ds.ResampledBarDataSeries(feed["spy"], bar.Frequency.HOUR)
            resampledFile = os.path.join(tmp_path, "hour-nt-spy-minute-2011.csv")
            resample.resample_to_csv(feed, bar.Frequency.HOUR, resampledFile)
            resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

            # Load the resampled file.
            feed = csvfeed.GenericBarFeed(bar.Frequency.HOUR, marketsession.USEquities.getTimezone())
            feed.addBarsFromCSV("spy", resampledFile)
            feed.loadAll()

        self.assertEqual(len(feed["spy"]), 340)
        self.assertEqual(feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3, 9), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1, 1), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][0].getOpen(), 126.35)
        self.assertEqual(feed["spy"][0].getHigh(), 126.45)
        self.assertEqual(feed["spy"][0].getLow(), 126.3)
        self.assertEqual(feed["spy"][0].getClose(), 126.4)
        self.assertEqual(feed["spy"][0].getVolume(), 3397.0)
        self.assertEqual(feed["spy"][0].getAdjClose(), None)

        self.assertEqual(len(resampledBarDS), len(feed["spy"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3, 9)))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1, 1)))
Пример #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)
Пример #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)
Пример #5
0
    def testResampleNinjaTraderDay(self):
        with common.TmpDir() as tmp_path:
            # Resample.
            feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
            feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
            resampledBarDS = resampled_ds.ResampledBarDataSeries(feed["spy"], bar.Frequency.DAY)
            resampledFile = os.path.join(tmp_path, "day-nt-spy-minute-2011.csv")
            resample.resample_to_csv(feed, bar.Frequency.DAY, resampledFile)
            resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

            # Load the resampled file.
            feed = csvfeed.GenericBarFeed(bar.Frequency.DAY)
            feed.addBarsFromCSV("spy", resampledFile, marketsession.USEquities.getTimezone())
            feed.loadAll()

        self.assertEqual(len(feed["spy"]), 25)
        self.assertEqual(feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1), marketsession.USEquities.getTimezone()))

        self.assertEqual(len(resampledBarDS), len(feed["spy"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3)))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1)))
Пример #6
0
    def testLoadFilterFromAndTo(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"),
                            "bitstampUSD",
                            fromDateTime=dt.as_utc(
                                datetime.datetime(2012, 5, 29)),
                            toDateTime=datetime.datetime(2012, 5, 31))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEqual(len(loaded), 579)

        self.assertEqual(loaded[0][0],
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEqual(loaded[0][1]["bitstampUSD"].getDateTime(),
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEqual(loaded[0][1]["bitstampUSD"].getClose(), 5.07)
        self.assertEqual(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288)

        self.assertEqual(loaded[-1][0],
                         dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
        self.assertEqual(loaded[-1][1]["bitstampUSD"].getDateTime(),
                         dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
        self.assertEqual(loaded[-1][1]["bitstampUSD"].getClose(), 5.14)
        self.assertEqual(loaded[-1][1]["bitstampUSD"].getVolume(), 20)
Пример #7
0
    def testLoadNoFilter(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEqual(len(loaded), 9999)

        self.assertEqual(loaded[0][0],
                         dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
        self.assertEqual(loaded[0][1]["BTC"].getDateTime(),
                         dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
        self.assertEqual(loaded[0][1]["BTC"].getClose(), 5.8)
        self.assertEqual(loaded[0][1]["BTC"].getPrice(), 5.8)
        self.assertEqual(loaded[0][1]["BTC"].getVolume(), 1.0)

        self.assertEqual(
            loaded[-1][0],
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEqual(
            loaded[-1][1]["BTC"].getDateTime(),
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEqual(loaded[-1][1]["BTC"].getClose(), 5.1)
        self.assertEqual(loaded[-1][1]["BTC"].getPrice(), 5.1)
        self.assertEqual(loaded[-1][1]["BTC"].getVolume(), 0.39215686)
Пример #8
0
def parse_datetime(dateTime):
    try:
        ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S")
    except ValueError:
        ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S.%f")
    return dt.as_utc(ret)
Пример #9
0
def to_utc_if_naive(dateTime):
    if dateTime is not None and dt.datetime_is_naive(dateTime):
        dateTime = dt.as_utc(dateTime)
    return dateTime