diff --git a/zipline/finance/performance/tracker.py b/zipline/finance/performance/tracker.py index 1b8b21e2d9..3c529bc9eb 100644 --- a/zipline/finance/performance/tracker.py +++ b/zipline/finance/performance/tracker.py @@ -346,7 +346,7 @@ def process_close_position(self, event): if txn: self.process_transaction(txn) - def check_upcoming_dividends(self, next_trading_day): + def check_upcoming_dividends(self, completed_date): """ Check if we currently own any stocks with dividends whose ex_date is the next trading day. Track how much we should be payed on those @@ -361,6 +361,13 @@ def check_upcoming_dividends(self, next_trading_day): # period, so bail. return + # Get the next trading day and, if it is outside the bounds of the + # simulation, bail. + next_trading_day = TradingEnvironment.instance().\ + next_trading_day(completed_date) + if (next_trading_day is None) or (next_trading_day >= self.last_close): + return + # Dividends whose ex_date is the next trading day. We need to check if # we own any of these stocks so we know to pay them out when the pay # date comes. @@ -402,13 +409,9 @@ def handle_minute_close(self, dt): bench_since_open, account) - # if this is the close, save the returns objects for cumulative risk - # calculations and update dividends for the next day. + # if this is the close, update dividends for the next day. if dt == self.market_close: - next_trading_day = TradingEnvironment.instance().\ - next_trading_day(todays_date) - if next_trading_day: - self.check_upcoming_dividends(next_trading_day) + self.check_upcoming_dividends(todays_date) def handle_intraday_market_close(self, new_mkt_open, new_mkt_close): """ @@ -460,9 +463,8 @@ def handle_market_close_daily(self): self.todays_performance.period_open = self.market_open self.todays_performance.period_close = self.market_close - next_trading_day = env.next_trading_day(completed_date) - if next_trading_day: - self.check_upcoming_dividends(next_trading_day) + # Check for any dividends + self.check_upcoming_dividends(completed_date) return daily_update