BUG: HistoryContainer creation at runtime did not work as intended.

This commit is contained in:
Joe Jevnik
2014-11-05 18:31:11 -05:00
parent 9ddb033e67
commit 93429d69f5
5 changed files with 82 additions and 31 deletions
+46 -8
View File
@@ -714,6 +714,18 @@ def handle_data(context, data):
class TestHistory(TestCase):
@classmethod
def setUpClass(cls):
cls._start = pd.Timestamp('1991-01-01', tz='UTC')
cls._end = pd.Timestamp('1991-01-15', tz='UTC')
cls.sim_params = factory.create_simulation_parameters(
data_frequency='minute',
)
@property
def source(self):
return RandomWalkSource(start=self._start, end=self._end)
def test_history(self):
history_algo = """
from zipline.api import history, add_history
@@ -724,16 +736,42 @@ def initialize(context):
def handle_data(context, data):
df = history(10, '1d', 'price')
"""
start = pd.Timestamp('1991-01-01', tz='UTC')
end = pd.Timestamp('1991-01-15', tz='UTC')
source = RandomWalkSource(start=start,
end=end)
sim_params = factory.create_simulation_parameters(
data_frequency='minute')
algo = TradingAlgorithm(script=history_algo, sim_params=sim_params)
output = algo.run(source)
algo = TradingAlgorithm(
script=history_algo,
sim_params=self.sim_params,
)
output = algo.run(self.source)
self.assertIsNot(output, None)
def test_history_without_add(self):
def handle_data(algo, data):
algo.history(1, '1m', 'price')
algo = TradingAlgorithm(
initialize=lambda _: None,
handle_data=handle_data,
sim_params=self.sim_params,
)
algo.run(self.source)
self.assertIsNotNone(algo.history_container)
self.assertEqual(algo.history_container.buffer_panel.window_length, 1)
def test_add_history_in_handle_data(self):
def handle_data(algo, data):
algo.add_history(1, '1m', 'price')
algo = TradingAlgorithm(
initialize=lambda _: None,
handle_data=handle_data,
sim_params=self.sim_params,
)
algo.run(self.source)
self.assertIsNotNone(algo.history_container)
self.assertEqual(algo.history_container.buffer_panel.window_length, 1)
class TestGetDatetime(TestCase):
+3 -3
View File
@@ -997,7 +997,7 @@ class TestHistoryContainerResize(TestCase):
)
for spec in to_add:
container.ensure_spec(spec, initial_dt)
container.ensure_spec(spec, initial_dt, bar_data)
self.assertEqual(
container.digest_panels[spec.frequency].window_length,
@@ -1052,7 +1052,7 @@ class TestHistoryContainerResize(TestCase):
data_frequency=data_frequency,
)
container.ensure_spec(new_spec, initial_dt)
container.ensure_spec(new_spec, initial_dt, bar_data)
if bar_count > 1:
digest_panel = container.digest_panels[new_spec.frequency]
@@ -1109,7 +1109,7 @@ class TestHistoryContainerResize(TestCase):
data_frequency=data_frequency,
)
container.ensure_spec(new_spec, initial_dt)
container.ensure_spec(new_spec, initial_dt, bar_data)
if bar_count > 1:
digest_panel = container.digest_panels[new_spec.frequency]
+2 -1
View File
@@ -15,6 +15,7 @@
import datetime
import random
from itertools import islice
from six import iteritems
from six.moves import range, map
from nose_parameterized import parameterized
from unittest import TestCase
@@ -216,7 +217,7 @@ class RuleTestCase(TestCase):
return # This is the base class testing, it is always complete.
dem = {
k for k, v in vars(zipline.utils.events).iteritems()
k for k, v in iteritems(vars(zipline.utils.events))
if isinstance(v, type)
and issubclass(v, self.class_)
and v is not self.class_