| | |
| | | |
| | | 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" |
| | | # 命令信息 |
| | |
| | | |
| | | 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: |
| | |
| | | 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: |
| | |
| | | # 正式账号 |
| | | 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' |
| | | # 登陆密码 |
| | |
| | | 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) |
| | |
| | | # 给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 |
| | | |
| | | # 撤买 |
| | |
| | | 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: |
| | |
| | | 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) |
| | |
| | | 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: |
| | |
| | | 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] |
| | | # 本地订单号-系统订单号映射 |
| | |
| | | "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( |
| | |
| | | # type, |
| | | # None, |
| | | # req_id) |
| | | print("非API结束") |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | |
| | | 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 |
| | |
| | | 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": |
| | |
| | | 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, |
| | |
| | | |
| | | # 通过量下单,返回(代码,账号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开头的代码下单") |
| | |
| | | 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: |
| | | # 此处出现超时,需要通过读取委托列表来设置订单信息 |
| | |
| | | raise e |
| | | |
| | | if result: |
| | | print("下单结果", result) |
| | | if blocking: |
| | | if result['code'] == 0: |
| | | result = result["data"] |