def sub_cmd_continue(args): old_instance = get_instance(args.sii) mds_name = old_instance['mds'] sc = old_instance['sc'] print('marketing data src: %s strategy config path: %s ' % (mds_name, sc)) config = xq.get_strategy_config(sc) pprint.pprint(config, indent=4) module_name = config["module_name"].replace("/", ".") class_name = config["class_name"] symbol = config['symbol'] instance_id = create_instance_id() if args.log: logfilename = class_name + "_" + symbol + "_" + instance_id + ".log" print(logfilename) log.init("backtest", logfilename) log.info("strategy name: %s; config: %s" % (class_name, config)) engine = BackTest(instance_id, mds_name, config, args.log) strategy = ts.createInstance(module_name, class_name, config, engine) engine.orders = old_instance["orders"] continue_time = old_instance["end_time"] oldest_time = engine.md.get_oldest_time(strategy.config['symbol'], kl.KLINE_INTERVAL_1MINUTE) if continue_time < oldest_time: continue_time = oldest_time latest_time = engine.md.get_latest_time(strategy.config['symbol'], kl.KLINE_INTERVAL_1MINUTE) end_time = latest_time print(" time range old( %s ~ %s ); continue( %s ~ %s )" % (old_instance["start_time"].strftime("%Y-%m-%d %H:%M"), old_instance["end_time"].strftime("%Y-%m-%d %H:%M"), continue_time.strftime("%Y-%m-%d %H:%M"), end_time.strftime("%Y-%m-%d %H:%M"))) print("run2 kline_data_type: %s " % (engine.md.kline_data_type)) tick_count = run2(engine, engine.md, strategy, continue_time, end_time) print("\n total tick count: %d" % (tick_count)) engine.analyze_orders(engine.orders) _id = bt_db.insert_one( BACKTEST_INSTANCES_COLLECTION_NAME, { "instance_id": instance_id, "start_time": old_instance["start_time"], "end_time": end_time, "orders": engine.orders, "mds": mds_name, "sc": sc, }, ) engine.display(symbol, engine.orders, strategy.cur_price, end_time, True, args.rmk)
def sub_cmd_signal(args): if not (args.m and args.sc): exit(1) instance_id = create_instance_id() config = xq.get_strategy_config(args.sc) pprint.pprint(config) module_name = config["module_name"].replace("/", ".") class_name = config["class_name"] symbol = config['symbol'] interval = config["kline"]["interval"] if args.log: logfilename = class_name + "_" + symbol + "_" + instance_id + ".log" print(logfilename) log.init("testsignal", logfilename) log.info("strategy name: %s; config: %s" % (class_name, config)) md = DBMD(args.m, kl.KLINE_DATA_TYPE_JSON) engine = TestSignal(md, instance_id, config, args.log) strategy = ts.createInstance(module_name, class_name, config, engine) start_time, end_time = get_time_range(md, symbol, args.r) print("run2 kline_data_type: %s " % (md.kline_data_type)) tick_count = run2(engine, md, strategy, start_time, end_time) print("\n total tick count: %d" % (tick_count)) #pprint.pprint(engine.signals) print("signal count: ", len(engine.signals)) ''' _id = bt_db.insert_one( BACKTEST_INSTANCES_COLLECTION_NAME, { "instance_id": instance_id, "start_time": start_time, "end_time": end_time, "signals": engine.signals, "mds": args.m, "sc": args.sc, }, ) ''' if args.chart: signalsets = engine.get_signalsets() title = "signal: " + symbol + ' ' + config['kline']['interval'] + ' ' if strategy.name: title += strategy.name else: title += config['class_name'] chart(title, engine.md, symbol, interval, start_time, end_time, [], args, signalsets)
def sub_cmd_run(args): if not (args.m and args.sc): exit(1) instance_id = create_instance_id() config = xq.get_strategy_config(args.sc) pprint.pprint(config) module_name = config["module_name"].replace("/", ".") class_name = config["class_name"] symbol = config['symbol'] if args.log: logfilename = class_name + "_" + symbol + "_" + instance_id + ".log" print(logfilename) log.init("backtest", logfilename) log.info("strategy name: %s; config: %s" % (class_name, config)) engine = BackTest(instance_id, args.m, config, args.log) strategy = ts.createInstance(module_name, class_name, config, engine) md = engine.md start_time, end_time = get_time_range(md, symbol, args.r) print("run2 kline_data_type: %s " % (md.kline_data_type)) tick_count = run2(engine, md, strategy, start_time, end_time) print("\n total tick count: %d" % (tick_count)) engine.analyze_orders(engine.orders) _id = bt_db.insert_one( BACKTEST_INSTANCES_COLLECTION_NAME, { "instance_id": instance_id, "start_time": start_time, "end_time": end_time, "orders": engine.orders, "mds": args.m, "sc": args.sc, }, ) engine.display(symbol, engine.orders, strategy.cur_price, end_time, True, args.rmk) if args.chart: ordersets = [] ordersets.append(engine.orders) chart(md, engine.config, start_time, end_time, ordersets, args)
def run(args): if not (args.m and args.sc and args.r): exit(1) instance_id = datetime.now().strftime("%Y%m%d-%H%M%S_") + str( uuid.uuid1()) # 每次回测都是一个独立的实例 print('instance_id: %s' % instance_id) config = xq.get_strategy_config(args.sc) module_name = config["module_name"].replace("/", ".") class_name = config["class_name"] symbol = config['symbol'] time_range = args.r start_time, end_time = ts.parse_date_range(time_range) if args.log: logfilename = class_name + "_" + symbol + "_" + time_range + "_" + instance_id + ".log" print(logfilename) log.init("backtest", logfilename) log.info("strategy name: %s; config: %s" % (class_name, config)) engine = BackTest(instance_id, args.m, config) strategy = ts.createInstance(module_name, class_name, config, engine) engine.run(strategy, start_time, end_time) engine.analyze(symbol, engine.orders) _id = bt_db.insert_one( BACKTEST_INSTANCES_COLLECTION_NAME, { "instance_id": instance_id, "start_time": start_time, "end_time": end_time, "orders": engine.orders, "mds": args.m, "sc": args.sc, }, ) if args.chart: engine.chart(symbol, start_time, end_time, args)
def real_run(config, instance_id, exchange_name, value, args): info = 'instance_id: %s, exchange_name: %s, value: %s ' % (instance_id, exchange_name, value) print(info) module_name = config["module_name"].replace("/", ".") class_name = config["class_name"] if args.log: logfilename = instance_id + ".log" print(logfilename) server_ip = os.environ.get('LOG_SERVER_IP') server_port = os.environ.get('LOG_SERVER_PORT') print('Log server IP: %s, Log server port: %s' % (server_ip, server_port)) log.init('real', logfilename, server_ip, server_port) log.info("%s" % (info)) log.info("strategy name: %s; config: %s" % (class_name, config)) engine = RealEngine(instance_id, exchange_name, config, value, args.log) strategy = ts.createInstance(module_name, class_name, config, engine) engine.run(strategy, args.debug)
date_range = sys.argv[params_index] params_index += 1 else: help_print() exit(1) logfilename = (select + "_" + class_name + "_" + config["symbol"] + "_" + date_range + "_" + instance_id + ".log") start_time, end_time = parse_date_range(date_range) else: help_print() exit(1) print(logfilename) log.init(logfilename) log.info("strategy name: %s; config: %s" % (class_name, config)) if select == "real": engine = RealEngine(instance_id, config) strategy = ts.createInstance(module_name, class_name, config, engine) engine.value = value engine.run(strategy, debug) elif select == "backtest": if len(sys.argv) > params_index: display_switch = sys.argv[params_index] params_index += 1 else:
help_print() exit(1) logfilename = (class_name + "_" + config["symbol"] + "_" + date_range + "_" + instance_id + ".log") start_time, end_time = ts.parse_date_range(date_range) else: help_print() exit(1) print(logfilename) server_ip = os.environ.get('LOG_SERVER_IP') server_port = os.environ.get('LOG_SERVER_PORT') print('Log server IP: %s, Log server port: %s' % (server_ip, server_port)) log.init(select, logfilename, server_ip, server_port) log.info("strategy name: %s; config: %s" % (class_name, config)) if select == "display": engine = Display(instance_id, config) strategy = ts.createInstance(module_name, class_name, config, engine) engine.value = value engine.run(strategy, db_name) elif select == "real": engine = RealEngine(instance_id, config["exchange"], config, value) strategy = ts.createInstance(module_name, class_name, config, engine) engine.run(strategy, debug)
help_print() exit(1) logfilename = (select + "_" + class_name + "_" + config["symbol"] + "_" + date_range + "_" + instance_id + ".log") start_time, end_time = parse_date_range(date_range) else: help_print() exit(1) print(logfilename) server_ip = os.environ.get('LOG_SERVER_IP') server_port = os.environ.get('LOG_SERVER_PORT') print('Log server IP: %s, Log server port: %s' % (server_ip, server_port)) log.init(logfilename, server_ip, server_port) log.info("strategy name: %s; config: %s" % (class_name, config)) if select == "display": engine = Display(instance_id, config) strategy = ts.createInstance(module_name, class_name, config, engine) engine.value = value engine.run(strategy, db_name) elif select == "real": engine = RealEngine(instance_id, config) strategy = ts.createInstance(module_name, class_name, config, engine) engine.value = value