def __createStrategy(self): bar_feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) bar_filter = csvfeed.USEquitiesRTH() bar_feed.set_bar_filter(bar_filter) bar_feed.add_bars_from_csv( TradesAnalyzerTestCase.TestInstrument, common.get_data_file_path("nt-spy-minute-2011.csv")) return strategy_test.TestStrategy(bar_feed, 1000)
def __loadIntradayBarFeed(self, timeZone = None): ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timeZone) ret.add_bars_from_csv("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.stop_dispatching(): ret.dispatch() ret.stop() ret.join() return ret
def loadIntradayBarFeed(self): from_month = 1 to_month = 1 from_day = 3 to_day = 3 bar_filter = barfeed.USEquitiesRTH( us_equities_datetime(2011, from_month, from_day, 00, 00), us_equities_datetime(2011, to_month, to_day, 23, 59)) bar_feed = ninjatraderfeed.Feed(bar.Frequency.MINUTE) bar_feed.set_bar_filter(bar_filter) bar_feed.add_bars_from_csv( StrategyTestCase.TestInstrument, common.get_data_file_path("nt-spy-minute-2011.csv")) return bar_feed
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, } bar_feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timezone) bar_feed.add_bars_from_csv("spy", common.get_data_file_path("nt-spy-minute-2011-03.csv")) for bars in bar_feed: price = prices.get(bars.get_date_time(), None) if price != None: self.assertTrue(price == bars.get_bar("spy").get_close())
class NinjaTraderTestCase(unittest.TestCase): def __loadIntradayBarFeed(self, timeZone = None): ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, timeZone) ret.add_bars_from_csv("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.stop_dispatching(): ret.dispatch() ret.stop() ret.join() return ret def testWithTimezone(self): timeZone = marketsession.USEquities.getTimezone() bar_feed = self.__loadIntradayBarFeed(timeZone) ds = bar_feed.get_data_series() for i in xrange(ds.get_length()): current_bar = ds[i] self.assertFalse(dt.datetime_is_naive(current_bar.get_date_time())) def testWithoutTimezone(self): bar_feed = self.__loadIntradayBarFeed(None) ds = bar_feed.get_data_series() for i in xrange(ds.get_length()): current_bar = ds[i] # Datetime must be set to UTC. self.assertFalse(dt.datetime_is_naive(current_bar.get_date_time())) def testWithIntegerTimezone(self): try: bar_feed = 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: bar_feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) bar_feed.add_bars_from_csv("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 testWithIntegerTimezone(self): try: bar_feed = 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)