diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index e6b458b6..3c7ee734 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -608,7 +608,7 @@ def handle_data(context, data): def test_order_in_init(self): """ Test that calling order in initialize - will return an error + will raise an error. """ with self.assertRaises(OrderDuringInitialize): test_algo = TradingAlgorithm( diff --git a/zipline/algorithm.py b/zipline/algorithm.py index a10645ee..f67b9d86 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -102,6 +102,10 @@ 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. @@ -202,10 +206,13 @@ class TradingAlgorithm(object): if 'data_frequency' in kwargs: self.data_frequency = kwargs.pop('data_frequency') - # an algorithm subclass needs to set initialized to True when - # it is fully initialized. + # Subclasses that override initialize should only worry about + # setting self.initialized = True if AUTO_INITIALIZE is + # is manually set to False. self.initialized = False self.initialize(*args, **kwargs) + if self.AUTO_INITIALIZE: + self.initialized = True def initialize(self, *args, **kwargs): """ @@ -532,7 +539,6 @@ class TradingAlgorithm(object): Raises an UnsupportedOrderParameters if invalid arguments are found. """ - # Make sure we're not in init before we order. if not self.initialized: raise OrderDuringInitialize( msg="order() can only be called from within handle_data()" diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index 31e20274..0cdf380a 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -640,7 +640,6 @@ class BatchTransformAlgorithm(TradingAlgorithm): self.iter = 0 self.set_slippage(FixedSlippage()) - self.initialized = True def handle_data(self, data): self.history_return_price_class.append(