class TestFactor(CustomFactor): inputs = [InputDates(), USEquityPricing.close] window_length = 10 dtype = datetime64ns_dtype def compute(self, today, assets, out, dates, closes): first, last = dates[[0, -1], 0] assert last == today.asm8 assert len(dates) == len(closes) == self.window_length out[:] = first
def __init__(self, list_symbols, calendar=None, populate_initial_workspace=None): self._list_symbols = list_symbols if calendar is None: calendar = get_calendar('NYSE').all_sessions self._calendar = calendar self._root_mask_term = AssetExists() self._root_mask_dates_term = InputDates() self._populate_initial_workspace = (populate_initial_workspace or default_populate_initial_workspace)
def run_graph(self, graph, initial_workspace, mask=None): """ Compute the given TermGraph, seeding the workspace of our engine with `initial_workspace`. Parameters ---------- graph : zipline.pipeline.graph.ExecutionPlan Graph to run. initial_workspace : dict Initial workspace to forward to SimplePipelineEngine.compute_chunk. mask : DataFrame, optional This is a value to pass to `initial_workspace` as the mask from `AssetExists()`. Defaults to a frame of shape `self.default_shape` containing all True values. Returns ------- results : dict Mapping from termname -> computed result. """ def get_loader(c): raise AssertionError("run_graph() should not require any loaders!") engine = SimplePipelineEngine( get_loader, self.asset_finder, default_domain=US_EQUITIES, ) if mask is None: mask = self.default_asset_exists_mask dates, sids, mask_values = explode(mask) initial_workspace.setdefault(AssetExists(), mask_values) initial_workspace.setdefault(InputDates(), dates) refcounts = graph.initial_refcounts(initial_workspace) execution_order = graph.execution_order(initial_workspace, refcounts) return engine.compute_chunk( graph=graph, dates=dates, sids=sids, workspace=initial_workspace, execution_order=execution_order, refcounts=refcounts, hooks=NoHooks(), )
def run_graph(self, graph, initial_workspace, mask=None): """ Compute the given TermGraph, seeding the workspace of our engine with `initial_workspace`. Parameters ---------- graph : zipline.pipeline.graph.TermGraph Graph to run. initial_workspace : dict Initial workspace to forward to SimplePipelineEngine.compute_chunk. mask : DataFrame, optional This is a value to pass to `initial_workspace` as the mask from `AssetExists()`. Defaults to a frame of shape `self.default_shape` containing all True values. Returns ------- results : dict Mapping from termname -> computed result. """ engine = SimplePipelineEngine( lambda column: ExplodingObject(), self.nyse_sessions, self.asset_finder, ) if mask is None: mask = self.default_asset_exists_mask dates, assets, mask_values = explode(mask) initial_workspace.setdefault(AssetExists(), mask_values) initial_workspace.setdefault(InputDates(), dates) return engine.compute_chunk( graph, dates, assets, initial_workspace, )