| | |
| | | t1.start() |
| | | |
| | | |
| | | # 交易通道的错误次数 |
| | | trade_pipe_channel_error_count = 0 |
| | | |
| | | |
| | | # pipe的交易通道是否正常 |
| | | def is_pipe_channel_normal(): |
| | | return trade_pipe_channel_error_count < 3 |
| | | |
| | | |
| | | # 测试交易通道 |
| | | def test_trade_channel(): |
| | | global trade_pipe_channel_error_count |
| | | sid = random.randint(0, 1000000) |
| | | result = __test_trade_channel(sid) |
| | | if result["code"] == 0 and result["data"]["data"]["sid"] == sid: |
| | | trade_pipe_channel_error_count = 0 |
| | | return True |
| | | trade_pipe_channel_error_count += 1 |
| | | if trade_pipe_channel_error_count > 100: |
| | | trade_pipe_channel_error_count = 100 |
| | | return False |
| | | |
| | | |
| | |
| | | |
| | | |
| | | # 网络请求 |
| | | def __request(_type, data, request_id=None, blocking=False): |
| | | def __request(_type, data, request_id=None, blocking=False, is_pipe=True): |
| | | if not request_id: |
| | | request_id = __get_request_id(_type) |
| | | try: |
| | | async_log_util.info(hx_logger_trade_loop, "请求发送开始:client_id-{} request_id-{}", 0, request_id) |
| | | async_log_util.info(hx_logger_trade_loop, "请求发送开始:client_id-{} request_id-{} is_pipe-{}", 0, request_id, is_pipe) |
| | | root_data = {"type": _type, |
| | | "data": data, |
| | | "request_id": request_id} |
| | | root_data = socket_util.encryp_client_params_sign(root_data) |
| | | # TODO 测试 |
| | | sk = socket_util.create_socket("127.0.0.1", trade_client_server.SERVER_PORT) |
| | | if True: |
| | | |
| | | if not is_pipe: |
| | | sk = socket_util.create_socket("127.0.0.1", trade_client_server.SERVER_PORT) |
| | | sk.sendall(socket_util.load_header(json.dumps(root_data).encode("utf-8"))) |
| | | try: |
| | | if blocking: |
| | |
| | | "local_order_id": local_order_id, |
| | | "volume": volume, |
| | | "price_type": price_type, |
| | | "price": price, "sinfo": sinfo}, request_id=request_id, blocking=blocking) |
| | | "price": price, "sinfo": sinfo}, request_id=request_id, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | if blocking: |
| | | try: |
| | | return __read_response(request_id, blocking) |
| | |
| | | "direction": direction, |
| | | "code": code, |
| | | "localOrderID": localOrderID, |
| | | "orderSysID": orderSysID, "sinfo": sinfo}, request_id=request_id, blocking=blocking) |
| | | "orderSysID": orderSysID, "sinfo": sinfo}, request_id=request_id, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | try: |
| | | return __read_response(request_id, blocking) |
| | | finally: |
| | |
| | | def get_delegate_list(can_cancel=True, blocking=True, timeout=TIMEOUT): |
| | | request_id = __request(ClientSocketManager.CLIENT_TYPE_DELEGATE_LIST, |
| | | {"type": ClientSocketManager.CLIENT_TYPE_DELEGATE_LIST, |
| | | "can_cancel": 1 if can_cancel else 0}, blocking=blocking) |
| | | "can_cancel": 1 if can_cancel else 0}, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | |
| | | return __read_response(request_id, blocking, timeout=timeout) |
| | | |
| | |
| | | # 获取成交列表 |
| | | def get_deal_list(blocking=True): |
| | | request_id = __request(ClientSocketManager.CLIENT_TYPE_DEAL_LIST, |
| | | {"type": ClientSocketManager.CLIENT_TYPE_DEAL_LIST}, blocking=blocking) |
| | | {"type": ClientSocketManager.CLIENT_TYPE_DEAL_LIST}, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | return __read_response(request_id, blocking) |
| | | |
| | | |
| | | # 获取持仓列表 |
| | | def get_position_list(blocking=True): |
| | | request_id = __request(ClientSocketManager.CLIENT_TYPE_POSITION_LIST, |
| | | {"type": ClientSocketManager.CLIENT_TYPE_POSITION_LIST}, blocking=blocking) |
| | | {"type": ClientSocketManager.CLIENT_TYPE_POSITION_LIST}, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | return __read_response(request_id, blocking) |
| | | |
| | | |
| | | # 获取账户资金状况 |
| | | def get_money(blocking=True): |
| | | request_id = __request(ClientSocketManager.CLIENT_TYPE_MONEY, |
| | | {"type": ClientSocketManager.CLIENT_TYPE_MONEY}, blocking=blocking) |
| | | {"type": ClientSocketManager.CLIENT_TYPE_MONEY}, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | return __read_response(request_id, blocking) |
| | | |
| | | |
| | | # 设置L2订阅数据 |
| | | def set_l2_codes_data(codes_data, blocking=True): |
| | | request_id = __request(ClientSocketManager.CLIENT_TYPE_CMD_L2, |
| | | {"type": ClientSocketManager.CLIENT_TYPE_CMD_L2, "data": codes_data}, blocking=blocking) |
| | | {"type": ClientSocketManager.CLIENT_TYPE_CMD_L2, "data": codes_data}, blocking=blocking, is_pipe=is_pipe_channel_normal()) |
| | | return __read_response(request_id, blocking) |
| | | |
| | | |