def real2_update(args): record = {} if args.user: record["user"] = args.user if args.instance_id: record["instance_id"] = args.instance_id if args.config_path: record["config_path"] = args.config_path if args.exchange: record["exchange"] = args.exchange if args.value: instance = si.get_strategy_instance(args.sii) instance_id = instance["instance_id"] exchange_name = instance["exchange"] if not exchange_name: exchange_name = record["exchange_name"] config_path = instance["config_path"] if not config_path: config_path = record["config_path"] value = instance["value"] config = xq.get_strategy_config(config_path) symbol = config['symbol'] realEngine = RealEngine(instance_id, exchange_name, config, value) orders = realEngine.get_orders(symbol) if orders: return record["value"] = args.value if args.status: record["status"] = args.status if record: si.update_strategy_instance({"instance_id": args.sii}, record)
def real_analyze(config, instance_id, exchange_name, value, print_switch_hl, display_rmk): symbol = config['symbol'] realEngine = RealEngine(instance_id, exchange_name, config, value) orders = realEngine.get_orders(symbol) realEngine.analyze(symbol, orders, print_switch_hl, display_rmk)
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)
def real2_list(args): td_db = get_mongodb(setup.trade_db_name) ss = td_db.find(si.STRATEGY_INSTANCE_COLLECTION_NAME, {"user": args.user}) #pprint(ss) all_value = 0 all_his_profit = 0 all_flo_profit = 0 all_commission = 0 title_head_fmt = "%-30s %10s%12s" head_fmt = "%-30s %10s(%10.0f)" title_profit_fmt = "%21s %21s %12s" profit_fmt = "%12.2f(%6.2f%%) %12.2f(%6.2f%%) %12.2f" title_tail_fmt = " %-60s %-20s %10s" print(title_head_fmt % ("instance_id", "value", "") + title_profit_fmt % ("history_profit", "floating_profit", "commission") + title_tail_fmt % ("config_path", "exchange", "status")) for s in ss: instance_id = s["instance_id"] exchange_name = s["exchange"] value = s["value"] config_path = s["config_path"] if "status" in s: status = s["status"] else: status = "" if status != args.status and status != "": continue all_value += value profit_info = "" try: config = xq.get_strategy_config(config_path) symbol = config['symbol'] realEngine = RealEngine(instance_id, exchange_name, config, value) orders = realEngine.get_orders(symbol) pst_info = realEngine.get_pst_by_orders(orders) history_profit, history_profit_rate, history_commission = realEngine.get_history(pst_info) all_his_profit += history_profit floating_profit, floating_profit_rate, floating_commission, cur_price = realEngine.get_floating(symbol, pst_info) all_flo_profit += floating_profit commission = history_commission + floating_commission all_commission += commission profit_info = profit_fmt % (history_profit, history_profit_rate*100, floating_profit, floating_profit_rate*100, commission) except Exception as ept: profit_info = "error: %s" % (ept) print(head_fmt % (instance_id, value, (value+history_profit+floating_profit)) + profit_info + title_tail_fmt % (config_path, exchange_name, status)) if args.stat: print(head_fmt % ("all", all_value, all_value+all_his_profit+all_flo_profit) + profit_fmt % (all_his_profit, all_his_profit/all_value*100, all_flo_profit, all_flo_profit/all_value*100, all_commission))
def real2_list(args): td_db = get_mongodb(setup.trade_db_name) ss = td_db.find(si.STRATEGY_INSTANCE_COLLECTION_NAME, {"user": args.user}) #pprint(ss) title1_fmt = "%-30s %10s" title2_fmt = "%-60s %-20s %10s" print((title1_fmt + " %s " + title2_fmt) % ("instance_id", "value", " history_profit floating_profit", "config_path", "exchange", "status")) for s in ss: instance_id = s["instance_id"] exchange_name = s["exchange"] value = s["value"] config_path = s["config_path"] if "status" in s: status = s["status"] else: status = "" if status != args.status and status != "": continue profit_info = "" try: config = xq.get_strategy_config(config_path) symbol = config['symbol'] realEngine = RealEngine(instance_id, exchange_name, config, value) orders = realEngine.get_orders(symbol) pst_info = realEngine.get_pst_by_orders(orders) history_profit, history_profit_rate, history_commission = realEngine.get_history( pst_info) floating_profit, floating_profit_rate, floating_commission, cur_price = realEngine.get_floating( symbol, pst_info) profit_info = "%10.2f(%6.2f%%) %10.2f(%6.2f%%)" % ( history_profit, history_profit_rate * 100, floating_profit, floating_profit_rate * 100) except Exception as ept: profit_info = "error: %s" % (ept) print((title1_fmt + " %s " + title2_fmt) % (instance_id, value, profit_info, config_path, exchange_name, status))
choices=[bl.DIRECTION_LONG, bl.DIRECTION_SHORT], help='direction') parser.add_argument('-action', choices=[bl.OPEN_POSITION, bl.CLOSE_POSITION], help='action') #parser.add_argument('-pr', type=float, default=0, help='postion rate') parser.add_argument('-rmk', help='remark') args = parser.parse_args() print(args) if not (args.sii and args.action): parser.print_help() exit(1) instance = get_strategy_instance(args.sii) config = xq.get_strategy_config(instance['config_path']) re = RealEngine(args.sii, instance['exchange'], config, instance['value']) symbol = config['symbol'] klines = re.md.get_klines(symbol, config["kline"]["interval"], 1) if len(klines) <= 0: exit(1) cur_price = float(klines[-1][re.md.get_kline_seat_close()]) pst_info = re.get_position(symbol, cur_price) if args.action == bl.OPEN_POSITION: pst_rate = 1 else: pst_rate = 0 print('before position info: %s' % (pst_info)) print('args.action: %s, pst_rate: %g' % (args.action, pst_rate))
def real_view(config, instance_id, exchange_name, value): symbol = config['symbol'] realEngine = RealEngine(instance_id, exchange_name, config, value) orders = realEngine.get_orders(symbol) realEngine.view(symbol, orders)
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)
+ strategy_config["symbol"] + "_" + start_time.strftime("%Y%m%d") + "_" + end_time.strftime("%Y%m%d") + "_" + instance_id + ".log" ) print(logfilename) logging.basicConfig(level=logging.NOTSET, filename=logfilename) logging.info("strategy name: %s; config: %s", class_name, strategy_config) logging.info("engine config: %s", engine_config) if engine_config["select"] == "real": engine = RealEngine(instance_id, engine_config) else: engine = BackTest(instance_id, engine_config) strategy = createInstance(module_name, class_name, strategy_config, engine) if debug: engine.run(strategy) else: try: engine.run(strategy) except Exception as ept: logging.critical(ept)
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) 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["exchange"], config) strategy = ts.createInstance(module_name, class_name, config, engine) engine.run(strategy, start_time, end_time, display_switch)
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 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)