From 279e125ab8e61b8d04cc487e52f17a6e1cadeb6e Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Wed, 20 Mar 2013 14:34:12 -0400 Subject: [PATCH] MAINT: Pull initialization of simulation dt out of event loop. Fakes a 'peek' into the data stream, so that the 'do we have a simulation_dt?' check isn't called on each leoop iteration. --- zipline/gens/tradesimulation.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 91adef27..59e86013 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import itertools from logbook import Logger, Processor from collections import defaultdict @@ -234,16 +234,20 @@ class AlgorithmSimulator(object): """ Main generator work loop. """ + # Set the simulation date to be the first event we see. + peek_date, peek_snapshot = next(stream_in) + self.simulation_dt = peek_date + + # Stitch back together the generator by placing the peeked + # event back in front + stream = itertools.chain([(peek_date, peek_snapshot)], + stream_in) + # inject the current algo # snapshot time to any log record generated. with self.processor.threadbound(): - for date, snapshot in stream_in: - # Set the simulation date to be the first event we see. - # This should only occur once, at the start of the test. - if self.simulation_dt is None: - self.simulation_dt = date - + for date, snapshot in stream: # We're still in the warmup period. Use the event to # update our universe, but don't yield any perf messages, # and don't send a snapshot to handle_data.