| | |
| | | import time |
| | | |
| | | import constant |
| | | import log |
| | | import socket_manager |
| | | import trade_manager |
| | | from code_attribute.code_price_manager import CodesLimitRateManager |
| | | from db import mysql_data, redis_manager |
| | | from db.redis_manager import RedisUtils |
| | | from log import logger_request_debug |
| | | from log_module import log, request_log_util |
| | | from log_module.log import logger_request_debug |
| | | from middle_l1_data_server import L1DataManager |
| | | from output import push_msg_manager |
| | | from utils import socket_util, hosting_api_util, huaxin_trade_record_manager, huaxin_util, tool, global_data_cache_util |
| | | from utils.history_k_data_util import HistoryKDatasUtils, JueJinApi |
| | | from utils.huaxin_trade_record_manager import PositionManager |
| | |
| | | data_json = json.loads(data_str) |
| | | type_ = data_json['type'] |
| | | try: |
| | | log.request_info("middle_api_server", f"请求开始:{type_}") |
| | | request_log_util.request_info("middle_api_server", f"请求开始:{type_}") |
| | | if type(type_) == int: |
| | | # 处理数字型TYPE |
| | | return_str = self.process_num_type(sk, type_, data_str) |
| | |
| | | raise Exception("签名错误") |
| | | codes_data = data_json["data"] |
| | | code = codes_data["code"] |
| | | volume = codes_data["volume"] |
| | | price = codes_data["price"] |
| | | money = codes_data.get("money") |
| | | volume = codes_data.get("volume") |
| | | price = codes_data.get("price") |
| | | price_type = codes_data.get("price_type") |
| | | try: |
| | | if not code: |
| | | raise Exception("请上传code") |
| | | if not volume: |
| | | raise Exception("请上传volume") |
| | | |
| | | if round(float(price), 2) <= 0: |
| | | prices = HistoryKDatasUtils.get_now_price([code]) |
| | | if not prices: |
| | | raise Exception("现价获取失败") |
| | | price = prices[0][1] |
| | | if not price or round(float(price), 2) <= 0: |
| | | if price_type is None: |
| | | price_type = 0 |
| | | # 默认为笼子价 |
| | | pre_close = HistoryKDatasUtils.get_gp_latest_info([code], "sec_id,pre_close")[0][ |
| | | "pre_close"] |
| | | if price_type == 0: # 价格笼子 |
| | | # 获取现价 |
| | | prices = HistoryKDatasUtils.get_now_price([code]) |
| | | if not prices: |
| | | raise Exception("现价获取失败") |
| | | now_price = prices[0][1] |
| | | limit_up_price = round( |
| | | float(tool.get_limit_up_price_by_preprice(code, pre_close)), |
| | | 2) |
| | | price = min(tool.get_buy_max_price(now_price), limit_up_price) |
| | | elif price_type == 1: # 跌停价 |
| | | limit_down_price = round( |
| | | float(tool.get_limit_down_price_by_preprice(code, pre_close)), |
| | | 2) |
| | | price = limit_down_price |
| | | elif price_type == 2: # 涨停价 |
| | | limit_up_price = round( |
| | | float(tool.get_limit_up_price_by_preprice(code, pre_close)), |
| | | 2) |
| | | price = limit_up_price |
| | | elif price_type == 3: # 现价 |
| | | prices = HistoryKDatasUtils.get_now_price([code]) |
| | | if not prices: |
| | | raise Exception("现价获取失败") |
| | | now_price = prices[0][1] |
| | | price = now_price |
| | | elif price_type == 4: # 买5价 |
| | | prices = HistoryKDatasUtils.get_now_price([code]) |
| | | if not prices: |
| | | raise Exception("现价获取失败") |
| | | now_price = prices[0][1] |
| | | price = now_price - 0.04 |
| | | if not volume and money: |
| | | volume = (money // int(round(float(price) * 100))) * 100 |
| | | if volume < 100: |
| | | volume = 100 |
| | | # 下单 |
| | | result = hosting_api_util.trade_order(hosting_api_util.TRADE_DIRECTION_BUY, code, volume, |
| | | round(float(price), 2)) |
| | | result = hosting_api_util.trade_order(hosting_api_util.TRADE_DIRECTION_BUY, code, |
| | | volume, price) |
| | | if result: |
| | | resultJSON = result |
| | | print("下单结果:", resultJSON) |
| | |
| | | break |
| | | |
| | | elif type_ == 'common': |
| | | # 验证签名 |
| | | if not is_sign_right: |
| | | raise Exception("签名错误") |
| | | params = data_json["data"] |
| | | result = hosting_api_util.common_request(params) |
| | | ctype = params.get("ctype") |
| | | trade_sell_types = {"get_current_l1_codes", "get_positions", "get_l2_deal_price", |
| | | "buy_cb_for_commission", "sell_cb_for_commission", "get_deal_queue", "auto_cancel_sell_mode"} |
| | | if ctype in trade_sell_types: |
| | | result = hosting_api_util.common_request(params, |
| | | client_type=socket_manager.ClientSocketManager.CLIENT_TYPE_TRADE_SELL) |
| | | else: |
| | | result = hosting_api_util.common_request(params) |
| | | return_str = json.dumps(result) |
| | | break |
| | | |
| | |
| | | # 同步交易数据 |
| | | sync_type = data_json["data"]["type"] |
| | | hosting_api_util.refresh_trade_data(sync_type) |
| | | hosting_api_util.refresh_trade_data(sync_type, blocking=False, |
| | | client_type=socket_manager.ClientSocketManager.CLIENT_TYPE_TRADE_SELL) |
| | | return_str = json.dumps( |
| | | {"code": 0, "data": {}, "msg": ""}) |
| | | elif type_ == "get_huaxin_subscript_codes": |
| | | # 获取华鑫订阅的代码 |
| | | fresults = global_data_cache_util.huaxin_subscript_codes |
| | | fdata = [] |
| | | try: |
| | | # 获取当前涨停比例 |
| | | rate_results_dict = global_data_cache_util.huaxin_subscript_codes_rate |
| | | for r in fresults: |
| | | fdata.append( |
| | | (r[0], r[1], rate_results_dict.get(r[0]) if r[0] in rate_results_dict else 0, |
| | | r[2])) |
| | | fdata.sort(key=lambda r: r[2], reverse=True) |
| | | except: |
| | | fdata = fresults |
| | | update_time = global_data_cache_util.huaxin_subscript_codes_update_time |
| | | if update_time is None: |
| | | update_time = '' |
| | | return_str = json.dumps( |
| | | {"code": 0, "data": {"count": len(fresults), "list": fresults, "update_time": update_time}, |
| | | {"code": 0, |
| | | "data": {"count": len(fresults), "list": fdata, "update_time": update_time}, |
| | | "msg": ""}) |
| | | pass |
| | | elif type_ == "get_huaxin_position_subscript_codes": |
| | | # 获取华鑫订阅的代码 |
| | | fresults = global_data_cache_util.huaxin_position_subscript_codes |
| | | update_time = global_data_cache_util.huaxin_position_subscript_codes_update_time |
| | | if update_time is None: |
| | | update_time = '' |
| | | return_str = json.dumps( |
| | | {"code": 0, |
| | | "data": {"count": len(fresults), "list": fresults, "update_time": update_time}, |
| | | "msg": ""}) |
| | | pass |
| | | elif type_ == "export_l2_data": |
| | |
| | | return_str = json.dumps(result) |
| | | elif type_ == "trade_server_channels": |
| | | trade_channels = socket_manager.ClientSocketManager.list_client( |
| | | socket_manager.ClientSocketManager.CLIENT_TYPE_TRADE) |
| | | socket_manager.ClientSocketManager.CLIENT_TYPE_TRADE_SELL) |
| | | common_channels = socket_manager.ClientSocketManager.list_client( |
| | | socket_manager.ClientSocketManager.CLIENT_TYPE_COMMON) |
| | | data = {} |
| | |
| | | elif type_ == "add_sell_rule": |
| | | result = hosting_api_util.sell_rule(hosting_api_util.OPERRATE_ADD, data=data_json["data"]) |
| | | return_str = json.dumps(result) |
| | | elif type_ == "update_sell_rule": |
| | | result = hosting_api_util.sell_rule(hosting_api_util.OPERRATE_SET, data=data_json["data"]) |
| | | return_str = json.dumps(result) |
| | | elif type_ == "del_sell_rule": |
| | | id_ = data_json["data"]["id"] |
| | | result = hosting_api_util.sell_rule(hosting_api_util.OPERRATE_DELETE, data={"id": id_}) |
| | |
| | | code = data_json["data"]["code"] |
| | | result = hosting_api_util.get_code_position_info(code) |
| | | return_str = json.dumps(result) |
| | | elif type_ == "common": |
| | | elif type_ == "register_msg_receiver": |
| | | params = data_json["data"] |
| | | result = hosting_api_util.common_request(params) |
| | | msg_types = params["types"] |
| | | try: |
| | | push_msg_manager.SocketManager.regirster_socket(sk, msg_types) |
| | | result = {"code": 0} |
| | | except Exception as e: |
| | | result = {"code": 1, "msg": str(e)} |
| | | return_str = json.dumps(result) |
| | | sk.sendall(socket_util.load_header(return_str.encode(encoding='utf-8'))) |
| | | while True: |
| | | try: |
| | | buf = sk.recv(1024) |
| | | print(f"收到数据:{buf.decode(encoding='utf-8')}") |
| | | time.sleep(1) |
| | | except: |
| | | print("数据断开") |
| | | break |
| | | elif type_ == "get_latest_cancel_orders": |
| | | # 获取最近的撤单 |
| | | results = huaxin_trade_record_manager.DelegateRecordManager.list_latest_cancel_records(10) |
| | | fresults = [] |
| | | for result in results: |
| | | temp = {} |
| | | for key in result: |
| | | if key in ["securityID", "securityName", "direction", "orderSysID", "acceptTime", |
| | | "cancelTime", "limitPrice", "volume"]: |
| | | temp[key] = result[key] |
| | | # 过滤虚拟单与卖单 |
| | | if int(temp["volume"] <= 100): |
| | | continue |
| | | if int(temp["direction"] != 0): |
| | | continue |
| | | fresults.append(temp) |
| | | return_str = json.dumps(json.dumps({"code": 0, "data": fresults})) |
| | | elif type_ == "get_buy1_info": |
| | | code = data_json["data"]["code"] |
| | | result = hosting_api_util.common_request({"ctype": "get_buy1_info", "code": code}, |
| | | client_type=socket_manager.ClientSocketManager.CLIENT_TYPE_TRADE_SELL) |
| | | return_str = json.dumps(result) |
| | | elif type_ == "get_l1_data": |
| | | results = L1DataManager().get_current_l1_data() |
| | | return_str = json.dumps({"code": 0, "data": results}) |
| | | elif type_ == "set_l1_codes": |
| | | codes = data_json["data"]["codes"] |
| | | # 将代码暂存到本地 |
| | | # 设置L1代码 |
| | | L1DataManager().save_target_codes(codes) |
| | | return_str = json.dumps({"code": 0, "data": {}}) |
| | | finally: |
| | | log.request_info("middle_api_server", f"请求结束:{type_}") |
| | | request_log_util.request_info("middle_api_server", f"请求结束:{type_}") |
| | | break |
| | | # sk.close() |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | logger_request_debug.exception(e) |
| | | return_str = json.dumps({"code": 401, "msg": str(e)}) |
| | | break |
| | | finally: |
| | |
| | | return_str = json.dumps({"code": 1, "msg": "不可以取消"}) |
| | | |
| | | elif type == 421: |
| | | # 加入暂不买 |
| | | # 加红 |
| | | data = json.loads(_str) |
| | | codes = data["data"]["codes"] |
| | | for code in codes: |
| | |
| | | return_str = json.dumps({"code": 0}) |
| | | |
| | | elif type == 422: |
| | | # 移除暂不买 |
| | | # 移红 |
| | | data = json.loads(_str) |
| | | codes = data["data"]["codes"] |
| | | for code in codes: |
| | |
| | | return_str = json.dumps({"code": 0}) |
| | | |
| | | elif type == 423: |
| | | # 暂不买列表 |
| | | # 红单列表 |
| | | result = hosting_api_util.get_code_list(hosting_api_util.CODE_LIST_MUST_BUY) |
| | | return_str = json.dumps(result) |
| | | |
| | | elif type == 441: |
| | | # 加绿 |
| | | data = json.loads(_str) |
| | | codes = data["data"]["codes"] |
| | | for code in codes: |
| | | hosting_api_util.add_code_list(code, hosting_api_util.CODE_LIST_GREEN) |
| | | return_str = json.dumps({"code": 0}) |
| | | |
| | | elif type == 442: |
| | | # 移绿 |
| | | data = json.loads(_str) |
| | | codes = data["data"]["codes"] |
| | | for code in codes: |
| | | hosting_api_util.remove_code_list(code, hosting_api_util.CODE_LIST_GREEN) |
| | | return_str = json.dumps({"code": 0}) |
| | | |
| | | elif type == 443: |
| | | # 绿单列表 |
| | | result = hosting_api_util.get_code_list(hosting_api_util.CODE_LIST_GREEN) |
| | | return_str = json.dumps(result) |
| | | |
| | | |
| | | elif type == 430: |
| | | # 查询代码属性 |