MAINT: One way to set sim_params and data_frequency.

There were sevaral places you could supply sim_params
in TradingAlgorithm (__init__, run). This got confusing
as its not clear who updated what and which one was the
correct one to use at each time.

Then there were to ways to define data_frequency, one in
__init__() and one in the sim_params which also added code
complexity.

This refactor makes it explicit that sim_params are to be
passed to __init__() only. Moreover, data_frequency is
only stored in sim_params. For backwards compatibility,
it can still be supplied separately but will link to
the one in sim_params.

For example, you could create new sim params via:

sim_params = create_simulation_parameters(data_frequency='minute')
algo = MyAlgo(sim_params)
algo.run(data)

In addition, perf_tracker only gets initialized in one place:
_create_generator() which should also make the various ways
of running an algorithm more deterministic.

This also fixes a bug with SimulationParameters where
you could not change the period_start. Unfortunately, the
current implementation still requieres an implicit call to
update the internal variables.
This commit is contained in:
Thomas Wiecki
2014-06-30 17:28:02 +02:00
parent 4c9cf1321d
commit 10885e1b77
9 changed files with 95 additions and 101 deletions
+4 -2
View File
@@ -27,6 +27,7 @@ from zipline.finance import trading
from zipline.algorithm import TradingAlgorithm
from zipline.finance import slippage
from zipline.utils import factory
from zipline.utils.factory import create_simulation_parameters
from zipline.utils.test_utils import (
setup_logger,
teardown_logger
@@ -222,7 +223,8 @@ class AlgorithmGeneratorTestCase(TestCase):
algo.datetime should be equal to the last benchmark time.
See https://github.com/quantopian/zipline/issues/241
"""
sim_params = factory.create_simulation_parameters(num_days=1)
algo = TestAlgo(self, sim_params=sim_params, data_frequency='minute')
sim_params = create_simulation_parameters(num_days=1,
data_frequency='minute')
algo = TestAlgo(self, sim_params=sim_params)
algo.run(source=[])
self.assertEqual(algo.datetime, sim_params.last_close)