From ccb7f493f7886be5232a767a0b51292dc0293fee Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Tue, 19 Nov 2013 14:39:19 -0500 Subject: [PATCH] PERF: Only update the portfolio once per dt. So that each reference to `.portfolio` in the algoscript, cache the value of the portfolio, and mark the need for a new value at the end of each dt in the tradesimulation loop. --- zipline/algorithm.py | 8 +++++++- zipline/gens/tradesimulation.py | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 4344d1d5..bdc304e1 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -130,6 +130,9 @@ class TradingAlgorithm(object): if not self.blotter: self.blotter = Blotter() + self.portfolio_needs_update = True + self._portfolio = None + # an algorithm subclass needs to set initialized to True when # it is fully initialized. self.initialized = False @@ -397,7 +400,10 @@ class TradingAlgorithm(object): def updated_portfolio(self): # internally this will cause a refresh of the # period performance calculations. - return self.perf_tracker.get_portfolio() + if self.portfolio_needs_update: + self._portfolio = self.perf_tracker.get_portfolio() + self.portfolio_needs_update = False + return self._portfolio def set_logger(self, logger): self.logger = logger diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index f8bd0ff3..db72f5bd 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -195,6 +195,8 @@ class AlgorithmSimulator(object): ) self.algo.perf_tracker.handle_intraday_close() + self.portfolio_needs_update = True + risk_message = self.algo.perf_tracker.handle_simulation_end() yield risk_message