Exemplo n.º 1
0
 def testResampleBarFeedWithMultipleInstrumentsFails(self):
     with self.assertRaisesRegex(Exception, "Only barfeeds with 1 instrument can be resampled"):
         with common.TmpDir() as tmp_path:
             feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
             feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
             feed.addBarsFromCSV("spb", common.get_data_file_path("nt-spy-minute-2011.csv"))
             resample.resample_to_csv(feed, bar.Frequency.HOUR, os.path.join(tmp_path, "any.csv"))
Exemplo n.º 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)))
Exemplo n.º 3
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.BaseStrategy(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))
Exemplo n.º 4
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
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 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())
Exemplo n.º 8
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)))
Exemplo n.º 9
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
Exemplo n.º 10
0
 def testInvalidFrequency(self):
     with self.assertRaisesRegex(Exception, "Invalid frequency.*"):
         ninjatraderfeed.Feed(bar.Frequency.WEEK)
Exemplo n.º 11
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)