From 726e083845c4cfe2d50f381ee09336136be3e01e Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期四, 16 五月 2024 18:21:32 +0800 Subject: [PATCH] 可转债接口 --- utils/hosting_api_util.py | 99 ++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 84 insertions(+), 15 deletions(-) diff --git a/utils/hosting_api_util.py b/utils/hosting_api_util.py index 0565187..8bd4aef 100644 --- a/utils/hosting_api_util.py +++ b/utils/hosting_api_util.py @@ -97,6 +97,12 @@ raise Exception(f"璇诲彇鍐呭瓒呮椂: request_id={request_id}") finally: pass + else: + try: + # 濡傛灉涓嶆槸闃诲璇锋眰闇�瑕侀噴鏀惧鎴风 + ClientSocketManager.release_client(client[0]) + except: + pass return None @@ -131,13 +137,14 @@ # 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) @@ -229,8 +236,8 @@ # 鍒锋柊浜ゆ槗鏁版嵁 -def refresh_trade_data(type, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, +def refresh_trade_data(type, blocking=True, client_type=ClientSocketManager.CLIENT_TYPE_COMMON): + request_id, client = __request(client_type, {"type": API_TYPE_REFRESH_TRADE_DATA, "ctype": type, "sinfo": f"cb_{API_TYPE_REFRESH_TRADE_DATA}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) @@ -310,7 +317,7 @@ # 淇濆瓨姝e湪杩愯鐨勬暟鎹� 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) @@ -318,22 +325,84 @@ # 鑾峰彇浠g爜鎸佷粨淇℃伅 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) -def common_request(params={}, blocking=True): +def common_request(params={}, blocking=True, client_type=ClientSocketManager.CLIENT_TYPE_COMMON): data = {"type": API_TYPE_COMMON_REQUEST, "sinfo": f"cb_{API_TYPE_COMMON_REQUEST}_{round(time.time() * 1000)}"} if params: for k in params: data[k] = params[k] - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, data) + request_id, client = __request(client_type, data) + return __read_response(client, request_id, blocking, timeout=10) + + +###############鍙浆鍊�################# +def trade_order_for_cb(direction, code, volume, price, price_type=2, blocking=True): + """ + 浜ゆ槗 + :param direction: 1-涔� 2-鍗� + :param code: 浠g爜 + :param volume: 閲� + :param price: 浠锋牸 + :param price_type: 浠锋牸绫诲瀷 + :param blocking: 鏄惁闃诲 + :return: + """ + request_id, client = __request( + ClientSocketManager.CLIENT_TYPE_TRADE_CB, + {"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 get_position_for_cb(code=None, blocking=True): + """ + 鑾峰彇鎸佷粨 + :param code: + :param blocking: + :return: + """ + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, + {"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) + + +def get_account_money_for_cb(blocking=True): + """ + 鑾峰彇璐︽埛璧勯噾鎯呭喌 + :param code: + :param blocking: + :return: + """ + return common_request_for_cb({"ctype": "get_account_money"}, blocking) + + +def common_request_for_cb(params, blocking=True): + """ + 閫氱敤璇锋眰 + :param params: + :param blocking: + :return: + """ + data = {"type": API_TYPE_COMMON_REQUEST, + "sinfo": f"cb_{API_TYPE_COMMON_REQUEST}_{round(time.time() * 1000)}"} + if params: + for k in params: + data[k] = params[k] + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, data) return __read_response(client, request_id, blocking, timeout=10) if __name__ == "__main__": - d = {"id": "123123"} - print(d.pop("id")) + pass -- Gitblit v1.8.0