mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-02 19:03:42 +08:00
Merge pull request #1114 from quantopian/handle-data-optional
ENH: make handle_data optional
This commit is contained in:
@@ -106,8 +106,8 @@ from zipline.test_algorithms import (
|
||||
call_with_kwargs,
|
||||
call_without_kwargs,
|
||||
call_with_bad_kwargs_current,
|
||||
call_with_bad_kwargs_history
|
||||
)
|
||||
call_with_bad_kwargs_history,
|
||||
no_handle_data)
|
||||
from zipline.testing import (
|
||||
make_jagged_equity_info,
|
||||
to_utc,
|
||||
@@ -1494,6 +1494,10 @@ class TestAlgoScript(TestCase):
|
||||
algo = TradingAlgorithm(script=noop_algo)
|
||||
algo.run(self.data_portal)
|
||||
|
||||
def test_no_handle_data(self):
|
||||
algo = TradingAlgorithm(script=no_handle_data)
|
||||
algo.run(self.data_portal)
|
||||
|
||||
def test_api_calls(self):
|
||||
algo = TradingAlgorithm(initialize=initialize_api,
|
||||
handle_data=handle_data_api,
|
||||
|
||||
@@ -318,6 +318,8 @@ class TradingAlgorithm(object):
|
||||
create_context=kwargs.pop('create_event_context', None),
|
||||
)
|
||||
|
||||
self._handle_data = None
|
||||
|
||||
if self.algoscript is not None:
|
||||
filename = kwargs.pop('algo_filename', None)
|
||||
if filename is None:
|
||||
@@ -325,9 +327,7 @@ class TradingAlgorithm(object):
|
||||
code = compile(self.algoscript, filename, 'exec')
|
||||
exec_(code, self.namespace)
|
||||
self._initialize = self.namespace.get('initialize')
|
||||
if 'handle_data' not in self.namespace:
|
||||
raise ValueError('You must define a handle_data function.')
|
||||
else:
|
||||
if 'handle_data' in self.namespace:
|
||||
self._handle_data = self.namespace['handle_data']
|
||||
|
||||
self._before_trading_start = \
|
||||
@@ -407,7 +407,8 @@ class TradingAlgorithm(object):
|
||||
self._in_before_trading_start = False
|
||||
|
||||
def handle_data(self, data):
|
||||
self._handle_data(self, data)
|
||||
if self._handle_data:
|
||||
self._handle_data(self, data)
|
||||
|
||||
# Unlike trading controls which remain constant unless placing an
|
||||
# order, account controls can change each bar. Thus, must check
|
||||
|
||||
@@ -784,6 +784,11 @@ def handle_data(context, data):
|
||||
pass
|
||||
"""
|
||||
|
||||
no_handle_data = """
|
||||
def initialize(context):
|
||||
pass
|
||||
"""
|
||||
|
||||
api_algo = """
|
||||
from zipline.api import (order,
|
||||
set_slippage,
|
||||
|
||||
Reference in New Issue
Block a user