Skip to content

Commit

Permalink
ENH: Adds StaticSids pipeline filter
Browse files Browse the repository at this point in the history
Useful for avoiding the need to create Asset objects when sids are
easier to use.

This is based off the existing implementation of StaticAssets, and
StaticAssets is now implemented as a wrapper around StaticSids.
  • Loading branch information
Andrew Daniels committed Mar 22, 2017
1 parent 5fd20e1 commit 7ff7c93
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
57 changes: 41 additions & 16 deletions tests/pipeline/test_filter.py
Expand Up @@ -30,7 +30,13 @@
from zipline.pipeline import Filter, Factor, Pipeline
from zipline.pipeline.classifiers import Classifier
from zipline.pipeline.factors import CustomFactor
from zipline.pipeline.filters import All, Any, AtLeastN, StaticAssets
from zipline.pipeline.filters import (
All,
Any,
AtLeastN,
StaticAssets,
StaticSids,
)
from zipline.testing import parameter_space, permute_rows, ZiplineTestCase
from zipline.testing.fixtures import WithSeededRandomPipelineEngine
from zipline.testing.predicates import assert_equal
Expand Down Expand Up @@ -825,29 +831,28 @@ def test_top_and_bottom_with_groupby_and_mask(self, dtype, seed):
)


class SpecificAssetsTestCase(WithSeededRandomPipelineEngine,
ZiplineTestCase):
class SidFactor(CustomFactor):
"""A factor that just returns each asset's sid."""
inputs = ()
window_length = 1

ASSET_FINDER_EQUITY_SIDS = tuple(range(10))
def compute(self, today, sids, out):
out[:] = sids

def test_specific_assets(self):
assets = self.asset_finder.retrieve_all(self.ASSET_FINDER_EQUITY_SIDS)

class SidFactor(CustomFactor):
"""A factor that just returns each asset's sid."""
inputs = ()
window_length = 1
class SpecificAssetsTestCase(WithSeededRandomPipelineEngine,
ZiplineTestCase):

def compute(self, today, sids, out):
out[:] = sids
ASSET_FINDER_EQUITY_SIDS = tuple(range(10))

def _check_filters(self, evens, odds, first_five, last_three):
pipe = Pipeline(
columns={
'sid': SidFactor(),
'evens': StaticAssets(assets[::2]),
'odds': StaticAssets(assets[1::2]),
'first_five': StaticAssets(assets[:5]),
'last_three': StaticAssets(assets[-3:]),
'evens': evens,
'odds': odds,
'first_five': first_five,
'last_three': last_three,
},
)

Expand All @@ -861,6 +866,26 @@ def compute(self, today, sids, out):
assert_equal(results.first_five, sids < 5)
assert_equal(results.last_three, sids >= 7)

def test_specific_assets(self):
assets = self.asset_finder.retrieve_all(self.ASSET_FINDER_EQUITY_SIDS)

self._check_filters(
evens=StaticAssets(assets[::2]),
odds=StaticAssets(assets[1::2]),
first_five=StaticAssets(assets[:5]),
last_three=StaticAssets(assets[-3:]),
)

def test_specific_sids(self):
sids = self.ASSET_FINDER_EQUITY_SIDS

self._check_filters(
evens=StaticSids(sids[::2]),
odds=StaticSids(sids[1::2]),
first_five=StaticSids(sids[:5]),
last_three=StaticSids(sids[-3:]),
)


class TestPostProcessAndToWorkSpaceValue(ZiplineTestCase):
def test_reversability(self):
Expand Down
2 changes: 2 additions & 0 deletions zipline/pipeline/filters/__init__.py
Expand Up @@ -9,6 +9,7 @@
PercentileFilter,
SingleAsset,
StaticAssets,
StaticSids,
)
from .smoothing import All, Any, AtLeastN

Expand All @@ -26,4 +27,5 @@
'PercentileFilter',
'SingleAsset',
'StaticAssets',
'StaticSids',
]
36 changes: 27 additions & 9 deletions zipline/pipeline/filters/filter.py
Expand Up @@ -502,27 +502,45 @@ def _compute(self, arrays, dates, assets, mask):
return out


class StaticAssets(Filter):
class StaticSids(Filter):
"""
A Filter that computes True for a specific set of predetermined assets.
A Filter that computes True for a specific set of predetermined sids.
``StaticAssets`` is mostly useful for debugging or for interactively
computing pipeline terms for a fixed set of assets that are known ahead of
``StaticSids`` is mostly useful for debugging or for interactively
computing pipeline terms for a fixed set of sids that are known ahead of
time.
Parameters
----------
assets : iterable[Asset]
An iterable of assets for which to filter.
sids : iterable[int]
An iterable of sids for which to filter.
"""
inputs = ()
window_length = 0
params = ('sids',)

def __new__(cls, assets):
sids = frozenset(asset.sid for asset in assets)
return super(StaticAssets, cls).__new__(cls, sids=sids)
def __new__(cls, sids):
sids = frozenset(sids)
return super(StaticSids, cls).__new__(cls, sids=sids)

def _compute(self, arrays, dates, sids, mask):
my_columns = sids.isin(self.params['sids'])
return repeat_first_axis(my_columns, len(mask)) & mask


class StaticAssets(StaticSids):
"""
A Filter that computes True for a specific set of predetermined assets.
``StaticAssets`` is mostly useful for debugging or for interactively
computing pipeline terms for a fixed set of assets that are known ahead of
time.
Parameters
----------
assets : iterable[Asset]
An iterable of assets for which to filter.
"""
def __new__(cls, assets):
sids = frozenset(asset.sid for asset in assets)
return super(StaticAssets, cls).__new__(cls, sids)

0 comments on commit 7ff7c93

Please sign in to comment.