From 0f88e4133dfc8e42f639db7f9d31ef53fa7cefd6 Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Sun, 30 Dec 2012 17:26:07 -0500 Subject: [PATCH] ENH: New batch_transform feature: compute_only_full. --- tests/test_transforms.py | 3 +++ zipline/test_algorithms.py | 10 +++++++++- zipline/transforms/utils.py | 23 +++++++++++++---------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 1d0197a9..8b775990 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -359,6 +359,9 @@ class TestBatchTransform(TestCase): for data in algo.history_return_ticks[wl:]: self.assertTrue(isinstance(data, deque)) + for data in algo.history_return_not_full: + self.assertIsNot(data, None) + # test overloaded class for test_history in [algo.history_return_price_class, algo.history_return_price_decorator]: diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index c82edcf8..b22d2719 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -273,6 +273,7 @@ class BatchTransformAlgorithm(TradingAlgorithm): self.history_return_field_filter = [] self.history_return_field_no_filter = [] self.history_return_ticks = [] + self.history_return_not_full = [] self.return_price_class = ReturnPriceBatchTransform( refresh_period=self.refresh_period, @@ -333,10 +334,15 @@ class BatchTransformAlgorithm(TradingAlgorithm): self.return_ticks = return_data( refresh_period=self.refresh_period, window_length=self.window_length, - clean_nans=True, create_panel=False ) + self.return_not_full = return_data( + refresh_period=0, + window_length=self.window_length, + compute_only_full=False + ) + self.iter = 0 self.set_slippage(FixedSlippage()) @@ -351,6 +357,8 @@ class BatchTransformAlgorithm(TradingAlgorithm): data, *self.args, **self.kwargs)) self.history_return_ticks.append( self.return_ticks.handle_data(data)) + self.history_return_not_full.append( + self.return_not_full.handle_data(data)) new_data = deepcopy(data) for sid in new_data: diff --git a/zipline/transforms/utils.py b/zipline/transforms/utils.py index 8182441c..0fd647b5 100644 --- a/zipline/transforms/utils.py +++ b/zipline/transforms/utils.py @@ -346,7 +346,9 @@ class BatchTransform(EventWindow): clean_nans=True, sids=None, fields=None, - create_panel=True): + create_panel=True, + compute_only_full=True): + """Instantiate new batch_transform object. :Arguments: @@ -374,6 +376,9 @@ class BatchTransform(EventWindow): If True, will pass the underlying deque reference directly to the function which will be significantly faster. + compute_only_full : bool + Only call the user-defined function once the window is + full. Returns None if window is not full yet. """ super(BatchTransform, self).__init__(True, @@ -386,6 +391,7 @@ class BatchTransform(EventWindow): self.clean_nans = clean_nans self.create_panel = create_panel + self.compute_only_full = compute_only_full self.sids = sids if isinstance(self.sids, (str, int)): @@ -451,7 +457,6 @@ class BatchTransform(EventWindow): if self.field_names is None: self.field_names = self._extract_field_names(event) self.last_dt = event.dt - return # update trading day counters if self.last_dt.day != event.dt.day: @@ -459,15 +464,14 @@ class BatchTransform(EventWindow): self.trading_days_since_update += 1 self.trading_days_total += 1 - if ( - self.trading_days_total >= self.window_length and - self.trading_days_since_update >= self.refresh_period - ): + if self.trading_days_total >= self.window_length: + self.full = True + + if self.trading_days_since_update >= self.refresh_period: # Setting updated to True will cause get_transform_value() # to call the user-defined batch-transform with the most # recent datapanel self.updated = True - self.full = True self.trading_days_since_update = 0 else: self.updated = False @@ -517,8 +521,7 @@ class BatchTransform(EventWindow): return data def handle_remove(self, event): - # since an event is expiring, we know the window is full - self.full = True + pass def get_value(self, *args, **kwargs): raise NotImplementedError( @@ -532,7 +535,7 @@ class BatchTransform(EventWindow): has actually been updated. Otherwise, the previously, cached value will be returned. """ - if not self.full: + if self.compute_only_full and not self.full: return None if self.updated: