mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-18 12:20:12 +08:00
Merge pull request #796 from quantopian/prevent-history-in-initialize
ENH: Fail when history() is called in initialize.
This commit is contained in:
+36
-1
@@ -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,),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -191,6 +191,13 @@ at frequency '{data_frequency}'.
|
||||
""".strip()
|
||||
|
||||
|
||||
class HistoryInInitialize(ZiplineError):
|
||||
"""
|
||||
Raised when an algorithm calls history() in initialize.
|
||||
"""
|
||||
msg = "history() should only be called in handle_data()"
|
||||
|
||||
|
||||
class MultipleSymbolsFound(ZiplineError):
|
||||
"""
|
||||
Raised when a symbol() call contains a symbol that changed over
|
||||
|
||||
Reference in New Issue
Block a user