Skip to content

Commit

Permalink
Merge c10af2a into 9103516
Browse files Browse the repository at this point in the history
  • Loading branch information
thewassermann committed Aug 1, 2016
2 parents 9103516 + c10af2a commit 3546b2c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/source/whatsnew/1.0.2.txt
Expand Up @@ -26,6 +26,9 @@ Enhancements
:meth:`~zipline.pipeline.factors.Factor.top`, and
:meth:`~zipline.pipeline.factors.Factor.bottom`. (:issue:`1349`).

- Added a smoothing filter that adds 'stickiness' to its input,
making boolean designations less volatile over time. (:issue:`1358`)

Bug Fixes
~~~~~~~~~

Expand Down
30 changes: 30 additions & 0 deletions tests/pipeline/test_filter.py
Expand Up @@ -21,6 +21,7 @@
ones,
ones_like,
putmask,
reshape,
rot90,
sum as np_sum
)
Expand Down Expand Up @@ -395,6 +396,35 @@ def test_isfinite(self):
)
check_arrays(results['isfinite'], isfinite(data))

def test_strictly_true_filter(self):
from zipline.pipeline.filters import StrictlyTrueFilter

data = ~eye(N=self.default_shape[0],
M=self.default_shape[1],
k=1,
dtype=bool)

class InputFilter(Filter):
inputs = ()
window_length = 0

strictly_true_filter = StrictlyTrueFilter(
inputs=[InputFilter()],
window_length=self.default_shape[0]
)

results = self.run_graph(
TermGraph({'Filter': strictly_true_filter}),
initial_workspace={InputFilter(): data}
)

expected_result = full(self.default_shape[1], False, dtype=bool)
expected_result[0] = True
check_arrays(
reshape(results['Filter'], expected_result.shape[0]),
expected_result,
)

@parameter_space(factor_len=[2, 3, 4])
def test_window_safe(self, factor_len):
# all true data set of (days, securities)
Expand Down
2 changes: 2 additions & 0 deletions zipline/pipeline/filters/__init__.py
Expand Up @@ -8,6 +8,7 @@
NumExprFilter,
PercentileFilter,
SingleAsset,
StrictlyTrueFilter,
)

__all__ = [
Expand All @@ -20,4 +21,5 @@
'NumExprFilter',
'PercentileFilter',
'SingleAsset',
'StrictlyTrueFilter',
]
14 changes: 14 additions & 0 deletions zipline/pipeline/filters/filter.py
Expand Up @@ -488,3 +488,17 @@ def _compute(self, arrays, dates, assets, mask):
asset=self._asset, start_date=dates[0], end_date=dates[-1],
)
return out


class StrictlyTrueFilter(CustomFilter):
"""
A Filter that requires its inputs to have
been True for the last `window_length` days.
**Default Inputs**: None
**Default Window Length**: None
"""

def compute(self, today, assets, out, arg):
out[:] = (sum(arg) == self.window_length)

0 comments on commit 3546b2c

Please sign in to comment.