mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-28 13:52:31 +08:00
ef4f642e62
This patch lays the groundwork for a compute engine designed to facilitate construction of factor-based universe screening and portfolio allocation. It contains: A new module, `zipline.modelling`, containing entities that can be used to express computations as dependency graphs. Each node in such a graph is an instance of the base `Term` class, defined in `zipline.modelling.term`. Dependency graphs are executed by instances of `FFCEngine`, defined in `zipline.modelling.engine`. A new module, `zipline.data.ffc`, containing loaders and dataset definitions for inputs to the modelling API. New `TradingAlgorithm` api methods: `add_factor`, and `add_filter`. These methods can only be called from `initialize`, and are used to inform the algorithm that each day it should compute the given terms. Computed factor results are made available through a new attribute of the `data` object in `before_trading_start` and `handle_data`. Computed filter results control which assets are available in the factor matrix on each day.
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
"""
|
|
Control flow utilities.
|
|
"""
|
|
from warnings import (
|
|
catch_warnings,
|
|
filterwarnings,
|
|
)
|
|
|
|
|
|
class nullctx(object):
|
|
"""
|
|
Null context manager. Useful for conditionally adding a contextmanager in
|
|
a single line, e.g.:
|
|
|
|
with SomeContextManager() if some_expr else nullctx():
|
|
do_stuff()
|
|
"""
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(*args):
|
|
return False
|
|
|
|
|
|
class WarningContext(object):
|
|
"""
|
|
Re-entrant contextmanager for contextually managing warnings.
|
|
"""
|
|
def __init__(self, *warning_specs):
|
|
self._warning_specs = warning_specs
|
|
self._catchers = []
|
|
|
|
def __enter__(self):
|
|
catcher = catch_warnings()
|
|
catcher.__enter__()
|
|
self._catchers.append(catcher)
|
|
for args, kwargs in self._warning_specs:
|
|
filterwarnings(*args, **kwargs)
|
|
return catcher
|
|
|
|
def __exit__(self, *exc_info):
|
|
catcher = self._catchers.pop()
|
|
return catcher.__exit__(*exc_info)
|
|
|
|
|
|
def ignore_nanwarnings():
|
|
"""
|
|
Helper for building a WarningContext that ignores warnings from numpy's
|
|
nanfunctions.
|
|
"""
|
|
return WarningContext(
|
|
(
|
|
('ignore',),
|
|
{'category': RuntimeWarning, 'module': 'numpy.lib.nanfunctions'},
|
|
)
|
|
)
|