ENH: Implemented AUTO_INITIALIZE feature.

Created a new flag in TradingAlgorithm that enables subclasses to
decide if they want to handle setting self.initialized = True.
Before it was the responsibility of an overriding subclass to set
initalized = True. This was causing problems because it's easy to
forget this. Now it is the responsibility of TradingAlgorithm
unless explicity stated otherwise.
This commit is contained in:
Delaney Granizo-Mackenzie
2014-07-08 14:03:53 -04:00
parent 3521a11ed4
commit 9b01d78f5d
3 changed files with 10 additions and 5 deletions
+1 -1
View File
@@ -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(
+9 -3
View File
@@ -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()"
-1
View File
@@ -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(