def handle_minute_close(self, dt): todays_date = dt.replace(hour=0, minute=0, second=0, microsecond=0) minute_returns = self.minute_performance.returns self.minute_performance.rollover() algo_minute_returns = pd.Series({dt: minute_returns}) bench_minute_returns = pd.Series({dt: self.all_benchmark_returns[dt]}) # the intraday risk is calculated on top of minute performance # returns for the bench and the algo self.intraday_risk_metrics.update(dt, algo_minute_returns, bench_minute_returns) bench_since_open = \ self.intraday_risk_metrics.benchmark_period_returns[-1] benchmark_returns = pd.Series({todays_date: bench_since_open}) # if we've reached market close, check on dividends if dt == self.market_close: for perf_period in self.perf_periods: perf_period.update_dividends(todays_date) algorithm_returns = pd.Series( {todays_date: self.todays_performance.returns}) self.cumulative_risk_metrics.update(todays_date, algorithm_returns, benchmark_returns) # if this is the close, save the returns objects for cumulative # risk calculations if dt == self.market_close: todays_return_obj = zp.DailyReturn(todays_date, self.todays_performance.returns) self.returns.append(todays_return_obj)
def handle_market_close(self): # add the return results from today to the list of DailyReturn objects. todays_date = self.market_close.replace(hour=0, minute=0, second=0, microsecond=0) self.cumulative_performance.update_dividends(todays_date) self.todays_performance.update_dividends(todays_date) todays_return_obj = zp.DailyReturn(todays_date, self.todays_performance.returns) self.returns.append(todays_return_obj) #update risk metrics for cumulative performance self.cumulative_risk_metrics.update( todays_return_obj.date, todays_return_obj.returns, self.all_benchmark_returns[todays_return_obj.date]) # increment the day counter before we move markers forward. self.day_count += 1.0 # Take a snapshot of our current peformance to return to the # browser. daily_update = self.to_dict() # On the last day of the test, don't create tomorrow's performance # period. We may not be able to find the next trading day if we're # at the end of our historical data if self.market_close >= self.last_close: return daily_update #move the market day markers forward self.market_open, self.market_close = \ trading.environment.next_open_and_close(self.market_open) # Roll over positions to current day. self.todays_performance.rollover() self.todays_performance.period_open = self.market_open self.todays_performance.period_close = self.market_close # The dividend calculation for the daily needs to be made # after the rollover. midnight_between is the last midnight # hour between the close of markets and the next open. To # make sure midnight_between matches identically with # dividend data dates, it is in UTC. midnight_between = self.market_open.replace(hour=0, minute=0, second=0, microsecond=0) self.cumulative_performance.update_dividends(midnight_between) self.todays_performance.update_dividends(midnight_between) return daily_update