Example #1
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)))
Example #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)))
Example #3
0
    def testResample(self):
        barDs = bards.BarDataSeries()
        resampledDS = resampled_ds.ResampledDataSeries(
            barDs.getCloseDataSeries(), bar.Frequency.MINUTE, sum)
        resampledBarDS = resampled_ds.ResampledBarDataSeries(
            barDs, bar.Frequency.MINUTE)

        barDs.append(
            bar.BasicBar(datetime.datetime(2011, 1, 1, 1, 1, 1), 2.1, 3, 1, 2,
                         10, 1, bar.Frequency.SECOND))
        barDs.append(
            bar.BasicBar(datetime.datetime(2011, 1, 1, 1, 1, 2), 2, 3, 1, 2.3,
                         10, 2, bar.Frequency.SECOND))
        barDs.append(
            bar.BasicBar(datetime.datetime(2011, 1, 1, 1, 2, 1), 2, 3, 1, 2,
                         10, 2, bar.Frequency.SECOND))

        self.assertEqual(len(resampledBarDS), 1)
        self.assertEqual(resampledBarDS[0].getDateTime(),
                         datetime.datetime(2011, 1, 1, 1, 1))
        self.assertEqual(resampledBarDS[0].getOpen(), 2.1)
        self.assertEqual(resampledBarDS[0].getHigh(), 3)
        self.assertEqual(resampledBarDS[0].getLow(), 1)
        self.assertEqual(resampledBarDS[0].getClose(), 2.3)
        self.assertEqual(resampledBarDS[0].getVolume(), 20)
        self.assertEqual(resampledBarDS[0].getAdjClose(), 2)
        self.assertEqual(resampledDS[-1], 2 + 2.3)

        resampledBarDS.pushLast()
        self.assertEqual(len(resampledBarDS), 2)
        self.assertEqual(resampledBarDS[1].getDateTime(),
                         datetime.datetime(2011, 1, 1, 1, 2))
        self.assertEqual(resampledBarDS[1].getOpen(), 2)
        self.assertEqual(resampledBarDS[1].getHigh(), 3)
        self.assertEqual(resampledBarDS[1].getLow(), 1)
        self.assertEqual(resampledBarDS[1].getClose(), 2)
        self.assertEqual(resampledBarDS[1].getVolume(), 10)
        self.assertEqual(resampledBarDS[1].getAdjClose(), 2)

        resampledDS.pushLast()
        self.assertEqual(resampledDS[1], 2)
Example #4
0
    def testCheckNow(self):
        barDs = bards.BarDataSeries()
        resampledBarDS = resampled_ds.ResampledBarDataSeries(
            barDs, bar.Frequency.MINUTE)

        barDateTime = datetime.datetime(2014, 7, 7, 22, 46, 28, 10000)
        barDs.append(
            bar.BasicBar(barDateTime, 2.1, 3, 1, 2, 10, 1,
                         bar.Frequency.MINUTE))
        self.assertEqual(len(resampledBarDS), 0)

        resampledBarDS.checkNow(barDateTime + datetime.timedelta(minutes=1))
        self.assertEqual(len(resampledBarDS), 1)
        self.assertEqual(barDs[0].getOpen(), resampledBarDS[0].getOpen())
        self.assertEqual(barDs[0].getHigh(), resampledBarDS[0].getHigh())
        self.assertEqual(barDs[0].getLow(), resampledBarDS[0].getLow())
        self.assertEqual(barDs[0].getClose(), resampledBarDS[0].getClose())
        self.assertEqual(barDs[0].getVolume(), resampledBarDS[0].getVolume())
        self.assertEqual(barDs[0].getAdjClose(),
                         resampledBarDS[0].getAdjClose())
        self.assertEqual(resampledBarDS[0].getDateTime(),
                         datetime.datetime(2014, 7, 7, 22, 46))
Example #5
0
def resample_impl(barFeed, frequency, csvFile):
    instruments = barFeed.getRegisteredInstruments()

    if len(instruments) != 1:
        raise Exception("Only barfeeds with 1 instrument can be resampled")

    csvWriter = CSVFileWriter(csvFile)

    def on_bar(ds, dateTime, value):
        csvWriter.writeBar(value)

    if not (type(instruments) == list):
        instruments = list(instruments)

    insrumentDS = barFeed[instruments[0]]
    resampledDS = resampled.ResampledBarDataSeries(insrumentDS, frequency)
    resampledDS.getNewValueEvent().subscribe(on_bar)

    # Process all bars.
    disp = dispatcher.Dispatcher()
    disp.addSubject(barFeed)
    disp.run()

    resampledDS.pushLast()