mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-16 11:18:11 +08:00
Merge pull request #1230 from quantopian/pipeline-example
DOC/TEST: Add example algo using Pipeline.
This commit is contained in:
+34
-20
@@ -31,6 +31,7 @@ from six import (
|
||||
iteritems,
|
||||
itervalues,
|
||||
string_types,
|
||||
viewkeys,
|
||||
)
|
||||
|
||||
from zipline._protocol import handle_non_market_minutes
|
||||
@@ -81,7 +82,7 @@ from zipline.assets import Asset, Future
|
||||
from zipline.assets.futures import FutureChain
|
||||
from zipline.gens.tradesimulation import AlgorithmSimulator
|
||||
from zipline.pipeline.engine import (
|
||||
NoOpPipelineEngine,
|
||||
ExplodingPipelineEngine,
|
||||
SimplePipelineEngine,
|
||||
)
|
||||
from zipline.utils.api_support import (
|
||||
@@ -332,29 +333,46 @@ class TradingAlgorithm(object):
|
||||
|
||||
self._handle_data = None
|
||||
|
||||
def noop(*args, **kwargs):
|
||||
pass
|
||||
|
||||
if self.algoscript is not None:
|
||||
api_methods = {
|
||||
'initialize',
|
||||
'handle_data',
|
||||
'before_trading_start',
|
||||
'analyze',
|
||||
}
|
||||
unexpected_api_methods = viewkeys(kwargs) & api_methods
|
||||
if unexpected_api_methods:
|
||||
raise ValueError(
|
||||
"TradingAlgorithm received a script and the following API"
|
||||
" methods as functions:\n{funcs}".format(
|
||||
funcs=unexpected_api_methods,
|
||||
)
|
||||
)
|
||||
|
||||
filename = kwargs.pop('algo_filename', None)
|
||||
if filename is None:
|
||||
filename = '<string>'
|
||||
code = compile(self.algoscript, filename, 'exec')
|
||||
exec_(code, self.namespace)
|
||||
self._initialize = self.namespace.get('initialize')
|
||||
if 'handle_data' in self.namespace:
|
||||
self._handle_data = self.namespace['handle_data']
|
||||
|
||||
self._before_trading_start = \
|
||||
self.namespace.get('before_trading_start')
|
||||
self._initialize = self.namespace.get('initialize', noop)
|
||||
self._handle_data = self.namespace.get('handle_data', noop)
|
||||
self._before_trading_start = self.namespace.get(
|
||||
'before_trading_start',
|
||||
)
|
||||
# Optional analyze function, gets called after run
|
||||
self._analyze = self.namespace.get('analyze')
|
||||
|
||||
elif kwargs.get('initialize') and kwargs.get('handle_data'):
|
||||
if self.algoscript is not None:
|
||||
raise ValueError('You can not set script and \
|
||||
initialize/handle_data.')
|
||||
self._initialize = kwargs.pop('initialize')
|
||||
self._handle_data = kwargs.pop('handle_data')
|
||||
self._before_trading_start = kwargs.pop('before_trading_start',
|
||||
None)
|
||||
else:
|
||||
self._initialize = kwargs.pop('initialize', noop)
|
||||
self._handle_data = kwargs.pop('handle_data', noop)
|
||||
self._before_trading_start = kwargs.pop(
|
||||
'before_trading_start',
|
||||
None,
|
||||
)
|
||||
self._analyze = kwargs.pop('analyze', None)
|
||||
|
||||
self.event_manager.add_event(
|
||||
@@ -367,10 +385,6 @@ class TradingAlgorithm(object):
|
||||
prepend=True,
|
||||
)
|
||||
|
||||
# If method not defined, NOOP
|
||||
if self._initialize is None:
|
||||
self._initialize = lambda x: None
|
||||
|
||||
# Alternative way of setting data_frequency for backwards
|
||||
# compatibility.
|
||||
if 'data_frequency' in kwargs:
|
||||
@@ -390,7 +404,7 @@ class TradingAlgorithm(object):
|
||||
"""
|
||||
Construct and store a PipelineEngine from loader.
|
||||
|
||||
If get_loader is None, constructs a NoOpPipelineEngine.
|
||||
If get_loader is None, constructs an ExplodingPipelineEngine
|
||||
"""
|
||||
if get_loader is not None:
|
||||
self.engine = SimplePipelineEngine(
|
||||
@@ -399,7 +413,7 @@ class TradingAlgorithm(object):
|
||||
self.asset_finder,
|
||||
)
|
||||
else:
|
||||
self.engine = NoOpPipelineEngine()
|
||||
self.engine = ExplodingPipelineEngine()
|
||||
|
||||
def initialize(self, *args, **kwargs):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user