mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-07 18:51:43 +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):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user