From 5422970d13cdca9fbfab704d1180c4f7bf380411 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Fri, 5 Apr 2013 13:55:04 -0400 Subject: [PATCH] BUG: Stop intraday performance from emitting all transactions. The intraday performance results were emitting all transactions for the entire day up to that point, instead of the desired transaction list for the current timestamp. Add a `dt` parameter to the `to_dict` method of PerformancePeriod so that the transactions are limited to a specific datetime. When the parameter is `None`, a todays_performance object will function as previously with returning all transactions for the day. # Please enter the commit message for your changes. Lines starting --- tests/test_perf_tracking.py | 6 ++++++ zipline/finance/performance.py | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index 1cfacfcd..ca526942 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -1069,3 +1069,9 @@ class TestPerformanceTracker(unittest.TestCase): messages += tracker.process_event(foo_event_3) self.assertEquals(2, len(messages)) + + self.assertEquals(1, len(messages[0]['intraday_perf']['transactions']), + "The first message should contain one transaction.") + # Check that transactions aren't emitted for previous events. + self.assertEquals(0, len(messages[1]['intraday_perf']['transactions']), + "The second message should have no transactions.") diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 5bbac867..5d5ad5c4 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -245,7 +245,8 @@ class PerformanceTracker(object): # its own configuration down the line. # Naming as intraday to make clear that these results are # being updated per minute - _dict['intraday_perf'] = self.todays_performance.to_dict() + _dict['intraday_perf'] = self.todays_performance.to_dict( + self.saved_dt) return _dict @@ -634,10 +635,13 @@ class PerformancePeriod(object): return rval - def to_dict(self): + def to_dict(self, dt=None): """ Creates a dictionary representing the state of this performance period. See header comments for a detailed description. + + Kwargs: + dt (datetime): If present, only return transactions for the dt. """ rval = self.__core_dict() @@ -647,7 +651,14 @@ class PerformancePeriod(object): # we want the key to be absent, not just empty if self.keep_transactions: - transactions = [x.__dict__ for x in self.processed_transactions] + if dt: + # Only include transactions for given dt + transactions = [x.__dict__ + for x in self.processed_transactions + if x.dt == dt] + else: + transactions = [x.__dict__ + for x in self.processed_transactions] rval['transactions'] = transactions return rval