From b5dbaf88d13c5774479f9379723f8997b6e9959d Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Sun, 30 Mar 2014 13:24:20 -0400 Subject: [PATCH] BUG: Prevent out of sync market closes in performance tracker. In situations where the performance tracker has been reset or patched to handle state juggling with warming up live data, the `market_close` member of the performance tracker could end up out of sync with the current algo time as determined by the The symptom was dividends never triggering, because the end of day checks would not match the current time. Fix by having the tradesimulation loop be responsible, in minute/minute mode, for advancing the market close and passing that value to the performance tracker, instead of having the market close advanced by the performance tracker as well. --- zipline/finance/performance/tracker.py | 10 +++------- zipline/gens/tradesimulation.py | 6 ++++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/zipline/finance/performance/tracker.py b/zipline/finance/performance/tracker.py index 9e2317e8..74b1291b 100644 --- a/zipline/finance/performance/tracker.py +++ b/zipline/finance/performance/tracker.py @@ -305,19 +305,15 @@ class PerformanceTracker(object): if dt == self.market_close: self.returns[todays_date] = self.todays_performance.returns - def handle_intraday_close(self): + def handle_intraday_close(self, new_mkt_open, new_mkt_close): # update_performance should have been called in handle_minute_close # so it is not repeated here. self.intraday_risk_metrics = \ risk.RiskMetricsCumulative(self.sim_params) # increment the day counter before we move markers forward. self.day_count += 1.0 - # move the market day markers forward - if self.market_close < trading.environment.last_trading_day: - self.market_open, self.market_close = \ - trading.environment.next_open_and_close(self.market_open) - else: - self.market_close = self.sim_params.last_close + self.market_open = new_mkt_open + self.market_close = new_mkt_close def handle_market_close(self): self.update_performance() diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 7f40e320..d5ed767b 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -97,6 +97,7 @@ class AlgorithmSimulator(object): Main generator work loop. """ # Initialize the mkt_close + mkt_open = self.algo.perf_tracker.market_open mkt_close = self.algo.perf_tracker.market_close # inject the current algo @@ -189,7 +190,7 @@ class AlgorithmSimulator(object): tp.rollover() if mkt_close <= self.algo.perf_tracker.last_close: try: - _, mkt_close = \ + mkt_open, mkt_close = \ trading.environment.\ next_open_and_close( mkt_close @@ -198,7 +199,8 @@ class AlgorithmSimulator(object): # If at the end of backtest history, # skip advancing market close. pass - self.algo.perf_tracker.handle_intraday_close() + self.algo.perf_tracker.handle_intraday_close( + mkt_open, mkt_close) self.algo.portfolio_needs_update = True