From 7ce3667bc2401a420e7aaa45fee970f52c025c37 Mon Sep 17 00:00:00 2001 From: scottsanderson Date: Tue, 14 Aug 2012 15:28:02 -0400 Subject: [PATCH 1/3] hard coded one-day warmup --- zipline/finance/trading.py | 20 ++++++++++++++++++++ zipline/gens/tradesimulation.py | 18 +++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) 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. From 473dafcfb6e7d6c0862f4f1f1b97283c16b66bc0 Mon Sep 17 00:00:00 2001 From: scottsanderson Date: Tue, 14 Aug 2012 16:31:00 -0400 Subject: [PATCH 2/3] fix DONE bug when all events are in the warmup period --- zipline/gens/tradesimulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index ba474f3b..22afa34f 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -222,7 +222,7 @@ class AlgorithmSimulator(object): # 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: + if event.dt != 'DONE' and event.dt < self.algo_start: self.update_universe(event) if event.perf_message: log.info("Discarding perf message because we're in warmup.") From 76ff8e93eb3c259a2273e07849207dd0dadba5ca Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Tue, 14 Aug 2012 16:50:24 -0400 Subject: [PATCH 3/3] Adds source id when specifying events. So that when we add events for testing, we populate source_ids. --- zipline/gens/tradegens.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zipline/gens/tradegens.py b/zipline/gens/tradegens.py index e099091d..0842b3bd 100644 --- a/zipline/gens/tradegens.py +++ b/zipline/gens/tradegens.py @@ -101,10 +101,16 @@ class SpecificEquityTrades(object): def get_hash(self): return self.__class__.__name__ + "-" + self.arg_string + def update_source_id(self, gen): + for event in gen: + event.source_id = self.get_hash() + yield event + def create_fresh_generator(self): if self.event_list: - unfiltered = (event for event in self.event_list) + event_gen = (event for event in self.event_list) + unfiltered = self.update_source_id(event_gen) # Set up iterators for each expected field. else: