def testIntradayExitOnClose_BuyOnLastBar(self): bar_feed = self.loadIntradayBarFeed() strat = TestStrategy(bar_feed, 1000) strat.set_exit_on_session_close(True) # 3/Jan/2011 20:59:00 - Enter long # 3/Jan/2011 21:00:00 - Entry gets canceled. strat.addPosEntry(dt.localize(datetime.datetime(2011, 1, 3, 20, 59), pytz.utc), strat.enter_long, StrategyTestCase.TestInstrument, 1, True) strat.run() self.assertTrue(strat.getEnterOkEvents() == 0) self.assertTrue(strat.getExitOkEvents() == 0) self.assertTrue(strat.getEnterCanceledEvents() == 1) self.assertTrue(strat.getExitCanceledEvents() == 0) self.assertTrue(round(strat.get_broker().get_cash(), 2) == 1000)
def testIntradayExitOnClose_BuyOnLastBar(self): bar_feed = self.loadIntradayBarFeed() strat = TestStrategy(bar_feed, 1000) strat.set_exit_on_session_close(True) # 3/Jan/2011 20:59:00 - Enter long # 3/Jan/2011 21:00:00 - Entry gets canceled. strat.addPosEntry( dt.localize(datetime.datetime(2011, 1, 3, 20, 59), pytz.utc), strat.enter_long, StrategyTestCase.TestInstrument, 1, True) strat.run() self.assertTrue(strat.getEnterOkEvents() == 0) self.assertTrue(strat.getExitOkEvents() == 0) self.assertTrue(strat.getEnterCanceledEvents() == 1) self.assertTrue(strat.getExitCanceledEvents() == 0) self.assertTrue(round(strat.get_broker().get_cash(), 2) == 1000)
def __parse_date_time(self, date_time): ret = None if self.__frequency == bar.Frequency.MINUTE: ret = datetime.datetime.strptime(date_time, "%Y%m%d %H%M%S") elif self.__frequency == bar.Frequency.DAY: ret = datetime.datetime.strptime(date_time, "%Y%m%d") # Time on CSV files is empty. If told to set one, do it. if self.__daily_bar_time != None: ret = datetime.datetime.combine(ret, self.__daily_bar_time) else: assert (False) # According to NinjaTrader documentation the exported data will be in UTC. ret = pytz.utc.localize(ret) # Localize bars if a market session was set. if self.__timezone: ret = dt.localize(ret, self.__timezone) return ret
def __parse_date_time(self, date_time): ret = None if self.__frequency == bar.Frequency.MINUTE: ret = datetime.datetime.strptime(date_time, "%Y%m%d %H%M%S") elif self.__frequency == bar.Frequency.DAY: ret = datetime.datetime.strptime(date_time, "%Y%m%d") # Time on CSV files is empty. If told to set one, do it. if self.__daily_bar_time != None: ret = datetime.datetime.combine(ret, self.__daily_bar_time) else: assert(False) # According to NinjaTrader documentation the exported data will be in UTC. ret = pytz.utc.localize(ret) # Localize bars if a market session was set. if self.__timezone: ret = dt.localize(ret, self.__timezone) return ret
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())
def us_equities_datetime(*params): ret = datetime.datetime(*params) ret = dt.localize(ret, marketsession.USEquities.getTimezone()) return ret