Administrator
2023-09-12 09ca670240a660b81d8dbed4c54efec96e99e811
下单日志优化
4个文件已修改
46 ■■■■ 已修改文件
huaxin_client/command_manager.py 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
huaxin_client/trade_client.py 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_api.py 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/trade_huaxin.py 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
huaxin_client/command_manager.py
@@ -9,7 +9,8 @@
from huaxin_client import socket_util
from huaxin_client.client_network import SendResponseSkManager
from log_module.log import logger_local_huaxin_trade_debug
from log_module import async_log_util
from log_module.log import logger_local_huaxin_trade_debug, logger_trade
MSG_TYPE_HEART = "heart"
# 命令信息
@@ -87,19 +88,21 @@
        try:
            data = result_json["data"]
            print("接收内容", result_json)
            request_id = result_json.get('request_id')
            if not socket_util.is_client_params_sign_right(result_json):
                print("签名错误")
                # 签名出错
                SendResponseSkManager.send_error_response(_type, request_id, client_id,
                                                          {"code": -1, "msg": "签名错误"})
                return
            # 暂时取消签名
            # if not socket_util.is_client_params_sign_right(result_json):
            #     print("签名错误")
            #     # 签名出错
            #     SendResponseSkManager.send_error_response(_type, request_id, client_id,
            #                                               {"code": -1, "msg": "签名错误"})
            #     return
            if _type == CLIENT_TYPE_TRADE:
                # 交易
                ctype = data["trade_type"]
                async_log_util.info(logger_trade, f"交易开始")
                cls.action_callback.OnTrade(client_id, request_id, sk, ctype, data)
                async_log_util.info(logger_trade, f"交易结束")
            elif _type == CLIENT_TYPE_MONEY:
                cls.action_callback.OnMoney(client_id, request_id, sk)
            elif _type == CLIENT_TYPE_DEAL_LIST:
@@ -127,7 +130,7 @@
                    val = pipe_strategy.recv()
                    if val:
                        val = json.loads(val)
                        print("run_process_command", val)
                        # print("run_process_command", val)
                        _type = val["type"]
                        threading.Thread(target=lambda: cls.process_command(_type, None, val), daemon=True).start()
                except Exception as e:
huaxin_client/trade_client.py
@@ -15,7 +15,7 @@
# 正式账号
from huaxin_client.trade_transform_protocol import TradeRequest, TradeResponse
from log_module import async_log_util
from log_module.log import logger_local_huaxin_trade_debug, logger_system
from log_module.log import logger_local_huaxin_trade_debug, logger_system, logger_trade
UserID = '388000013349'
# 登陆密码
@@ -89,6 +89,7 @@
            return
        if sinfo in self.__buy_sinfo_set:
            raise Exception(f'下单请求已经提交:{sinfo}')
        async_log_util.info(logger_trade, f"{code}华鑫本地真实下单开始")
        async_log_util.info(logger_local_huaxin_trade_debug,
                            f"进入买入方法:code-{code} sinfo-{sinfo}")
        self.__buy_sinfo_set.add(sinfo)
@@ -148,6 +149,7 @@
        # 给L2发送消息
        if l2pipe is not None:
            l2pipe.send(json.dumps({"type": "listen_volume", "data": {"code": code, "volume": count}}).encode('utf-8'))
        async_log_util.info(logger_trade, f"{code}华鑫本地真实下单结束")
        return
    # 撤买
@@ -711,7 +713,6 @@
    def OnRspQryOrder(self, pOrderField: "CTORATstpOrderField", pRspInfoField: "CTORATstpRspInfoField",
                      nRequestID: "int", bIsLast: "bool") -> "void":
        try:
            print('查询报单回调', bIsLast)
            if nRequestID not in self.__temp_order_list_dict:
                self.__temp_order_list_dict[nRequestID] = []
            if not bIsLast:
@@ -820,12 +821,15 @@
            price = data["price"]
            sinfo = data["sinfo"]
            local_order_id = data.get("local_order_id")
            if direction == 1:
                async_log_util.info(logger_trade, f"{code}华鑫本地开始下单")
                # 买
                try:
                    req_rid_dict[sinfo] = (client_id, request_id, sk, local_order_id)
                    threading.Thread(target=lambda: self.__tradeSimpleApi.buy(code, volume, price, sinfo),
                                     daemon=True).start()
                    async_log_util.info(logger_trade, f"{code}华鑫本地下单线程结束")
                except Exception as e:
                    send_response(json.dumps({"code": 1, "msg": str(e)}), TYPE_ORDER, client_id,
                                  request_id)
