Administrator
2023-07-20 a21a08292fad35745037f979f3855b80e1fd53b0
bug修复
2个文件已修改
139 ■■■■■ 已修改文件
trade/huaxin/huaxin_trade_api.py 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/trade_api_server.py 112 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_api.py
@@ -47,6 +47,13 @@
            cls.socket_client_dict[_type] = (rid, sk)
            cls.socket_client_lock_dict[rid] = threading.Lock()
    # 是否已经被锁住
    @classmethod
    def is_client_locked(cls, rid):
        if rid in cls.socket_client_lock_dict:
            return cls.socket_client_lock_dict[rid].locked()
        return None
    @classmethod
    def acquire_client(cls, _type):
        if _type == cls.CLIENT_TYPE_TRADE:
@@ -164,15 +171,17 @@
def __read_response(client, request_id, blocking, timeout=TIMEOUT):
    if blocking:
        start_time = time.time()
        while True:
            time.sleep(0.01)
            if request_id in __request_response_dict:
                # 获取到了响应内容
                result = __request_response_dict.pop(request_id)
                return result
            if time.time() - start_time > timeout:
                ClientSocketManager.release_client(client[0])
                raise Exception(f"读取内容超时: request_id={request_id}")
        try:
            while True:
                time.sleep(0.01)
                if request_id in __request_response_dict:
                    # 获取到了响应内容
                    result = __request_response_dict.pop(request_id)
                    return result
                if time.time() - start_time > timeout:
                    raise Exception(f"读取内容超时: request_id={request_id}")
        finally:
            ClientSocketManager.release_client(client[0])
    return None
trade/huaxin/trade_api_server.py
@@ -289,8 +289,20 @@
                                 huaxin_trade_api.ClientSocketManager.CLIENT_TYPE_POSITION_LIST]
                        fdata = {}
                        for t in types:
                            trade_list = huaxin_trade_api.ClientSocketManager.list_client(t)
                            fdata[t] = len(trade_list)
                            client_list = huaxin_trade_api.ClientSocketManager.list_client(t)
                            client_state_list = []
                            for t in client_list:
                                # 判断是否已经上锁
                                lock_state = huaxin_trade_api.ClientSocketManager.is_client_locked(t[0])
                                lock_state_desc = ""
                                if lock_state is None:
                                    lock_state_desc = "未知"
                                elif lock_state:
                                    lock_state_desc = "已锁"
                                else:
                                    lock_state_desc = "未锁"
                                client_state_list.append((t[0],lock_state_desc))
                            fdata[t] = client_state_list
                        return_str = json.dumps(
                            {"code": 0, "data": fdata, "msg": ""})
                    elif type_ == 'juejin_is_valid':
@@ -355,55 +367,61 @@
            if data:
                type_ = data["type"]
                hx_logger_trade_debug.info(f"获取交易数据开始:{type_}")
                if type_ == "delegate_list":
                    dataJSON = huaxin_trade_api.get_delegate_list(can_cancel=False, timeout=5)
                    print("获取委托列表", dataJSON)
                    if dataJSON["code"] == 0:
                        data = dataJSON["data"]
                        huaxin_trade_record_manager.DelegateRecordManager.add(data)
                        # 是否可以撤单
                        if data:
                            codes = []
                            for d in data:
                                if huaxin_util.is_can_cancel(d["orderStatus"]):
                                    codes.append(d["securityID"])
                            if codes:
                try:
                    if type_ == "delegate_list":
                        dataJSON = huaxin_trade_api.get_delegate_list(can_cancel=False, timeout=5)
                        print("获取委托列表", dataJSON)
                        if dataJSON["code"] == 0:
                            data = dataJSON["data"]
                            huaxin_trade_record_manager.DelegateRecordManager.add(data)
                            # 是否可以撤单
                            if data:
                                codes = []
                                for d in data:
                                    if huaxin_util.is_can_cancel(d["orderStatus"]):
                                        codes.append(d["securityID"])
                                if codes:
                                    try:
                                        trade_manager.process_trade_delegate_data([{"code": c} for c in codes])
                                    except Exception as e:
                                        logging.exception(e)
                    elif type_ == "money":
                        dataJSON = huaxin_trade_api.get_money()
                        if dataJSON["code"] == 0:
                            data = dataJSON["data"]
                            huaxin_trade_record_manager.MoneyManager.save_data(data)
                            if data:
                                usefulMoney = data[0]["usefulMoney"]
                                # 设置可用资金
                                trade_manager.set_available_money(0, usefulMoney)
                            # 设置可用资金
                    elif type_ == "deal_list":
                        dataJSON = huaxin_trade_api.get_deal_list()
                        print("成交响应:", dataJSON)
                        if dataJSON["code"] == 0:
                            datas = dataJSON["data"]
                            huaxin_trade_record_manager.DealRecordManager.add(datas)
                            if datas:
                                tempList = [
                                    {"time": d["tradeTime"], "type": int(d['direction']), "code": d['securityID']}
                                    for d in datas]
                                try:
                                    trade_manager.process_trade_delegate_data([{"code": c} for c in codes])
                                    trade_manager.process_trade_success_data(tempList)
                                except Exception as e:
                                    logging.exception(e)
                elif type_ == "money":
                    dataJSON = huaxin_trade_api.get_money()
                    if dataJSON["code"] == 0:
                        data = dataJSON["data"]
                        huaxin_trade_record_manager.MoneyManager.save_data(data)
                        if data:
                            usefulMoney = data[0]["usefulMoney"]
                            # 设置可用资金
                            trade_manager.set_available_money(0, usefulMoney)
                        # 设置可用资金
                elif type_ == "deal_list":
                    dataJSON = huaxin_trade_api.get_deal_list()
                    print("成交响应:", dataJSON)
                    if dataJSON["code"] == 0:
                        datas = dataJSON["data"]
                        huaxin_trade_record_manager.DealRecordManager.add(datas)
                        if datas:
                            tempList = [{"time": d["tradeTime"], "type": int(d['direction']), "code": d['securityID']}
                                        for d in datas]
                            try:
                                trade_manager.process_trade_success_data(tempList)
                            except Exception as e:
                                logging.exception(e)
                # 持仓股
                elif type_ == "position_list":
                    dataJSON = huaxin_trade_api.get_position_list()
                    if dataJSON["code"] == 0:
                        data = dataJSON["data"]
                        huaxin_trade_record_manager.PositionManager.add(data)
                    # 持仓股
                    elif type_ == "position_list":
                        dataJSON = huaxin_trade_api.get_position_list()
                        if dataJSON["code"] == 0:
                            data = dataJSON["data"]
                            huaxin_trade_record_manager.PositionManager.add(data)
                hx_logger_trade_debug.info(f"获取交易数据成功:{type_}")
                    hx_logger_trade_debug.info(f"获取交易数据成功:{type_}")
                except Exception as e1:
                    if str(e1).find("超时") >= 0:
                        # 读取结果超时需要重新请求
                        trade_data_request_queue.put_nowait({"type": type_})
                    raise e1
        except Exception as e:
            hx_logger_trade_debug.exception(e)
        finally: