def backtesting(): if request.method == 'POST': action_type = request.args["action_type"] success = False reply = "Action failed" if action_type == "start_backtesting": files = request.get_json() source = request.args["source"] reset_tentacle_config = request.args["reset_tentacle_config"] if "reset_tentacle_config" in request.args \ else False success, reply = start_backtesting_using_specific_files( files, source, reset_tentacle_config) if success: return get_rest_reply(jsonify(reply)) else: return get_rest_reply(reply, 500) elif request.method == 'GET': if request.args: target = request.args["update_type"] if target == "backtesting_report": source = request.args["source"] backtesting_report = get_backtesting_report(source) return jsonify(backtesting_report) elif target == "backtesting_status": backtesting_status, progress = get_backtesting_status() status = {"status": backtesting_status, "progress": progress} return jsonify(status) else: return render_template( 'backtesting.html', activated_trading_mode=get_config_activated_trading_mode(), data_files=get_data_files_with_description())
def backtesting(): if request.method == 'POST': action_type = request.args["action_type"] success = False reply = "Action failed" if action_type == "start_backtesting": files = request.get_json() success, reply = start_backtesting_using_specific_files(files) if success: return get_rest_reply(jsonify(reply)) else: return get_rest_reply(reply, 500) elif request.method == 'GET': if request.args: target = request.args["update_type"] if target == "backtesting_report": backtesting_report = get_backtesting_report() return jsonify(backtesting_report) elif target == "backtesting_status": backtesting_status, progress = get_backtesting_status() status = {"status": backtesting_status, "progress": progress} return jsonify(status) else: return render_template( 'backtesting.html', data_files=get_data_files_with_description())
def data_collector(): if request.method == 'POST': action_type = request.args["action_type"] success = False reply = "Action failed" if action_type == "delete_data_file": file = request.get_json() success, reply = get_delete_data_file(file) elif action_type == "start_collector": details = request.get_json() success, reply = collect_data_file(details["exchange"], details["symbol"]) elif action_type == "import_data_file": if request.files: file = request.files['file'] name = secure_filename(request.files['file'].filename) success, reply = save_data_file(name, file) alert = {"success": success, "message": reply} else: alert = {} current_exchange = get_current_exchange() # here return template to force page reload because of file upload via input form return render_template( 'data_collector.html', data_files=get_data_files_with_description(), ccxt_exchanges=sorted(get_full_exchange_list()), current_exchange=get_current_exchange(), full_symbol_list=sorted(get_symbol_list([current_exchange])), alert=alert) if success: return get_rest_reply(jsonify(reply)) else: return get_rest_reply(reply, 500) elif request.method == 'GET': origin_page = None if request.args: action_type_key = "action_type" if action_type_key in request.args: target = request.args[action_type_key] if target == "symbol_list": exchange = request.args.get('exchange') return jsonify(sorted(get_symbol_list([exchange]))) from_key = "from" if from_key in request.args: origin_page = request.args[from_key] current_exchange = get_current_exchange() return render_template('data_collector.html', data_files=get_data_files_with_description(), ccxt_exchanges=sorted(get_full_exchange_list()), current_exchange=get_current_exchange(), full_symbol_list=sorted( get_symbol_list([current_exchange])), origin_page=origin_page, alert={})
def _handle_module_operation(update_type): request_data = request.get_json() if request_data: packages_operation_result = {} if update_type == "update_modules": packages_operation_result = update_modules(request_data) elif update_type == "uninstall_modules": packages_operation_result = uninstall_modules(request_data) if packages_operation_result is not None: return get_rest_reply(jsonify(packages_operation_result)) else: action = update_type.split("_")[0] return get_rest_reply( f'Impossible to {action} module(s), check the logs for more information.', 500) else: return get_rest_reply( '{"Need at least one element be selected": "ko"}', 500)
def config_actions(): action = request.args.get("action") if action == "reset_trading_history": reset_trading_history() return jsonify({ "title": "Trading history reset", "details": "Next trading sessions will not consider past sessions for " "profitability and trading simulator will start using a fresh portfolio." }) return get_rest_reply("No specified action.", code=500)
def config_tentacle(): if request.method == 'POST': tentacle_name = request.args.get("name") action = request.args.get("action") success = True response = "" if action == "update": request_data = request.get_json() success, response = update_tentacle_config(tentacle_name, request_data) elif action == "factory_reset": success, response = reset_config_to_default(tentacle_name) if success: return get_rest_reply(jsonify(response)) else: return get_rest_reply(response, 500) else: if request.args: tentacle_name = request.args.get("name") tentacle_class, tentacle_type, tentacle_desc = get_tentacle_from_string(tentacle_name) evaluator_config = get_evaluator_detailed_config() if tentacle_type == "strategy" and \ tentacle_desc[REQUIREMENTS_KEY] == ["*"] else None strategy_config = get_strategy_config() if tentacle_type == "trading mode" and \ len(tentacle_desc[REQUIREMENTS_KEY]) > 1 else None evaluator_startup_config = get_evaluator_startup_config() if evaluator_config or strategy_config else None return render_template('config_tentacle.html', name=tentacle_name, tentacle_type=tentacle_type, tentacle_class=tentacle_class, tentacle_desc=tentacle_desc, evaluator_startup_config=evaluator_startup_config, strategy_config=strategy_config, evaluator_config=evaluator_config, activated_trading_mode=get_config_activated_trading_mode(edited_config=True), data_files=get_data_files_with_description()) else: return render_template('config_tentacle.html')
def orders(): if request.method == 'GET': real_open_orders, simulated_open_orders = get_open_orders() return json.dumps({"real_open_orders": real_open_orders, "simulated_open_orders": simulated_open_orders}) elif request.method == "POST": result = "" request_data = request.get_json() action = request.args.get("action") if action == "cancel_order": if cancel_orders([request_data]): result = "Order cancelled" else: return get_rest_reply('Impossible to cancel order: order not found.', 500) elif action == "cancel_orders": removed_count = cancel_orders(request_data) result = f"{removed_count} orders cancelled" return jsonify(result)
def _handle_package_operation(update_type): if update_type == "add_package": request_data = request.get_json() success = False if request_data: path_or_url, action = next(iter(request_data.items())) path_or_url = path_or_url.strip() if action == "description": package_description = get_tentacles_package_description( path_or_url) if package_description: return get_rest_reply(jsonify(package_description)) else: return get_rest_reply( f'Impossible to find {path_or_url} tentacles package information.', 500) elif action == "register_and_install": installation_result = register_and_install(path_or_url) if installation_result: return get_rest_reply(jsonify(installation_result)) else: return get_rest_reply( 'Impossible to install the given tentacles package, check the logs ' 'for more information.', 500) if not success: return get_rest_reply('{"operation": "ko"}', 500) elif update_type in [ "install_packages", "update_packages", "reset_packages" ]: packages_operation_result = {} if update_type == "install_packages": packages_operation_result = install_packages() elif update_type == "update_packages": packages_operation_result = update_packages() elif update_type == "reset_packages": packages_operation_result = reset_packages() if packages_operation_result is not None: return get_rest_reply(jsonify(packages_operation_result)) else: action = update_type.split("_")[0] return get_rest_reply( f'Impossible to {action} packages, check the logs for more information.', 500)
def strategy_optimizer(): from interfaces.web.models.strategy_optimizer import get_strategies_list, get_current_strategy, \ get_time_frames_list, \ get_evaluators_list, get_risks_list, start_optimizer, get_optimizer_results, get_optimizer_status, \ get_optimizer_report, get_current_run_params, get_trading_mode if request.method == 'POST': update_type = request.args["update_type"] request_data = request.get_json() success = False reply = "Operation OK" if request_data: if update_type == "start_optimizer": try: strategy = request_data["strategy"][0] time_frames = request_data["time_frames"] evaluators = request_data["evaluators"] risks = request_data["risks"] success, reply = start_optimizer(strategy, time_frames, evaluators, risks) except Exception as e: return get_rest_reply( '{"start_optimizer": "ko: ' + str(e) + '"}', 500) if success: return get_rest_reply(jsonify(reply)) else: return get_rest_reply(reply, 500) elif request.method == 'GET': if request.args: target = request.args["update_type"] if target == "optimizer_results": optimizer_results = get_optimizer_results() return jsonify(optimizer_results) if target == "optimizer_report": optimizer_report = get_optimizer_report() return jsonify(optimizer_report) elif target == "optimizer_status": optimizer_status, progress, overall_progress, errors = get_optimizer_status( ) status = { "status": optimizer_status, "progress": progress, "overall_progress": overall_progress, "errors": errors } return jsonify(status) if target == "strategy_params": strategy_name = request.args["strategy_name"] params = { "time_frames": list(get_time_frames_list(strategy_name)), "evaluators": list(get_evaluators_list(strategy_name)) } return jsonify(params) else: current_strategy = get_current_strategy() return render_template( 'advanced_strategy_optimizer.html', strategies=get_strategies_list(), current_strategy=current_strategy, time_frames=get_time_frames_list(current_strategy), evaluators=get_evaluators_list(current_strategy), risks=get_risks_list(), trading_mode=get_trading_mode(), run_params=get_current_run_params())
def refresh_real_trader(): try: force_real_traders_refresh() return jsonify("Trader(s) refreshed") except RuntimeError: return get_rest_reply("No trader to refresh", 500)