Exemplo n.º 1
0
def sub_cmd_refresh(args):
    instance_id = args.sii
    instance = get_instance(instance_id)
    print('marketing data src: %s  strategy config path: %s  ' %
          (instance['mds'], instance['sc']))

    config = xq.get_strategy_config(instance['sc'])
    pprint.pprint(config, indent=4)

    engine = BackTest(instance_id, instance['mds'], config)
    module_name = config["module_name"].replace("/", ".")
    class_name = config["class_name"]
    strategy = ts.createInstance(module_name, class_name, config, engine)
    refresh(engine, engine.md, strategy, [
        datetime.fromtimestamp(order["create_time"])
        for order in instance['orders']
    ])
    orders = engine.orders
    for idx, order in enumerate(engine.orders):
        old_order = instance['orders'][idx]
        if "high" in old_order:
            order["high"] = old_order["high"]
            order["high_time"] = old_order["high_time"]
            order["low"] = old_order["low"]
            order["low_time"] = old_order["low_time"]
    engine.analyze(config['symbol'], engine.orders, True, True)
Exemplo n.º 2
0
def sub_cmd_search(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']
    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)

    count = args.count
    result = []
    for i in range(count):
        rs = strategy.search_init()
        print("%d/%d    %s" % (i, count, rs))
        tick_count = run2(engine, md, strategy, start_time, end_time)
        result.append((i, rs, engine.search_calc(symbol, engine.orders)))
        engine.orders = []

    sorted_rs = sorted(result, key=lambda x: x[2][0], reverse=True)
    for r in sorted_rs:
        info = "%6s    %30s    %s " % r
        print(info)
        engine.log_debug(info)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def child_process(name, task_q, result_q, instance_id, engine_config,
                  module_name, class_name, strategy_config, start_time,
                  end_time):
    #print("child process name %s, id %s start" % (name, os.getpid()))

    child_engine = MultiSearchChild(instance_id, engine_config)
    child_strategy = ts.createInstance(module_name, class_name,
                                       strategy_config, child_engine)

    while not task_q.empty():
        value = task_q.get(True, 1)
        rs = child_strategy.search_init()
        #print("child_process(%s)  %s, rs: %s" % (name, value, rs))

        result_q.put((value,
                      child_engine.handle_one(child_strategy, start_time,
                                              end_time), rs))
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
def child_process(name, task_q, result_q, md_name, config, module_name, class_name, start_time, end_time):
    #print("child process name %s, id %s start" % (name, os.getpid()))

    engine = BackTest(create_instance_id(), md_name, config)
    strategy = ts.createInstance(module_name, class_name, config, engine)
    while not task_q.empty():
        value = task_q.get(True, 1)
        rs = strategy.search_init()
        #print("child_process(%s)  %s, rs: %s" % (name, value, rs))
        tick_count = run2(engine, engine.md, strategy, start_time, end_time, False)
        #print("tick_count: %s" % (tick_count) )
        symbol = config['symbol']
        result_q.put((value, engine.search_calc(symbol, engine.orders), rs))
        engine.orders = []

    #print("child process name %s, id %s finish" % (name, os.getpid()))
    return
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
Arquivo: main.py Projeto: dxcv/xquant
        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:
            display_switch = False

        engine = BackTest(instance_id, config)
        strategy = ts.createInstance(module_name, class_name, config, engine)

        engine.run(strategy, start_time, end_time, display_switch)