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


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 gives these errors. If schedule_function is commented out, the error will be with attach_pipeline()

Appreciate your help.


''' 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 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],
sma_100 = SimpleMovingAverage(inputs=[USEquityPricing.close],

# 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):
pipeline_results = pipeline_output(‘my_pipeline’)
except NoFurtherDataError:
# If the the data is not available then return
except EmptyAssetsInTimeFrame:
# If asset is empty


$ pylivetrader run
[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/”, line 764, in call
return self.main(*args, **kwargs)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/”, line 717, in main
rv = self.invoke(ctx)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/”, line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/”, line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/”, line 555, in invoke
return callback(*args, **kwargs)
File “/opt/python/anaconda3/lib/python3.6/site-packages/click/”, 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/”, line 161, in run
File “/opt/python/anaconda3/lib/python3.6/site-packages/pylivetrader-0.1.1-py3.6.egg/pylivetrader/”, line 264, in run
File “/opt/python/anaconda3/lib/python3.6/site-packages/pylivetrader-0.1.1-py3.6.egg/pylivetrader/”, line 224, in initialize
self._initialize(self, *args, **kwargs)
File “”, 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/”, line 54, in wrapped
RuntimeError: zipline api method attach_pipeline must be called during a simulation.