def testWithIntegerTimezone(self): try: barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, -3) self.assertTrue(False, "Exception expected") except Exception as 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 as e: self.assertTrue(str(e).find("timezone as an int parameter is not supported anymore") == 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"))
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)))
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)))
def __loadBarFeed(self): ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFilter = csvfeed.USEquitiesRTH() ret.setBarFilter(barFilter) ret.addBarsFromCSV(INSTRUMENT, common.get_data_file_path("nt-spy-minute-2011.csv")) return ret
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))
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
def load_intraday_bars(): global feed print "Loading bars from file" feed = ninjatraderfeed.Feed(bar.Frequency.MINUTE) # feed.setBarFilter(csvfeed.DateRangeFilter(dt.as_utc(datetime.datetime(2008, 1, 1)), dt.as_utc(datetime.datetime(2008, 12, 31)))) feed.setBarFilter(csvfeed.DateRangeFilter(dt.as_utc(datetime.datetime(2008, 1, 1)), dt.as_utc(datetime.datetime(2008, 3, 31)))) feed.addBarsFromCSV(instrument, "/Users/gabo/Downloads/etf-quotes/SPY.Last.txt")
def __createStrategy(self): barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFilter = csvfeed.USEquitiesRTH() barFeed.setBarFilter(barFilter) barFeed.addBarsFromCSV( TradesAnalyzerTestCase.TestInstrument, common.get_data_file_path("nt-spy-minute-2011.csv")) return strategy_test.TestStrategy(barFeed, 1000)
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)
def __loadIntradayBarFeed(self): ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) ret.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv")) # This is need to get session close attributes set. Strategy class is responsible for calling this. ret.start() # Process all events to get the dataseries fully loaded. while not ret.stopDispatching(): ret.dispatch() return ret
def loadIntradayBarFeed(self): fromMonth = 1 toMonth = 1 fromDay = 3 toDay = 3 barFilter = csvfeed.USEquitiesRTH(us_equities_datetime(2011, fromMonth, fromDay, 00, 00), us_equities_datetime(2011, toMonth, toDay, 23, 59)) barFeed = ninjatraderfeed.Feed(barfeed.Frequency.MINUTE) barFeed.setBarFilter(barFilter) barFeed.addBarsFromCSV(BaseTestCase.TestInstrument, common.get_data_file_path("nt-spy-minute-2011.csv")) return barFeed
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)
class NinjaTraderTestCase(unittest.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)
class NinjaTraderTestCase(unittest.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")) # This is need to get session close attributes set. Strategy class is responsible for calling this. ret.start() # Process all events to get the dataseries fully loaded. while not ret.stopDispatching(): ret.dispatch() ret.stop() ret.join() return ret def testWithTimezone(self): timeZone = marketsession.USEquities.getTimezone() barFeed = self.__loadIntradayBarFeed(timeZone) ds = barFeed.getDataSeries() for i in xrange(ds.getLength()): currentBar = ds.getValueAbsolute(i) self.assertFalse(dt.datetime_is_naive(currentBar.getDateTime())) def testWithoutTimezone(self): barFeed = self.__loadIntradayBarFeed(None) ds = barFeed.getDataSeries() for i in xrange(ds.getLength()): currentBar = ds.getValueAbsolute(i) # Datetime must be set to UTC. self.assertFalse(dt.datetime_is_naive(currentBar.getDateTime())) 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)
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)
def testReset(self): barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFeed.addBarsFromCSV( INSTRUMENT, common.get_data_file_path("nt-spy-minute-2011.csv")) barFeed.loadAll() ds = barFeed.getDataSeries(INSTRUMENT) barFeed.reset() barFeed.loadAll() reloadedDs = barFeed.getDataSeries(INSTRUMENT) self.assertEqual(len(reloadedDs), len(ds)) self.assertNotEqual(reloadedDs, ds) for i in range(len(ds)): self.assertEqual(ds[i].getDateTime(), reloadedDs[i].getDateTime()) self.assertEqual(ds[i].getClose(), reloadedDs[i].getClose())
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())
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())
def testBaseFeedInterface(self): barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFeed.addBarsFromCSV( INSTRUMENT, common.get_data_file_path("nt-spy-minute-2011.csv")) feed_test.tstBaseFeedInterface(self, barFeed)
def __loadIntradayBarFeed(self, timeZone=None): ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timeZone) ret.addBarsFromCSV(INSTRUMENT, common.get_data_file_path("nt-spy-minute-2011.csv")) ret.loadAll() return ret
def testInvalidFrequency(self): with self.assertRaisesRegexp(Exception, "Invalid frequency.*"): ninjatraderfeed.Feed(bar.Frequency.WEEK)
def testBaseBarFeed(self): barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFeed.addBarsFromCSV( INSTRUMENT, common.get_data_file_path("nt-spy-minute-2011.csv")) barfeed_test.check_base_barfeed(self, barFeed, False)