From 358d2a3b5984eaf53e6b3b764fe475c049429daf Mon Sep 17 00:00:00 2001 From: warren-oneill Date: Mon, 23 Mar 2015 11:55:44 +0100 Subject: [PATCH] remove hardcoding of get_early_closes and passing trading_day and trading_days from env to load() --- zipline/finance/trading.py | 41 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 44b516b6..8214fefc 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -23,7 +23,6 @@ import numpy as np from zipline.data.loader import load_market_data from zipline.utils import tradingcalendar -from zipline.utils.tradingcalendar import get_early_closes log = logbook.Logger('Trading') @@ -93,13 +92,34 @@ class TradingEnvironment(object): max_date=None, env_trading_calendar=tradingcalendar ): + # `tc_tdays` is short for "trading calendar trading days" + # `tc_tday` is short for "trading calendar trading day" + tc_tdays = env_trading_calendar.trading_days + tc_tday = env_trading_calendar.trading_day + + if max_date: + self.trading_days = tc_tdays[tc_tdays <= max_date].copy() + self.trading_day = tc_tday[tc_tday <= max_date].copy() + else: + self.trading_days = tc_tdays.copy() + self.trading_day = tc_tday.copy() + + self.first_trading_day = self.trading_days[0] + self.last_trading_day = self.trading_days[-1] + + self.early_closes = env_trading_calendar.get_early_closes( + self.first_trading_day, self.last_trading_day) + + self.open_and_closes = env_trading_calendar.open_and_closes.loc[ + self.trading_days] + self.prev_environment = self self.bm_symbol = bm_symbol if not load: load = load_market_data self.benchmark_returns, treasury_curves_map = \ - load(self.bm_symbol) + load(self.trading_day, self.trading_days, self.bm_symbol) self.treasury_curves = pd.DataFrame(treasury_curves_map).T if max_date: @@ -112,23 +132,6 @@ class TradingEnvironment(object): self.exchange_tz = exchange_tz - # `tc_td` is short for "trading calendar trading days" - tc_td = env_trading_calendar.trading_days - - 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] - - self.early_closes = get_early_closes(self.first_trading_day, - self.last_trading_day) - - self.open_and_closes = env_trading_calendar.open_and_closes.loc[ - self.trading_days] - def __enter__(self, *args, **kwargs): global environment self.prev_environment = environment