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.
This commit is contained in:
Eddie Hebert
2014-03-30 13:33:45 -04:00
parent c09d501fbf
commit b5dbaf88d1
2 changed files with 7 additions and 9 deletions
+3 -7
View File
@@ -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()
+4 -2
View File
@@ -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