From 48267874757ec2e1bc9b71385db82cc195021265 Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 26 Oct 2015 12:04:31 -0400 Subject: [PATCH 1/2] ENH: Fail when history() is called in initialize. --- tests/test_history.py | 37 ++++++++++++++++++++++++++++++++++++- zipline/algorithm.py | 2 ++ zipline/errors.py | 7 +++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/test_history.py b/tests/test_history.py index f0aae3f3..ae9c9b40 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -34,7 +34,7 @@ from zipline.finance.trading import ( SimulationParameters, TradingEnvironment, ) -from zipline.errors import IncompatibleHistoryFrequency +from zipline.errors import HistoryInInitialize, IncompatibleHistoryFrequency from zipline.sources import RandomWalkSource, DataFrameSource @@ -1329,6 +1329,41 @@ def handle_data(context, data): ' HistorySpec', ) + def test_history_in_initialize(self): + algo_text = dedent( + """\ + from zipline.api import history + + def initialize(context): + history(10, '1d', 'price') + + def handle_data(context, data): + pass + """ + ) + + start = pd.Timestamp('2007-04-05', tz='UTC') + end = pd.Timestamp('2007-04-10', tz='UTC') + + sim_params = SimulationParameters( + period_start=start, + period_end=end, + capital_base=float("1.0e5"), + data_frequency='minute', + emission_rate='daily', + env=self.env, + ) + + test_algo = TradingAlgorithm( + script=algo_text, + data_frequency='minute', + sim_params=sim_params, + env=self.env, + ) + + with self.assertRaises(HistoryInInitialize): + test_algo.initialize() + @parameterized.expand([ (1,), (2,), diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 9c43d4df..1ebc80fd 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -35,6 +35,7 @@ from operator import attrgetter from zipline.errors import ( AttachPipelineAfterInitialize, + HistoryInInitialize, NoSuchPipeline, OrderDuringInitialize, OverrideCommissionPostInit, @@ -1250,6 +1251,7 @@ class TradingAlgorithm(object): return self.history_specs[spec_key] @api_method + @require_initialized(HistoryInInitialize()) def history(self, bar_count, frequency, field, ffill=True): history_spec = self.get_history_spec( bar_count, diff --git a/zipline/errors.py b/zipline/errors.py index 484dba40..a8047ffe 100644 --- a/zipline/errors.py +++ b/zipline/errors.py @@ -191,6 +191,13 @@ at frequency '{data_frequency}'. """.strip() +class HistoryInInitialize(ZiplineError): + """ + Raised when an algorithm calls history() in initialize. + """ + msg = "history() can only be called in handle_data()" + + class MultipleSymbolsFound(ZiplineError): """ Raised when a symbol() call contains a symbol that changed over From 114c3d3bdf90e0edf2f722e79e15cf310f2ac32a Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Tue, 27 Oct 2015 14:38:26 -0400 Subject: [PATCH 2/2] DOC: Weaker modality for history in initialize. `history` works, with some caveats, in before_trading_start as well. --- zipline/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zipline/errors.py b/zipline/errors.py index a8047ffe..1cab758c 100644 --- a/zipline/errors.py +++ b/zipline/errors.py @@ -195,7 +195,7 @@ class HistoryInInitialize(ZiplineError): """ Raised when an algorithm calls history() in initialize. """ - msg = "history() can only be called in handle_data()" + msg = "history() should only be called in handle_data()" class MultipleSymbolsFound(ZiplineError):