From 4bf8ab0f8dd441a0fa493823e41dcbbd0f1b4be6 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Fri, 28 Mar 2014 13:16:27 -0400 Subject: [PATCH] ENH: Remove dependency on benchmark for trading day calendar. Instead of the benchmarks' index, use the trading calendar to populate the environment's trading days. Remove `extra_date` field, since unlike the benchmarks list, the trading calendar can generate future dates, so dates for current day trading do not need to be appended. Motivations: - The source for the open and close/early close calendar and the trading day calendar is now the same, which should help prevent potential issues due to misalignment. - Allows configurations where the benchmark is provided as a generator based data source to need to supply a second benchmark list just to populate dates. --- zipline/finance/trading.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 83ca04d9..e367be15 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -81,7 +81,7 @@ class TradingEnvironment(object): bm_symbol='^GSPC', exchange_tz="US/Eastern", max_date=None, - extra_dates=None + env_trading_calendar=tradingcalendar ): self.prev_environment = self self.bm_symbol = bm_symbol @@ -93,23 +93,22 @@ class TradingEnvironment(object): self.treasury_curves = pd.DataFrame(treasury_curves_map).T if max_date: - self.treasury_curves = self.treasury_curves.ix[:max_date, :] + tr_c = self.treasury_curves + # Mask the treasury curvers down to the current date. + # In the case of live trading, the last date in the treasury + # curves would be the day before the date considered to be + # 'today'. + self.treasury_curves = tr_c[tr_c.index <= max_date] self.exchange_tz = exchange_tz - bi = self.benchmark_returns.index - if max_date: - self.trading_days = bi[bi <= max_date].copy() - else: - self.trading_days = bi.copy() + # `tc_td` is short for "trading calendar trading days" + tc_td = env_trading_calendar.trading_days - if len(self.benchmark_returns) and extra_dates: - for extra_date in extra_dates: - extra_date = extra_date.replace(hour=0, minute=0, second=0, - microsecond=0) - if extra_date not in self.trading_days: - self.trading_days = self.trading_days + \ - pd.DatetimeIndex([extra_date]) + if max_date: + self.trading_days = tc_td[tc_td <= max_date].copy() + else: + self.trading_days = tc_td.copy() self.first_trading_day = self.trading_days[0] self.last_trading_day = self.trading_days[-1]