コード例 #1
0
ファイル: resample_test.py プロジェクト: zhibzeng/potato
    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)))
コード例 #2
0
 def __loadBarFeed(self):
     ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
     barFilter = csvfeed.USEquitiesRTH()
     ret.setBarFilter(barFilter)
     ret.addBarsFromCSV(TradesAnalyzerTestCase.TestInstrument,
                        common.get_data_file_path("nt-spy-minute-2011.csv"))
     return ret
コード例 #3
0
    def testMaxRecursion(self):
        barFeed = ninjatraderfeed.Feed(bar.Frequency.MINUTE)
        barFeed.addBarsFromCSV(
            "any", common.get_data_file_path("nt-spy-minute-2011.csv"))
        ema = ma.EMA(barFeed["any"].getPriceDataSeries(), 10)
        # Load all the feed.
        barFeed.loadAll()

        # Check that the max recursion limit bug is not hit when generating the last value first.
        self.assertEqual(round(ema[-1], 2), 128.81)
コード例 #4
0
    def testBounded(self):
        barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, maxLen=2)
        barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011-03.csv"))
        barFeed.loadAll()

        barDS = barFeed["spy"]
        self.assertEqual(len(barDS), 2)
        self.assertEqual(len(barDS.getDateTimes()), 2)
        self.assertEqual(len(barDS.getCloseDataSeries()), 2)
        self.assertEqual(len(barDS.getCloseDataSeries().getDateTimes()), 2)
        self.assertEqual(len(barDS.getOpenDataSeries()), 2)
        self.assertEqual(len(barDS.getHighDataSeries()), 2)
        self.assertEqual(len(barDS.getLowDataSeries()), 2)
        self.assertEqual(len(barDS.getAdjCloseDataSeries()), 2)
コード例 #5
0
 def testLocalizeAndFilter(self):
     timezone = marketsession.USEquities.getTimezone()
     # The prices come from NinjaTrader interface when set to use 'US Equities RTH' session template.
     prices = {
         dt.localize(datetime.datetime(2011, 3, 9, 9, 31), timezone): 132.35,
         dt.localize(datetime.datetime(2011, 3, 9, 16), timezone): 132.39,
         dt.localize(datetime.datetime(2011, 3, 10, 9, 31), timezone): 130.81,
         dt.localize(datetime.datetime(2011, 3, 10, 16), timezone): 129.92,
         dt.localize(datetime.datetime(2011, 3, 11, 9, 31), timezone): 129.72,
         dt.localize(datetime.datetime(2011, 3, 11, 16), timezone): 130.84,
     }
     barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timezone)
     barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011-03.csv"))
     for dateTime, bars in barFeed:
         price = prices.get(bars.getDateTime(), None)
         if price is not None:
             self.assertTrue(price == bars.getBar("spy").getClose())
コード例 #6
0
class NinjaTraderTestCase(common.TestCase):
    def __loadIntradayBarFeed(self, timeZone=None):
        ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timeZone)
        ret.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
        ret.loadAll()
        return ret

    def testBaseFeedInterface(self):
        barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
        barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
        feed_test.tstBaseFeedInterface(self, barFeed)

    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 = 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 testWithIntegerTimezone(self):
        try:
            barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, -3)
            self.assertTrue(False, "Exception expected")
        except Exception, e:
            self.assertTrue(str(e).find("timezone as an int parameter is not supported anymore") == 0)

        try:
            barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
            barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"), -5)
            self.assertTrue(False, "Exception expected")
        except Exception, e:
            self.assertTrue(str(e).find("timezone as an int parameter is not supported anymore") == 0)
コード例 #7
0
    def testReset(self):
        instrument = "spy"
        barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
        barFeed.addBarsFromCSV(instrument, common.get_data_file_path("nt-spy-minute-2011.csv"))

        barFeed.loadAll()
        instruments = barFeed.getRegisteredInstruments()
        ds = barFeed[instrument]

        barFeed.reset()
        barFeed.loadAll()
        reloadedDs = barFeed[instrument]

        self.assertEqual(len(reloadedDs), len(ds))
        self.assertNotEqual(reloadedDs, ds)
        self.assertEqual(instruments, barFeed.getRegisteredInstruments())
        for i in range(len(ds)):
            self.assertEqual(ds[i].getDateTime(), reloadedDs[i].getDateTime())
            self.assertEqual(ds[i].getClose(), reloadedDs[i].getClose())
コード例 #8
0
ファイル: resample_test.py プロジェクト: zhibzeng/potato
    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)))
コード例 #9
0
    def testIntraDay(self):
        barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, marketsession.USEquities.getTimezone())
        barFeed.setBarFilter(csvfeed.USEquitiesRTH())
        barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
        strat = strategy_test.TestStrategy(barFeed, 1000)
        stratAnalyzer = sharpe.SharpeRatio(False)
        strat.attachAnalyzer(stratAnalyzer)
        strat.marketOrder("spy", 1)

        strat.run()

        tradingPeriods = 252*6.5*60
        manualAnnualized = sharpe.sharpe_ratio(stratAnalyzer.getReturns(), 0.04, tradingPeriods, True)
        manualNotAnnualized = sharpe.sharpe_ratio(stratAnalyzer.getReturns(), 0.04, tradingPeriods, False)
        analyzerAnnualized = stratAnalyzer.getSharpeRatio(0.04)
        analyzerNotAnnualized = stratAnalyzer.getSharpeRatio(0.04, False)

        self.assertEqual(round(analyzerAnnualized, 10), -1.1814830854)
        self.assertEqual(round(analyzerNotAnnualized, 10), -0.0037659686)
        # They should be similar, but not identical because the analyzer uses 365 days/year
        # when useDailyReturns is set to False.
        self.assertEqual(round(analyzerAnnualized, 1), round(manualAnnualized, 1))
        self.assertEqual(round(analyzerNotAnnualized, 3), round(manualNotAnnualized, 3))
コード例 #10
0
 def testWithIntegerTimezone(self):
     try:
         barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, -3)
         self.assertTrue(False, "Exception expected")
     except Exception, e:
         self.assertTrue(str(e).find("timezone as an int parameter is not supported anymore") == 0)
コード例 #11
0
 def testBaseFeedInterface(self):
     barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
     barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
     feed_test.tstBaseFeedInterface(self, barFeed)
コード例 #12
0
 def __loadIntradayBarFeed(self, timeZone=None):
     ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timeZone)
     ret.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
     ret.loadAll()
     return ret
コード例 #13
0
 def testInvalidFrequency(self):
     with self.assertRaisesRegexp(Exception, "Invalid frequency.*"):
         ninjatraderfeed.Feed(bar.Frequency.WEEK)
コード例 #14
0
 def testBaseBarFeed(self):
     barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
     barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
     barfeed_test.check_base_barfeed(self, barFeed, False)