RuntimeError: zipline api method schedule_function must be called during a simulation

Hi,

Please suggest what is wrong in this program ? The calls schedule_function() and attach_pipeline() are not being allowed and exceptions are are occuring. How do I get to call these functions ? Are there any place these constraints are documented ?

pylivetrader run smapipeline.py gives these errors. If schedule_function is commented out, the error will be with attach_pipeline()

Appreciate your help.

Thanks,
T.Fg

''' smapipeline.py This is a sample moving average crossover strategy using pipeline Dataset: All '''

from zipline.api import attach_pipeline, pipeline_output
from zipline.pipeline import Pipeline
from zipline.pipeline.data import USEquityPricing
from zipline.pipeline.factors import SimpleMovingAverage

‘’’
A function to define things to do at the start of the strategy
‘’’
def initialize(context):
# Call strategy function on the first trading day of each week at 12 noon.

schedule_function(strategy, date_rules.week_start(), time_rules.market_open(hours=2, minutes=30))

# Attach the Pipeline created using make_pipeline() function

attach_pipeline(make_pipeline(), name='my_pipeline')

'''
A function to define pipeline
'''

def make_pipeline():
pipe = Pipeline()

# Construct Factors
# Calculate simple moving average using built-in factors

sma_20 = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                         window_length=20)
sma_100 = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                              window_length=50)

# Register outputs

pipe.add(sma_20, 'sma_20')
pipe.add(sma_100, 'sma_100')

return pipe

‘’’
A function to define strategy logic
‘’’
def strategy(context, data):
try:
pipeline_results = pipeline_output(‘my_pipeline’)
except NoFurtherDataError:
# If the the data is not available then return
return
except EmptyAssetsInTimeFrame:
# If asset is empty
return

Output:

$ pylivetrader run smapipeline.py
[2020-01-28 19:08:24.073875] INFO: Algorithm: livetrader start running with backend = alpaca data-frequency = minute
In make_pipeline
Traceback (most recent call last):
File “/opt/python/anaconda3/bin/pylivetrader”, line 11, in
load_entry_point(‘pylivetrader==0.1.1’, ‘console_scripts’, ‘pylivetrader’)()
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/core.py”, line 764, in call
return self.main(*args, **kwargs)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/core.py”, line 717, in main
rv = self.invoke(ctx)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/core.py”, line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/core.py”, line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/core.py”, line 555, in invoke
return callback(*args, **kwargs)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/decorators.py”, line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File “/opt/python/anaconda3/lib/python3.6/site-packages/pylivetrader-0.1.1-py3.6.egg/pylivetrader/main.py”, line 161, in run
algorithm.run(retry=ctx.retry)
File “/opt/python/anaconda3/lib/python3.6/site-packages/pylivetrader-0.1.1-py3.6.egg/pylivetrader/algorithm.py”, line 264, in run
self.initialize()
File “/opt/python/anaconda3/lib/python3.6/site-packages/pylivetrader-0.1.1-py3.6.egg/pylivetrader/algorithm.py”, line 224, in initialize
self._initialize(self, *args, **kwargs)
File “smapipeline.py”, line 22, in initialize
attach_pipeline(make_pipeline(), name=‘my_pipeline’)
File “/opt/python/anaconda3/lib/python3.6/site-packages/zipline-1.3.0-py3.6-linux-x86_64.egg/zipline/utils/api_support.py”, line 54, in wrapped
% f.name
RuntimeError: zipline api method attach_pipeline must be called during a simulation.