ENH: Moves calling of TradingAlgorithm.initialize to _create_generator

This commit is contained in:
jfkirk
2015-06-11 10:14:07 -04:00
parent 3d18a2954f
commit 9da644f767
4 changed files with 20 additions and 19 deletions
+1
View File
@@ -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):
"""
+1 -1
View File
@@ -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,
+3 -1
View File
@@ -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 = """
+15 -17
View File
@@ -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