diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 1d82bc66..2fa99d9b 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -184,6 +184,8 @@ class TradingEnvironment(object): self.first_open = self.calculate_first_open() self.last_close = self.calculate_last_close() + self.prior_day_open = self.calculate_prior_day_open() + def calculate_first_open(self): """ Finds the first trading day on or after self.period_start. @@ -197,6 +199,24 @@ class TradingEnvironment(object): first_open = self.set_NYSE_time(first_open, 9, 30) return first_open + def calculate_prior_day_open(self): + """ + Finds the first trading day open that falls at least a day + before period_start. + """ + one_day = datetime.timedelta(days=1) + first_open = self.period_start - one_day + + if first_open <= self.trading_days[0]: + log.warn("Cannot calculate prior day open.") + return self.period_start + + while not self.is_trading_day(first_open): + first_open = first_open - one_day + + first_open = self.set_NYSE_time(first_open, 9, 30) + return first_open + def calculate_last_close(self): """ Finds the last trading day on or before self.period_end diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index e64ba86a..ba474f3b 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -55,6 +55,9 @@ class TradeSimulationClient(object): self.style = sim_style self.algo_sim = None + self.warmup_start = self.environment.prior_day_open + self.algo_start = self.environment.first_open + def get_hash(self): """ There should only ever be one TSC in the system. @@ -96,6 +99,7 @@ class TradeSimulationClient(object): with_portfolio, ordering_client.state, self.algo, + self.algo_start ) # The algorithm will yield a daily_results message (as @@ -107,7 +111,7 @@ class TradeSimulationClient(object): class AlgorithmSimulator(object): - def __init__(self, stream_in, order_book, algo): + def __init__(self, stream_in, order_book, algo, algo_start): self.stream_in = stream_in @@ -121,6 +125,7 @@ class AlgorithmSimulator(object): self.algo = algo self.sids = algo.get_sid_filter() + self.algo_start = algo_start # Monkey patch the user algorithm to place orders in the # TransactionSimulator's order book. @@ -212,6 +217,17 @@ class AlgorithmSimulator(object): self.algo.initialize() for event in self.stream_in: + + # We're still in the warmup period. Use the event to + # update our universe, but don't start a snapshot or + # pass anything to handle_data. Discard any + # perf messages. + if event.dt < self.algo_start: + self.update_universe(event) + if event.perf_message: + log.info("Discarding perf message because we're in warmup.") + continue + # Yield any perf messages received to be relayed back to # the browser.