def run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, benchmark_file, benchmark_symbol, benchmark_sid, no_benchmark, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter): """Run a backtest for the given algorithm. """ # check that the start and end dates are passed correctly if start is None and end is None: # check both at the same time to avoid the case where a user # does not pass either of these and then passes the first only # to be told they need to pass the second argument also ctx.fail( "must specify dates with '-s' / '--start' and '-e' / '--end'", ) if start is None: ctx.fail("must specify a start date with '-s' / '--start'") if end is None: ctx.fail("must specify an end date with '-e' / '--end'") if (algotext is not None) == (algofile is not None): ctx.fail( "must specify exactly one of '-f' / '--algofile' or" " '-t' / '--algotext'", ) trading_calendar = get_calendar(trading_calendar) benchmark_spec = BenchmarkSpec.from_cli_params( no_benchmark=no_benchmark, benchmark_sid=benchmark_sid, benchmark_symbol=benchmark_symbol, benchmark_file=benchmark_file, ) return _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, trading_calendar=trading_calendar, print_algo=print_algo, metrics_set=metrics_set, local_namespace=local_namespace, environ=os.environ, blotter=blotter, benchmark_spec=benchmark_spec, )
def run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter): """Run a backtest for the given algorithm. """ # check that the start and end dates are passed correctly if start is None and end is None: # check both at the same time to avoid the case where a user # does not pass either of these and then passes the first only # to be told they need to pass the second argument also ctx.fail( "must specify dates with '-s' / '--start' and '-e' / '--end'", ) if start is None: ctx.fail("must specify a start date with '-s' / '--start'") if end is None: ctx.fail("must specify an end date with '-e' / '--end'") if (algotext is not None) == (algofile is not None): ctx.fail( "must specify exactly one of '-f' / '--algofile' or" " '-t' / '--algotext'", ) trading_calendar = get_calendar(trading_calendar) perf = _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, data=None, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, trading_calendar=trading_calendar, print_algo=print_algo, metrics_set=metrics_set, local_namespace=local_namespace, environ=os.environ, blotter=blotter, ) if output == '-': click.echo(str(perf)) elif output != os.devnull: # make the zipline magic not write any data perf.to_pickle(output) return perf
def main(job_id, D): print "job_id", job_id, " params:", D equities1 = {} register( 'my-db-bundle', # name this whatever you like viadb(equities1), calendar='SHSZ') parsed = {} parsed['initialize'] = None parsed['handle_data'] = None parsed['before_trading_start'] = None parsed['analyze'] = None parsed['algotext'] = None parsed['defines'] = () parsed['capital_base'] = 1000000 parsed['data'] = None parsed['bundle'] = 'my-db-bundle' #parsed['bundle']='YAHOO' #parsed['bundle_timestamp']=None parsed['bundle_timestamp'] = pd.Timestamp.utcnow() parsed['start'] = Timestamp('2017-03-01 13:30:00+0000', tz='UTC') parsed['end'] = Timestamp('2017-06-01 13:30:00+0000', tz='UTC') parsed['algofile'] = open( '/data/kanghua/workshop/strategy/campaign/hyperparam/example-new/zipline_strategy.py' ) parsed['data_frequency'] = 'daily' parsed['print_algo'] = False parsed['output'] = 'os.devnull' parsed['local_namespace'] = None parsed['environ'] = os.environ parsed['bm_symbol'] = None # Below what we expect spearmint to pass us # parsed['algo_params']=[47,88.7,7.7] # D={} # D['timeperiod']=10 # D['nbdevup']=1.00 # D['nbdevdn']=1.00 parsed['algo_params'] = D perf = _run(**parsed) StartV = perf['portfolio_value'][0] EndV = perf['portfolio_value'][-1] # spearmint wants to minimize so return negative profit OPTIM = (StartV - EndV) return OPTIM
def run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter): """Run a backtest for the given algorithm. """ # check that the start and end dates are passed correctly if start is None and end is None: # check both at the same time to avoid the case where a user # does not pass either of these and then passes the first only # to be told they need to pass the second argument also ctx.fail( "must specify dates with '-s' / '--start' and '-e' / '--end'", ) if start is None: ctx.fail("must specify a start date with '-s' / '--start'") if end is None: ctx.fail("must specify an end date with '-e' / '--end'") if (algotext is not None) == (algofile is not None): ctx.fail( "must specify exactly one of '-f' / '--algofile' or" " '-t' / '--algotext'", ) trading_calendar = get_calendar(trading_calendar) perf = _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, trading_calendar=trading_calendar, print_algo=print_algo, metrics_set=metrics_set, local_namespace=local_namespace, environ=os.environ, blotter=blotter, benchmark_returns=None, ) if output == '-': click.echo(str(perf)) elif output != os.devnull: # make the zipline magic not write any data perf.to_pickle(output) return perf
def run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter, broker, broker_uri, state_file, realtime_bar_target, list_brokers): """Run a backtest for the given algorithm. """ if list_brokers: click.echo("Supported brokers:") for _, name, _ in pkgutil.iter_modules(brokers.__path__): if name != 'broker': click.echo(name) return # check that the start and end dates are passed correctly if not broker and start is None and end is None: # check both at the same time to avoid the case where a user # does not pass either of these and then passes the first only # to be told they need to pass the second argument also ctx.fail( "must specify dates with '-s' / '--start' and '-e' / '--end'", ) if not broker and start is None: ctx.fail("must specify a start date with '-s' / '--start'") if not broker and end is None: ctx.fail("must specify an end date with '-e' / '--end'") if broker and broker_uri is None: ctx.fail("must specify broker-uri if broker is specified") if broker and state_file is None: ctx.fail("must specify state-file with live trading") if broker and realtime_bar_target is None: ctx.fail("must specify realtime-bar-target with live trading") brokerobj = None if broker: mod_name = 'zipline.gens.brokers.%s_broker' % broker.lower() try: bmod = import_module(mod_name) except ImportError: ctx.fail("unsupported broker: can't import module %s" % mod_name) cl_name = '%sBroker' % broker.upper() try: bclass = getattr(bmod, cl_name) except AttributeError: ctx.fail("unsupported broker: can't import class %s from %s" % (cl_name, mod_name)) brokerobj = bclass(broker_uri) if end is None: end = pd.Timestamp.utcnow() + pd.Timedelta(days=1) if (algotext is not None) == (algofile is not None): ctx.fail( "must specify exactly one of '-f' / '--algofile' or" " '-t' / '--algotext'", ) trading_calendar = get_calendar(trading_calendar) perf = _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, teardown=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, trading_calendar=trading_calendar, print_algo=print_algo, metrics_set=metrics_set, local_namespace=local_namespace, environ=os.environ, blotter=blotter, benchmark_returns=None, broker=brokerobj, state_filename=state_file, realtime_bar_target=realtime_bar_target, performance_callback=None, stop_execution_callback=None, execution_id=None ) if output == '-': click.echo(str(perf)) elif output != os.devnull: # make the zipline magic not write any data perf.to_pickle(output) return perf
def run(self): spy_init_code = """ import inspect stacks = inspect.stack()[1:] __channel = None print(stacks) while stacks: stacks, stack = stacks[1:], stacks[0] frame, filename, lineno, func, code_ctx, index = stack if '__channel__' in frame.f_locals: __channel = frame.f_locals['__channel__'] break if __channel is None: raise Exception('Cannot find any stack that has __channel') context.__channel = __channel """ spy_action_code = """ context.__channel.give(context.portfolio) """ code = self.code.split('\n') try: i = list( filter(lambda x: 'def initialize' in x[1], enumerate(code)))[0][0] code.insert(i + 1, spy_init_code) j = list( filter(lambda x: 'def handle_data' in x[1], enumerate(code)))[0][0] code.insert(j + 1, spy_action_code) algotext = '\n'.join(code) except Exception as e: print('Failed to find <initialize> or <handle_data> in code') print(e) return __channel__ = Channel( consumer=self.consume_portfolio or self.dummy_consume) print(algotext) algotext = algotext algofile = None define = () data_frequency = 'daily' capital_base = 1000000 bundle = 'quandl' bundle_timestamp = pd.Timestamp.utcnow() start = self.start_date end = self.end_date output = '-' trading_calendar = get_calendar('XNYS') print_algo = False metrics_set = 'default' local_namespace = {} blotter = 'default' _ = _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, data=None, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, trading_calendar=trading_calendar, print_algo=print_algo, metrics_set=metrics_set, local_namespace=local_namespace, environ=os.environ, blotter=blotter, )
def run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, print_algo, local_namespace, broker, broker_uri, state_file): """Run a backtest for the given algorithm. """ # check that the start and end dates are passed correctly if not broker and start is None and end is None: # check both at the same time to avoid the case where a user # does not pass either of these and then passes the first only # to be told they need to pass the second argument also ctx.fail( "must specify dates with '-s' / '--start' and '-e' / '--end'", ) if not broker and start is None: ctx.fail("must specify a start date with '-s' / '--start'") if not broker and end is None: ctx.fail("must specify an end date with '-e' / '--end'") if broker and broker_uri is None: ctx.fail("must specify broker-uri if broker is specified") if broker and state_file is None: ctx.fail("must specify state-file with live trading") brokerobj = None if broker: mod_name = 'zipline.gens.brokers.%s_broker' % broker.lower() try: bmod = import_module(mod_name) except ImportError: ctx.fail("unsupported broker: can't import module %s" % mod_name) cl_name = '%sBroker' % broker.upper() try: bclass = getattr(bmod, cl_name) except AttributeError: ctx.fail("unsupported broker: can't import class %s from %s" % (cl_name, mod_name)) brokerobj = bclass(broker_uri) if (algotext is not None) == (algofile is not None): ctx.fail( "must specify exactly one of '-f' / '--algofile' or" " '-t' / '--algotext'", ) perf = _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, data=None, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, print_algo=print_algo, local_namespace=local_namespace, environ=os.environ, broker=brokerobj, state_filename=state_file, ) if output == '-': click.echo(str(perf)) elif output != os.devnull: # make the zipline magic not write any data perf.to_pickle(output) return perf
def run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, print_algo, local_namespace, broker, broker_uri, state_file, realtime_bar_target, list_brokers, reader): """Run a backtest for the given algorithm. """ if list_brokers: click.echo("Supported brokers:") for _, name, _ in pkgutil.iter_modules(brokers.__path__): if name != 'broker': click.echo(name) return # check that the start and end dates are passed correctly if not broker and start is None and end is None: # check both at the same time to avoid the case where a user # does not pass either of these and then passes the first only # to be told they need to pass the second argument also ctx.fail( "must specify dates with '-s' / '--start' and '-e' / '--end'", ) if not broker and start is None: ctx.fail("must specify a start date with '-s' / '--start'") if not broker and end is None: ctx.fail("must specify an end date with '-e' / '--end'") if broker and broker_uri is None: ctx.fail("must specify broker-uri if broker is specified") if broker and state_file is None: ctx.fail("must specify state-file with live trading") if broker and realtime_bar_target is None: ctx.fail("must specify realtime-bar-target with live trading") brokerobj = None if broker: mod_name = 'zipline.gens.brokers.%s_broker' % broker.lower() try: bmod = import_module(mod_name) except ImportError: ctx.fail("unsupported broker: can't import module %s" % mod_name) cl_name = '%sBroker' % broker.upper() try: bclass = getattr(bmod, cl_name) except AttributeError: ctx.fail("unsupported broker: can't import class %s from %s" % (cl_name, mod_name)) brokerobj = bclass(broker_uri) if (algotext is not None) == (algofile is not None): ctx.fail( "must specify exactly one of '-f' / '--algofile' or" " '-t' / '--algotext'", ) perf = _run( initialize=None, handle_data=None, before_trading_start=None, analyze=None, algofile=algofile, algotext=algotext, defines=define, data_frequency=data_frequency, capital_base=capital_base, data=None, bundle=bundle, bundle_timestamp=bundle_timestamp, start=start, end=end, output=output, trading_calendar=None, print_algo=print_algo, local_namespace=local_namespace, environ=os.environ, broker=brokerobj, state_filename=state_file, realtime_bar_target=realtime_bar_target, reader = reader ) if output == '-': click.echo(str(perf)) elif output != os.devnull: # make the zipline magic not write any data perf.to_pickle(output) return perf