@@ -880,7 +884,7 @@
    def OnDealList(self, client_id, request_id, sk):
        async_log_util.info(logger_local_huaxin_trade_debug, f"请求成交列表:client_id-{client_id} request_id-{request_id}")
        try:
            print("开始请求成交列表")
            # print("开始请求成交列表")
            req_id = self.__tradeSimpleApi.list_traded_orders()
            req_rid_dict[req_id] = (client_id, request_id, sk)
        except Exception as e:
@@ -1047,9 +1051,7 @@
    if type == TYPE_ORDER or type == TYPE_CANCEL_ORDER:
        key = data["sinfo"]
    try:
        print("traderapi_callback", req_rid_dict)
        if req_rid_dict and key in req_rid_dict:
            print("API回调")
            temp_params = req_rid_dict.pop(key)
            client_id, request_id = temp_params[0], temp_params[1]
            # 本地订单号-系统订单号映射
@@ -1068,10 +1070,8 @@
                 "request_id": request_id})
            async_log_util.info(logger_local_huaxin_trade_debug, "API回调结束 req_id-{} request_id-{}", req_id, request_id)
            print("API回调结束")
        else:
            async_log_util.info(logger_local_huaxin_trade_debug, "非API回调 req_id-{}", req_id)
            print("非API回调")
            trade_response.OnTradeCallback({"type": "trade_callback", "data": {"code": 0, "data": data, "type": type}})
            # # 非API回调
            # send_response(
@@ -1079,7 +1079,6 @@
            #     type,
            #     None,
            #     req_id)
            print("非API结束")
    except Exception as e:
        logging.exception(e)
trade/huaxin/huaxin_trade_api.py
@@ -7,7 +7,6 @@
import threading
import time
from huaxin_client import trade_client_server
from huaxin_client.trade_transform_protocol import TradeRequest
from log_module import async_log_util
from log_module.log import hx_logger_trade_debug, hx_logger_trade_loop, hx_logger_trade_callback, logger_trade
@@ -34,9 +33,7 @@
            try:
                val = pipe_trade.recv()
                if val:
                    print("收到来自交易服务器的数据:", val)
                    data_json = json.loads(val)
                    print("收到来自交易客户端的数据:", data_json["type"])
                    # 处理数据
                    type_ = data_json["type"]
                    if type_ == "response":
@@ -327,7 +324,6 @@
    timestamp = round(time.time() * 1000)
    if not sinfo:
        sinfo = f"b_{code}_{timestamp}"
    print("客户端", ClientSocketManager.socket_client_dict)
    local_order_id = f"l_{code}_{direction}_{volume}_{timestamp}_{random.randint(0, 999)}"
    request_id = __request(ClientSocketManager.CLIENT_TYPE_TRADE,
                           {"type": ClientSocketManager.CLIENT_TYPE_TRADE, "trade_type": 1,
trade/trade_huaxin.py
@@ -34,6 +34,7 @@
# 通过量下单,返回(代码,账号ID,订单号)
def order_volume(code, price, count, last_data_index):
    async_log_util.info(logger_trade, f"{code}下单方法开始")
    price = round(float(price), 2)
    if code.find("00") != 0 and code.find("60") != 0:
        raise Exception("只支持00开头与60开头的代码下单")
@@ -45,9 +46,9 @@
    result = None
    blocking = False
    try:
        async_log_util.info(logger_trade, f"{code}下单开始")
        result = huaxin_trade_api.order(1, code, count, price, blocking=blocking)
        print("华鑫下单耗时", time.time() - start_time)
        async_log_util.info(hx_logger_trade_debug, f"{code}:下单耗时{round(time.time() - start_time, 3)}s")
        async_log_util.info(logger_trade, f"{code}下单结束")
    except Exception as e:
        if str(e).find("超时") >= 0:
            # 此处出现超时,需要通过读取委托列表来设置订单信息
@@ -56,7 +57,6 @@
            raise e
    if result:
        print("下单结果", result)
        if blocking:
            if result['code'] == 0:
                result = result["data"]