| | |
| | | self.conn.commit() |
| | | except Exception as e: |
| | | print("提交出错\n:", e) |
| | | logging.exception(e) |
| | | # logging.exception(e) |
| | | # 如果出错要回滚 |
| | | self.conn.rollback() |
| | | |
| | |
| | | filter=lambda record: record["extra"].get("name") == "request_debug", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | logger.add(self.get_path("request", "tuoguan_request_debug"), |
| | | filter=lambda record: record["extra"].get("name") == "tuoguan_request_debug", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | def get_path(self, dir_name, log_name): |
| | | path_str = "{}/logs/gp/{}/{}".format(constant.get_path_prefix(), dir_name, log_name) + ".{time:YYYY-MM-DD}.log" |
| | | # print(path_str) |
| | |
| | | |
| | | logger_request_debug = __mylogger.get_logger("request_debug") |
| | | |
| | | logger_tuoguan_request_debug = __mylogger.get_logger("tuoguan_request_debug") |
| | | |
| | | |
| | | |
| | | |
| | | class AsyncLogManager: |
| | | __log_queue = queue.Queue() |
| | |
| | | t1.start() |
| | | t1 = threading.Thread(target=lambda: log.async_log_util.run_sync(), daemon=True) |
| | | t1.start() |
| | | t1 = threading.Thread(target=lambda: middle_server.run(12880), daemon=True) |
| | | t1.start() |
| | | middle_server.run() |
| | |
| | | encoding='utf-8'))) |
| | | continue |
| | | type_ = data_json["type"] |
| | | log.request_info("middle_server", f"请求开始:{type_}") |
| | | __start_time = time.time() |
| | | try: |
| | | if data_json["type"] == 'register': |
| | | client_type = data_json["data"]["client_type"] |
| | |
| | | push_msg_manager.push_msg(_type, data) |
| | | result_str = json.dumps({"code": 0, "data": {}}) |
| | | sk.sendall(socket_util.load_header(result_str.encode(encoding='utf-8'))) |
| | | except Exception as e: |
| | | log.logger_tuoguan_request_debug.exception(e) |
| | | finally: |
| | | log.request_info("middle_server", f"请求结束") |
| | | |
| | | if time.time() - __start_time > 2: |
| | | log.logger_tuoguan_request_debug.info(f"耗时:{int(time.time() - __start_time)}s 数据:{data_json}") |
| | | else: |
| | | # 断开连接 |
| | | break |
| | | # sk.close() |
| | | except Exception as e: |
| | | # log.logger_tuoguan_request_debug.exception(e) |
| | | logging.exception(e) |
| | | break |
| | | |
| | |
| | | pass |
| | | |
| | | |
| | | def run(): |
| | | def run(port = constant.MIDDLE_SERVER_PORT): |
| | | print("create MiddleServer") |
| | | t1 = threading.Thread(target=lambda: clear_invalid_client(), daemon=True) |
| | | t1.start() |
| | | |
| | | laddr = "0.0.0.0", constant.MIDDLE_SERVER_PORT |
| | | laddr = "0.0.0.0", port |
| | | print("MiddleServer is at: http://%s:%d/" % (laddr)) |
| | | tcpserver = MyThreadingTCPServer(laddr, MyBaseRequestHandle) # 注意:参数是MyBaseRequestHandle |
| | | tcpserver.serve_forever() |
| | |
| | | # 客户端类型 |
| | | CLIENT_TYPE_COMMON = "common" |
| | | CLIENT_TYPE_TRADE = "trade" |
| | | CLIENT_TYPE_TRADE_SELL = "trade_sell" |
| | | |
| | | socket_client_dict = {} |
| | | socket_client_lock_dict = {} |
| | |
| | | # blocking是否阻塞进程 |
| | | def trade_order(direction, code, volume, price, price_type=2, blocking=True): |
| | | print("客户端", ClientSocketManager.socket_client_dict) |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, |
| | | {"type": API_TYPE_TRADE, "trade_type": 1, |
| | | "direction": direction, |
| | | "code": code, |
| | | "volume": volume, |
| | | "price_type": price_type, |
| | | "price": price, "sinfo": f"order_{code}_{round(time.time() * 1000)}"}) |
| | | request_id, client = __request( |
| | | ClientSocketManager.CLIENT_TYPE_TRADE if direction == 1 else ClientSocketManager.CLIENT_TYPE_TRADE_SELL, |
| | | {"type": API_TYPE_TRADE, "trade_type": 1, |
| | | "direction": direction, |
| | | "code": code, |
| | | "volume": volume, |
| | | "price_type": price_type, |
| | | "price": price, "sinfo": f"order_{code}_{round(time.time() * 1000)}"}) |
| | | |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | |
| | | |
| | | # 保存正在运行的数据 |
| | | def sell_rule(operate, data={}, blocking=True): |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_SELL, |
| | | {"type": API_TYPE_SELL_RULE, "operate": operate, "data": data, |
| | | "sinfo": f"cb_{API_TYPE_SELL_RULE}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking, timeout=10) |
| | |
| | | |
| | | # 获取代码持仓信息 |
| | | def get_code_position_info(code, blocking=True): |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_SELL, |
| | | {"type": API_TYPE_GET_CODE_POSITION_INFO, "code": code, |
| | | "sinfo": f"cb_{API_TYPE_GET_CODE_POSITION_INFO}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking) |
| | |
| | | class DelegateRecordManager: |
| | | key_list = ["id", "orderLocalID", "securityID", "securityName", "direction", "orderSysID", "insertTime", |
| | | "insertDate", "acceptTime", "cancelTime", "limitPrice", "turnover", "volume", "volumeTraded", |
| | | "orderStatus", "orderSubmitStatus", "statusMsg", "createTime", "updateTime", "accountID"] |
| | | "orderStatus", "orderSubmitStatus", "statusMsg", "createTime", "updateTime", "accountID", "orderRef"] |
| | | |
| | | @classmethod |
| | | def add(cls, datas): |