mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-09 11:19:23 +08:00
BUG: HistoryContainer creation at runtime did not work as intended.
This commit is contained in:
+46
-8
@@ -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):
|
||||
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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_
|
||||
|
||||
Reference in New Issue
Block a user