ENH: New batch_transform feature: compute_only_full.

This commit is contained in:
Thomas Wiecki
2012-12-30 17:26:07 -05:00
committed by Eddie Hebert
parent d1dace948e
commit 0f88e4133d
3 changed files with 25 additions and 11 deletions
+3
View File
@@ -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]:
+9 -1
View File
@@ -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:
+13 -10
View File
@@ -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 <default=True>
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: