From 751183dcd74207a50834cacc575f0dfccb41658c Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期二, 06 六月 2023 18:13:05 +0800
Subject: [PATCH] 交易优化,看盘接口完善

---
 third_data/code_plate_key_manager.py |  361 +++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 276 insertions(+), 85 deletions(-)

diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py
index 4a13787..2a30371 100644
--- a/third_data/code_plate_key_manager.py
+++ b/third_data/code_plate_key_manager.py
@@ -6,64 +6,29 @@
 import json
 
 import constant
-import global_data_loader
 import global_util
 import log
 import tool
 from db import redis_manager
 
-from log import logger_kpl_limit_up
+from log import logger_kpl_limit_up, logger_kpl_block_can_buy
+from third_data.kpl_util import KPLPlatManager
 from trade import trade_manager
 
 
-# 瀹炴椂寮�鐩樺暒甯傚満鏁版嵁
-class RealTimeKplMarketData:
-    # 绮鹃�夊墠5
-    top_5_reason_set = set()
-    # 琛屼笟鍓�5
-    top_5_industry_set = set()
+# 寮�鐩樺暒绂佹浜ゆ槗鏉垮潡绠$悊
+class KPLPlateForbiddenManager:
+    __redisManager = redis_manager.RedisManager(3)
 
-    @classmethod
-    def set_top_5_reasons(cls, datas):
-        temp_set = set()
-        base_count = 5
-        for i in range(0, len(datas)):
-            if datas[i][1] in constant.KPL_INVALID_BLOCKS:
-                base_count += 1
-            if i >= base_count:
-                break
-            if datas[i][3] > 5000 * 10000:
-                temp_set.add(datas[i][1])
-        cls.top_5_reason_set = temp_set
+    def __get_redis(self):
+        return self.__redisManager.getRedis()
 
-    @classmethod
-    def set_top_5_industry(cls, datas):
-        temp_set = set()
-        base_count = 5
-        for i in range(0, len(datas)):
-            if datas[i][1] in constant.KPL_INVALID_BLOCKS:
-                base_count += 1
-            if i >= base_count:
-                break
+    def save_plate(self, plate):
+        self.__get_redis().sadd("kpl_forbidden_plates", plate)
+        self.__get_redis().expire("kpl_forbidden_plates", tool.get_expire())
 
-            if datas[i][2] > 5000 * 10000:
-                temp_set.add(datas[i][1])
-        cls.top_5_reason_set = temp_set
-
-    # 鑾峰彇鑳藉涔扮殑琛屼笟鍏抽敭瀛梥et
-    @classmethod
-    def get_can_buy_key_set(cls):
-        temp_set = cls.top_5_reason_set | cls.top_5_industry_set
-        return temp_set
-
-    @classmethod
-    def is_in_top(cls, keys):
-        reasons = cls.get_can_buy_key_set()
-        temp_set = keys & reasons
-        if temp_set:
-            return True, temp_set
-        else:
-            return False, None
+    def list_all(self):
+        return self.__get_redis().smembers("kpl_forbidden_plates")
 
 
 class LimitUpCodesPlateKeyManager:
@@ -123,14 +88,136 @@
         codes_set.discard(code)
         return codes_set
 
+    # 娑ㄥ仠鍘熷洜鍖归厤鍏抽敭瀛�(鍜屾定鍋滃垪琛ㄤ腑鐨勬定鍋滃師鍥犲仛瀵规瘮),杩斿洖:{鍏抽敭璇�:浠g爜闆嗗悎}
+    def match_limit_up_reason_keys(self, code, keys):
+        fresult = {}
+        for k in keys:
+            if k in self.total_key_codes_dict:
+                codes = set(self.total_key_codes_dict[k])
+                codes.discard(code)
+                if codes:
+                    fresult[k] = codes
+        return fresult
 
-# 鐩爣浠g爜鍏抽敭璇嶇鐞�
-class TargetCodePlateKeyManager:
+
+# 瀹炴椂寮�鐩樺暒甯傚満鏁版嵁
+class RealTimeKplMarketData:
+    # 绮鹃�夊墠5
+    top_5_reason_list = []
+    # 琛屼笟鍓�5
+    top_5_industry_list = []
+    #
+    top_5_key_dict = {}
+    total_reason_dict = {}
+    total_industry_dict = {}
+    __KPLPlateForbiddenManager = KPLPlateForbiddenManager()
+    __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager()
+    __KPLPlatManager = KPLPlatManager()
+
+    @classmethod
+    def set_top_5_reasons(cls, datas):
+        temp_list = []
+        for d in datas:
+            cls.total_reason_dict[d[1]] = d
+        # 鎺掑簭
+        for i in range(0, len(datas)):
+            if datas[i][1] not in constant.KPL_INVALID_BLOCKS:
+                # 锛堝悕绉�,鍑�娴佸叆閲戦,鎺掑悕锛�
+                temp_list.append((datas[i][1], datas[i][3], len(temp_list)))
+                # 鍙幏鍙栧墠10涓�
+                if len(temp_list) > 5:
+                    break
+                if datas[i][3] < 1 * 10000 * 10000:
+                    break
+
+        for temp in temp_list:
+            names = cls.__KPLPlatManager.get_same_plat_names_by_id(temp[0])
+            for name in names:
+                if name == temp[1]:
+                    continue
+                temp_list.append((name, temp[1], temp[2]))
+        cls.top_5_reason_list = temp_list
+        cls.__reset_top_5_dict()
+
+    @classmethod
+    def set_top_5_industry(cls, datas):
+        for d in datas:
+            cls.total_industry_dict[d[1]] = d
+        temp_list = []
+        for i in range(0, len(datas)):
+            if datas[i][1] in constant.KPL_INVALID_BLOCKS:
+                continue
+            temp_list.append((datas[i][1], datas[i][2], len(temp_list)))
+            if len(temp_list) > 5:
+                break
+            if datas[i][2] < 1 * 10000 * 10000:
+                break
+        cls.top_5_industry_list = temp_list
+        cls.__reset_top_5_dict()
+
+    @classmethod
+    def __reset_top_5_dict(cls):
+        temp_dict = {}
+        for t in cls.top_5_industry_list:
+            temp_dict[t[0]] = t
+        for t in cls.top_5_reason_list:
+            temp_dict[t[0]] = t
+        cls.top_5_key_dict = temp_dict
+
+    # 鑾峰彇鑳藉涔扮殑琛屼笟鍏抽敭瀛梥et
+    @classmethod
+    def get_can_buy_key_set(cls):
+        temp_set = cls.top_5_key_dict.keys()
+        return temp_set
+
+    # 閫氳繃鍏抽敭瀛楀垽鏂兘涔扮殑浠g爜鏁伴噺
+    @classmethod
+    def get_can_buy_codes_count(cls, code, key):
+        # 鍒ゆ柇琛屼笟娑ㄥ仠绁ㄦ暟閲忥紝闄ゅ紑鑷繁蹇呴』澶т簬1涓�
+        temp_codes = LimitUpCodesPlateKeyManager.total_key_codes_dict.get(key)
+        if temp_codes is None:
+            temp_codes = set()
+        else:
+            temp_codes = set(temp_codes)
+        temp_codes.discard(code)
+        if len(temp_codes) < 1:
+            # 鍚庢帓鎵嶈兘鎸傚崟
+            return 0, "韬綅涓嶄负鍚庢帓"
+
+        forbidden_plates = cls.__KPLPlateForbiddenManager.list_all()
+        if key in forbidden_plates:
+            return 0, "涓嶄拱璇ユ澘鍧�"
+
+        # 10:30浠ュ墠鍙互鎸�2涓崟
+        if int(tool.get_now_time_str().replace(':', '')) < int("103000"):
+            return 2, "10:30浠ュ墠鍙互鎸�2涓崟"
+        # 10:30浠ュ悗
+        if key not in cls.top_5_key_dict:
+            return 0, "鍑�娴佸叆娌″湪鍓�5"
+        if cls.top_5_key_dict[key][1] > 3 * 10000 * 10000:
+            return 2, "鍑�娴佸叆鍦ㄥ墠5涓斿ぇ浜�3浜�"
+        else:
+            return 1, "鍑�娴佸叆鍦ㄥ墠5"
+
+    @classmethod
+    def is_in_top(cls, keys):
+        reasons = cls.get_can_buy_key_set()
+        log.logger_kpl_debug.debug("甯傚満娴佸叆鍓�5:{}", reasons)
+        forbidden_plates = cls.__KPLPlateForbiddenManager.list_all()
+        reasons = reasons - forbidden_plates
+        temp_set = keys & reasons
+        log.logger_kpl_debug.debug("甯傚満娴佸叆鍓�5鍖归厤缁撴灉:{}", temp_set)
+        if temp_set:
+            return True, temp_set
+        else:
+            return False, None
+
+
+#
+class CodesHisReasonAndBlocksManager:
     __redisManager = redis_manager.RedisManager(1)
     # 鍘嗗彶娑ㄥ仠鍘熷洜
     __history_limit_up_reason_dict = {}
-    # 浜岀骇琛屼笟
-    __second_industry_dict = {}
     # 鏉垮潡
     __blocks_dict = {}
 
@@ -141,7 +228,8 @@
         self.__history_limit_up_reason_dict[code] = set(reasons)
         self.__get_redis().setex(f"kpl_his_limit_up_reason-{code}", tool.get_expire(), json.dumps(list(reasons)))
 
-    # 濡傛灉杩斿洖鍊间笉涓篘one琛ㄧず宸茬粡鍔犺浇杩囧巻鍙插師鍥犱簡
+        # 濡傛灉杩斿洖鍊间笉涓篘one琛ㄧず宸茬粡鍔犺浇杩囧巻鍙插師鍥犱簡
+
     def get_history_limit_up_reason(self, code):
         reasons = self.__history_limit_up_reason_dict.get(code)
         if reasons is None:
@@ -150,7 +238,10 @@
             if val is not None:
                 val = set(json.loads(val))
                 self.__history_limit_up_reason_dict[code] = val
-            return self.__history_limit_up_reason_dict.get(code)
+            if code in self.__history_limit_up_reason_dict:
+                return self.__history_limit_up_reason_dict.get(code)
+            else:
+                return None
         else:
             return reasons
 
@@ -166,9 +257,30 @@
             if val is not None:
                 val = set(json.loads(val))
                 self.__blocks_dict[code] = val
-            return self.__blocks_dict.get(code)
+            if code in self.__blocks_dict:
+                return self.__blocks_dict.get(code)
+            else:
+                return None
         else:
             return reasons
+
+    def get_total_keys(self, code):
+        reasons = self.get_history_limit_up_reason(code)
+        if reasons is None:
+            reasons = set()
+        blocks = self.get_blocks(code)
+        if blocks is None:
+            blocks = set()
+        return reasons | blocks
+
+
+# 鐩爣浠g爜鍏抽敭璇嶇鐞�
+class TargetCodePlateKeyManager:
+    __redisManager = redis_manager.RedisManager(1)
+    __CodesPlateKeysManager = CodesHisReasonAndBlocksManager()
+
+    def __get_redis(self):
+        return self.__redisManager.getRedis()
 
     # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,鏉垮潡
     def get_plate_keys(self, code):
@@ -178,17 +290,17 @@
             k1 = {LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict[code]}
         # 鍔犺浇鍘嗗彶鍘熷洜
         k11 = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}")
-        k2 = set()
-        if code in self.__history_limit_up_reason_dict:
-            k2 = self.__history_limit_up_reason_dict[code]
+        k2 = self.__CodesPlateKeysManager.get_history_limit_up_reason(code)
+        if k2 is None:
+            k2 = set()
         k3 = set()
         industry = global_util.code_industry_map.get(code)
         if industry:
             k3 = {industry}
 
-        k4 = set()
-        if code in self.__blocks_dict:
-            k4 = self.__blocks_dict[code]
+        k4 = self.__CodesPlateKeysManager.get_blocks(code)
+        if k4 is None:
+            k4 = set()
         for k in [k1, k11, k2, k3, k4]:
             keys |= k
 
@@ -201,40 +313,119 @@
 class CodePlateKeyBuyManager:
     __TargetCodePlateKeyManager = TargetCodePlateKeyManager()
     __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager()
+    __CodesHisReasonAndBlocksManager = CodesHisReasonAndBlocksManager()
 
     # 鏄惁鍙互涓嬪崟
     @classmethod
     def can_buy(cls, code):
+        if constant.TEST:
+            return True, ""
         keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code)
-        # 鏉垮潡Key鏄惁鍦ㄥ競鍦哄墠5key涓�
-        is_in, valid_keys = RealTimeKplMarketData.is_in_top(keys)
-        if not valid_keys:
-            return False, "鏉垮潡鏈湪甯傚満娴佸叆鍓�5"
-        # 鐩稿悓鏉垮潡涓槸鍚﹀凡缁忔湁鍒殑绁ㄦ定鍋�
-        is_back = False, ''
-        for key in valid_keys:
-            codes = cls.__LimitUpCodesPlateKeyManager.get_codes_by_key_without_mine(key, code)
-            if codes and len(codes) > 0:
-                is_back = True, key
+        log.logger_kpl_debug.info("{}鍏抽敭璇嶏細{},{},{},{},{},{}", code, keys, k1, k11, k2, k3, k4)
+        # 娑ㄥ仠鍒楄〃涓尮閰嶅叧閿瘝
+        match_limit_up_result = cls.__LimitUpCodesPlateKeyManager.match_limit_up_reason_keys(code, keys)
+        log.logger_kpl_debug.info("{}鍏抽敭璇嶈韩浣嶅尮閰嶇粨鏋滐細{}", code, match_limit_up_result)
+        if not match_limit_up_result:
+            return False, "鏈湪娑ㄥ仠鍒楄〃涓湭鍖归厤鍒版定鍋滃師鍥�"
+
+        # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------start------------
+        # 鍒ゆ柇鍖归厤鍑虹殑娑ㄥ仠鍘熷洜锛屽垽鏂槸鍚︽湁宸茬粡涓嬪崟鐨勭エ
+        # reason_need_buy_dict = {}
+        # for k in match_limit_up_result:
+        #     codes = match_limit_up_result[k]
+        #     final_codes_keys = [keys]
+        #     for code_ in codes:
+        #         temp_key_set = set()
+        #         temp_key_set |= cls.__CodesHisReasonAndBlocksManager.get_total_keys(code_)
+        #         temp = cls.__LimitUpCodesPlateKeyManager.total_code_keys_dict.get(code_)
+        #         if temp:
+        #             temp_key_set |= temp
+        #         # 浜岀骇
+        #         industry = global_util.code_industry_map.get(code_)
+        #         if industry:
+        #             temp_key_set.add(industry)
+        #
+        #         final_codes_keys.append(temp_key_set)
+        #     # 姹傚叡鍚岀殑鍏抽敭璇�
+        #     intersection = set(final_codes_keys[0])
+        #     for s in final_codes_keys:
+        #         intersection &= s
+        #     log.logger_kpl_debug.info("{}鐨勬澘鍧楁眰浜ら泦锛歿}-{}", code, k, intersection)
+        #
+        #     # 姹傚叕鍏辩殑鏉垮潡鏄惁鍦ㄦ祦鍏ュ墠5涓�
+        #     is_in, valid_keys = RealTimeKplMarketData.is_in_top(intersection)
+        #     if is_in:
+        #         reason_need_buy_dict[k] = (is_in, valid_keys)
+        # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------end------------
+
+        # 鑾峰彇鏉垮潡鍙互涓嬪崟鐨勪釜鏁�
+        can_buy_codes_count_dict = {}
+
+        for key__ in match_limit_up_result:
+            can_buy_count, msg = RealTimeKplMarketData.get_can_buy_codes_count(code, key__)
+            can_buy_codes_count_dict[key__] = can_buy_count
+
+        has_available_key = False
+        for key in can_buy_codes_count_dict:
+            if can_buy_codes_count_dict[key] > 0:
+                has_available_key = True
                 break
-        if not is_back[0]:
-            return False, f"鏉垮潡涓涓定鍋滐細{valid_keys}"
-        # 鐪嬫澘鍧椾腑鏄惁宸茬粡鏈夊凡缁忎笅鍗曠殑鎴栬�呮垚浜ょ殑浠g爜
-        codes = trade_manager.get_codes_by_trade_states(
-            {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER,
-             trade_manager.TRADE_STATE_BUY_SUCCESS})
-        # 閬嶅巻宸茬粡鎴愪氦/涓嬪崟鐨勪唬鐮侊紝鑾峰彇鍏舵定鍋滃師鍥狅紝鐒跺悗鍜屽綋鍓嶄唬鐮佹定鍋滃師鍥犲仛姣旇緝锛屾湁鐩稿悓浠g爜鐨勪笉鑳戒拱
+        if not has_available_key:
+            return False, f"鍖归厤鍒扮殑銆恵','.join(match_limit_up_result.keys())}銆戞病鍦ㄧ簿閫�/琛屼笟鍙互涔板叆鐨勬澘鍧椾腑"
+
+        # ---------------------------------鍔犺浇宸茬粡涓嬪崟/鎴愪氦鐨勪唬鐮佷俊鎭�------------start-------------
+        match_reasons = match_limit_up_result.keys()
+        # 鍒ゆ柇鍖归厤鍒扮殑鍘熷洜鏄惁宸茬粡鏈変笅鍗�/涔板叆鎴愬姛鐨勪唬鐮�
+        codes_delegate = set(trade_manager.get_codes_by_trade_states(
+            {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER}))
+        codes_success = set(trade_manager.get_codes_by_trade_states(
+            {trade_manager.TRADE_STATE_BUY_SUCCESS}))
+
+        codes = codes_delegate | codes_success
+
+        # 缁熻鎴愪氦浠g爜鐨勬澘鍧�
+        trade_codes_blocks_dict = {}
+        # 宸茬粡鎴愪氦鐨勬澘鍧�
+        trade_success_blocks = set()
         for c in codes:
             keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(c)
             # 瀹炴椂娑ㄥ仠鍘熷洜
-            for k_ in k1_:
-                # 褰撳墠浠g爜宸茬粡鏈夋寕鐨勬垨鑰呮垚浜ょ殑
-                if k_ in valid_keys:
-                    return False, f"{k_}鏉垮潡涓殑{c}宸茬粡涓嬪崟/涔板叆鎴愬姛锛屽悓涓�鏉垮潡涓彧鑳戒拱1涓エ"
-        return True, f"娑ㄥ仠鍘熷洜锛歿is_back[1]}"
+            trade_codes_blocks_dict[c] = k1_
+        # 缁熻鏉垮潡涓殑浠g爜
+        trade_block_codes_dict = {}
+        for c in trade_codes_blocks_dict:
+            for b in trade_codes_blocks_dict[c]:
+                if c in codes_success:
+                    trade_success_blocks.add(b)
+                if b not in trade_block_codes_dict:
+                    trade_block_codes_dict[b] = set()
+                trade_block_codes_dict[b].add(c)
+
+        # ---------------------------------鍔犺浇宸茬粡涓嬪崟/鎴愪氦鐨勪唬鐮佷俊鎭�------------end-------------
+
+        msg_list = []
+        for key in can_buy_codes_count_dict:
+            log.logger_kpl_debug.debug(f"{code}锛氭澘鍧楀彲浠ヤ笅鍗曠殑鏁伴噺銆恵key}銆�-{can_buy_codes_count_dict[key]}")
+            if can_buy_codes_count_dict[key] < 1:
+                continue
+            # 鏉垮潡涓凡缁忔湁鎴愪氦鐨勫氨涓嶄笅鍗曚簡
+            if key in trade_success_blocks:
+                msg_list.append(f"銆恵key}銆戜腑宸茬粡鏈夋垚浜や唬鐮�")
+                log.logger_kpl_debug.debug(f"{code}锛氭澘鍧楀凡缁忔湁鎴愪氦銆恵key}銆�")
+                continue
+            # 鏉垮潡鍙互涓嬪崟鏁伴噺
+            if trade_block_codes_dict.get(key) is None or len(trade_block_codes_dict.get(key)) < \
+                    can_buy_codes_count_dict[key]:
+                order_count = len(trade_block_codes_dict.get(key)) if key in trade_block_codes_dict else 0
+                logger_kpl_block_can_buy.info(
+                    f"code={code}锛氥�恵key}銆戝彲浠ヤ笅鍗曪紝鐜版湁鏁伴噺锛歿order_count} 鏈�澶ф暟閲忥細{can_buy_codes_count_dict[key]}")
+                return True, f"鍙互涓嬪崟锛屾澘鍧�:銆恵key}銆�,鏉垮潡涓凡缁忎笅鍗曠殑鏁伴噺锛歿order_count}"
+            else:
+                order_count = len(trade_block_codes_dict.get(key))
+                msg_list.append(f"銆恵key}銆戜腑涓嬪崟浠g爜鏁伴噺{order_count}/鍏佽涓嬪崟鏁伴噺{can_buy_codes_count_dict[key]}")
+
+        return False, ",".join(msg_list)
 
 
 if __name__ == "__main__":
-    datas = log.load_kpl_reason_changes()
-    for k in datas:
-        LimitUpCodesPlateKeyManager().set_today_limit_up_reason_change(k[0], k[1], k[2])
+    pass

--
Gitblit v1.8.0