diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 67c7c61b..5adcdd85 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -733,6 +733,7 @@ def handle_data(context, data): sim_params=self.sim_params, ) set_algo_instance(test_algo) + test_algo.run(self.source) def test_portfolio_in_init(self): """ diff --git a/tests/test_batchtransform.py b/tests/test_batchtransform.py index c0f6f6d5..62c2fb54 100644 --- a/tests/test_batchtransform.py +++ b/tests/test_batchtransform.py @@ -244,10 +244,10 @@ class TestBatchTransform(TestCase): def test_passing_of_args(self): algo = BatchTransformAlgorithm(1, kwarg='str', sim_params=self.sim_params) + algo.run(self.source) self.assertEqual(algo.args, (1,)) self.assertEqual(algo.kwargs, {'kwarg': 'str'}) - algo.run(self.source) expected_item = ((1, ), {'kwarg': 'str'}) self.assertEqual( algo.history_return_args, diff --git a/tests/test_history.py b/tests/test_history.py index ecc2781d..ef2191b5 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -472,11 +472,13 @@ def handle_data(context, data): start=start, end=end) with self.assertRaises(IncompatibleHistoryFrequency): - TradingAlgorithm( + algo = TradingAlgorithm( script=algo_text, data_frequency='daily', sim_params=sim_params ) + source = RandomWalkSource(start=start, end=end) + algo.run(source) def test_basic_history(self): algo_text = """ diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 27b5678b..023f1c7b 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -113,10 +113,6 @@ class TradingAlgorithm(object): """ - # If this is set to false then it is the responsibility - # of the overriding subclass to set initialized = true - AUTO_INITIALIZE = True - def __init__(self, *args, **kwargs): """Initialize sids and other state variables. @@ -248,13 +244,10 @@ class TradingAlgorithm(object): self._most_recent_data = None - # Subclasses that override initialize should only worry about - # setting self.initialized = True if AUTO_INITIALIZE is - # is manually set to False. + # Prepare the algo for initialization self.initialized = False - self.initialize(*args, **kwargs) - if self.AUTO_INITIALIZE: - self.initialized = True + self.initialize_args = args + self.initialize_kwargs = kwargs def initialize(self, *args, **kwargs): """ @@ -371,6 +364,11 @@ class TradingAlgorithm(object): processed by the zipline, and False for those that should be skipped. """ + + if not self.initialized: + self.initialize(*self.initialize_args, **self.initialize_kwargs) + self.initialized = True + if self.perf_tracker is None: # HACK: When running with the `run` method, we set perf_tracker to # None so that it will be overwritten here. @@ -451,6 +449,13 @@ class TradingAlgorithm(object): # of first_open and last_close. self.sim_params._update_internal() + # force a reset of the performance tracker, in case + # this is a repeat run of the algorithm. + self.perf_tracker = None + + # create zipline + self.gen = self._create_generator(self.sim_params) + # Create history containers if self.history_specs: self.history_container = self.history_container_class( @@ -460,13 +465,6 @@ class TradingAlgorithm(object): self.sim_params.data_frequency, ) - # force a reset of the performance tracker, in case - # this is a repeat run of the algorithm. - self.perf_tracker = None - - # create zipline - self.gen = self._create_generator(self.sim_params) - with ZiplineAPI(self): # loop through simulated_trading, each iteration returns a # perf dictionary