From 287c506725b2d970f721f80169f83c2418cb0991 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 04 六月 2025 18:39:02 +0800 Subject: [PATCH] 添加新版低吸中间服务器 --- utils/hosting_api_util.py | 261 +++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 229 insertions(+), 32 deletions(-) diff --git a/utils/hosting_api_util.py b/utils/hosting_api_util.py index 20e91fc..8ddf2ed 100644 --- a/utils/hosting_api_util.py +++ b/utils/hosting_api_util.py @@ -16,17 +16,20 @@ OPERRATE_SET = 1 # 璁剧疆 OPERRATE_DELETE = 2 # 鍒犻櫎 OPERRATE_GET = 3 # 鑾峰彇 +OPERRATE_ADD = 4 # 娣诲姞 # 浠g爜鍚嶅崟绫诲瀷 CODE_LIST_WHITE = "white" CODE_LIST_BLACK = "black" CODE_LIST_WANT = "want" CODE_LIST_PAUSE_BUY = "pause_buy" - +CODE_LIST_MUST_BUY = "must_buy" +CODE_LIST_GREEN = "green" # 绫诲瀷 API_TYPE_TRADE = "trade" # 浜ゆ槗 API_TYPE_TRADE_STATE = "trade_state" # 浜ゆ槗鐘舵�� API_TYPE_TRADE_MODE = "trade_mode" # 浜ゆ槗妯″紡 +API_TYPE_SELL_RULE = "sell_rule" # 鍗栧嚭瑙勫垯 API_TYPE_CODE_LIST = "code_list" # 浠g爜鍚嶅崟 API_TYPE_EXPORT_L2 = "export_l2" # 瀵煎嚭L2鏁版嵁 API_TYPE_INIT = "init" # 鍒濆鍖� @@ -34,8 +37,16 @@ API_TYPE_CODE_ATRRIBUTE = "code_attribute" # 浠g爜灞炴�� API_TYPE_CODE_TRADE_STATE = "code_trade_state" # 浠g爜浜ゆ槗鐘舵�� API_TYPE_GET_ENV = "get_env" # 鑾峰彇鐜淇℃伅 +API_TYPE_SYNC_L1_TARGET_CODES = "sync_l1_subscript_codes" # 鍚屾L1闇�瑕佽闃呯殑浠g爜 +API_TYPE_SYSTEM_LOG = "system_log" # 绯荤粺鏃ュ織 +API_TYPE_GET_FROM_DATA_SERVER = "get_from_data_server" # 浠庢暟鎹湇鍔″櫒鎷夊彇鏁版嵁 +API_TYPE_CODE_TRADE_INFO = "code_trade_info" # 浠g爜浜ゆ槗淇℃伅 +API_TYPE_CODE_L2_LISTEN_ACTIVE_COUNT = "l2_listen_active_count" # L2鏈夋晥鐩戝惉鏁伴噺 +API_TYPE_SAVE_RUNNING_DATA = "save_running_data" # 淇濆瓨杩愯鏃舵暟鎹� +API_TYPE_GET_CODE_POSITION_INFO = "get_code_position_info" # 鑾峰彇浠g爜鎸佷粨淇℃伅 +API_TYPE_COMMON_REQUEST = "common_request" # 閫氱敤璇锋眰 # 瓒呮椂鏃堕棿2s -TIMEOUT = 2.0 +TIMEOUT = 5.0 # 绛夊緟鍝嶅簲鐨剅equest_id __request_response_dict = {} @@ -87,6 +98,12 @@ raise Exception(f"璇诲彇鍐呭瓒呮椂: request_id={request_id}") finally: pass + else: + try: + # 濡傛灉涓嶆槸闃诲璇锋眰闇�瑕侀噴鏀惧鎴风 + ClientSocketManager.release_client(client[0]) + except: + pass return None @@ -121,13 +138,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) @@ -140,13 +158,13 @@ "code": code, "accountID": accountID, "orderSysID": orderSysID, - "sinfo": f"cancel_order_{code}_{round(time.time() * 1000)}"}) + "sinfo": f"cb_{code}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) # 璁剧疆浜ゆ槗鐘舵�� def set_trade_state(state, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_TRADE_STATE, "operate": OPERRATE_SET, "state": state, "sinfo": f"cb_{API_TYPE_TRADE_STATE}_{round(time.time() * 1000)}"}) @@ -155,7 +173,7 @@ # 鑾峰彇浜ゆ槗鐘舵�� def get_trade_state(blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_TRADE_STATE, "operate": OPERRATE_GET, "sinfo": f"cb_{API_TYPE_TRADE_STATE}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) @@ -163,7 +181,7 @@ # 璁剧疆浜ゆ槗妯″紡 def set_trade_mode(mode, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_TRADE_MODE, "operate": OPERRATE_SET, "mode": mode, "sinfo": f"cb_{API_TYPE_TRADE_MODE}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) @@ -171,7 +189,7 @@ # 鑾峰彇浜ゆ槗妯″紡 def get_trade_mode(blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_TRADE_MODE, "operate": OPERRATE_GET, "sinfo": f"cb_{API_TYPE_TRADE_MODE}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) @@ -179,7 +197,7 @@ # -----浠g爜鍚嶅崟鎿嶄綔---- def add_code_list(code, code_list_type, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_CODE_LIST, "code_list_type": code_list_type, "code": code, "operate": OPERRATE_SET, "sinfo": f"cb_{API_TYPE_CODE_LIST}_{round(time.time() * 1000)}"}) @@ -187,7 +205,7 @@ def remove_code_list(code, code_list_type, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_CODE_LIST, "code_list_type": code_list_type, "code": code, "operate": OPERRATE_DELETE, "sinfo": f"cb_{API_TYPE_CODE_LIST}_{round(time.time() * 1000)}"}) @@ -195,7 +213,7 @@ def get_code_list(code_list_type, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_CODE_LIST, "code_list_type": code_list_type, "operate": OPERRATE_GET, "sinfo": f"cb_{API_TYPE_CODE_LIST}_{round(time.time() * 1000)}"}) @@ -204,52 +222,231 @@ # -----瀵煎嚭L2鏁版嵁---- def export_l2_data(code, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_EXPORT_L2, "code": code, - "sinfo": f"cb_{API_TYPE_CODE_LIST}_{round(time.time() * 1000)}"}) + "sinfo": f"cb_{API_TYPE_EXPORT_L2}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) # -----姣忔棩鍒濆鍖�---- def everyday_init(blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_INIT, - "sinfo": f"cb_{API_TYPE_CODE_LIST}_{round(time.time() * 1000)}"}) + "sinfo": f"cb_{API_TYPE_INIT}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) # 鍒锋柊浜ゆ槗鏁版嵁 -def refresh_trade_data(type, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, +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"update_trade_data_{round(time.time() * 1000)}"}) + "sinfo": f"cb_{API_TYPE_REFRESH_TRADE_DATA}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) # 鑾峰彇浠g爜灞炴�� def get_code_attribute(code, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_CODE_ATRRIBUTE, "code": code, - "sinfo": f"update_trade_data_{round(time.time() * 1000)}"}) + "sinfo": f"cb_{API_TYPE_CODE_ATRRIBUTE}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) # 鑾峰彇浠g爜浜ゆ槗鐘舵�� def get_code_trade_state(code, blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_CODE_TRADE_STATE, "code": code, - "sinfo": f"update_trade_data_{round(time.time() * 1000)}"}) + "sinfo": f"{API_TYPE_CODE_TRADE_STATE}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) # 鑾峰彇鐜淇℃伅 def get_env_info(blocking=True): - request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, {"type": API_TYPE_GET_ENV, - "sinfo": f"update_trade_data_{round(time.time() * 1000)}"}) + "sinfo": f"cb_{API_TYPE_GET_ENV}_{round(time.time() * 1000)}"}) return __read_response(client, request_id, blocking) +# 鑾峰彇鐜淇℃伅 +def sync_l1_subscript_codes(blocking=True): + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, + {"type": API_TYPE_SYNC_L1_TARGET_CODES, + "sinfo": f"cb_{API_TYPE_SYNC_L1_TARGET_CODES}_{round(time.time() * 1000)}"}) + return __read_response(client, request_id, blocking) + + +# 鑾峰彇绯荤粺鏃ュ織 +def get_system_logs(start_index, count, blocking=True): + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, + {"type": API_TYPE_SYSTEM_LOG, "start_index": start_index, "count": count, + "sinfo": f"cb_{API_TYPE_SYSTEM_LOG}_{round(time.time() * 1000)}"}) + return __read_response(client, request_id, blocking) + + +# 鎷夊彇data_server鐨勫唴瀹� +def get_from_data_server(path, params, blocking=True): + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, + {"type": API_TYPE_GET_FROM_DATA_SERVER, "path": path, "params": params, + "sinfo": f"cb_{API_TYPE_GET_FROM_DATA_SERVER}_{round(time.time() * 1000)}"}) + return __read_response(client, request_id, blocking, timeout=30) + + +# 鑾峰彇浠g爜鐨勪氦鏄撲俊鎭� +def get_code_trade_info(code, blocking=True): + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, + {"type": API_TYPE_CODE_TRADE_INFO, "code": code, + "sinfo": f"cb_{API_TYPE_CODE_TRADE_INFO}_{round(time.time() * 1000)}"}) + return __read_response(client, request_id, blocking, timeout=30) + + +# L2鏈夋晥鐩戝惉鏁伴噺 +def get_l2_listen_active_count(blocking=True): + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, + {"type": API_TYPE_CODE_L2_LISTEN_ACTIVE_COUNT, + "sinfo": f"cb_{API_TYPE_CODE_L2_LISTEN_ACTIVE_COUNT}_{round(time.time() * 1000)}"}) + return __read_response(client, request_id, blocking, timeout=30) + + +# 淇濆瓨姝e湪杩愯鐨勬暟鎹� +def save_running_data(blocking=True): + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, + {"type": API_TYPE_SAVE_RUNNING_DATA, + "sinfo": f"cb_{API_TYPE_SAVE_RUNNING_DATA}_{round(time.time() * 1000)}"}) + return __read_response(client, request_id, blocking, timeout=30) + + +# 淇濆瓨姝e湪杩愯鐨勬暟鎹� +def sell_rule(operate, data={}, blocking=True): + 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) + + +# 鑾峰彇浠g爜鎸佷粨淇℃伅 +def get_code_position_info(code, blocking=True): + 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, 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(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 refresh_trade_data_for_cb(refresh_type, blocking=True): + """ + 鍒锋柊浜ゆ槗鏁版嵁 + :param refresh_type: + :param blocking: + :return: + """ + + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, + {"type": API_TYPE_REFRESH_TRADE_DATA, "ctype": refresh_type, + "sinfo": f"cb_{API_TYPE_REFRESH_TRADE_DATA}_{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 refresh_trade_data_for_cb(type_, blocking=True): + """ + 鍒锋柊鍙浆鍊轰氦鏄撴暟鎹� + :param code: + :param blocking: + :return: + """ + request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, + {"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) + + +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) + + +def common_request_for_low_suction(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_LOW_SUCTION, 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