From cbe19ea6066a600cbd0b5110db5d43f8252d14a8 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期四, 13 六月 2024 11:23:53 +0800
Subject: [PATCH] L撤成交进度相关改进

---
 third_data/code_plate_key_manager.py |  353 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 273 insertions(+), 80 deletions(-)

diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py
index b45b190..d92d255 100644
--- a/third_data/code_plate_key_manager.py
+++ b/third_data/code_plate_key_manager.py
@@ -11,14 +11,14 @@
 from code_attribute import code_nature_analyse
 from db.redis_manager_delegate import RedisUtils
 from third_data import kpl_block_util, kpl_api, kpl_util
-from trade.trade_manager import MarketSituationManager
+from settings.trade_setting import MarketSituationManager
 from utils import global_util, tool, buy_condition_util
 from log_module import log, async_log_util
 from db import redis_manager_delegate as redis_manager
 
 from log_module.log import logger_kpl_block_can_buy
 from third_data.kpl_util import KPLPlatManager
-from trade import trade_manager, l2_trade_util
+from trade import trade_manager, l2_trade_util, trade_constant
 
 
 # 浠g爜绮鹃�夋澘鍧楃鐞�
@@ -93,15 +93,17 @@
             # logger_kpl_block_can_buy.info(f"鍑嗗鏇存柊绮鹃�夋澘鍧楋細{code}-{buy_1_price}-{limit_up_price}")
             if limit_up_price and buy_1_price:
                 # 澶勭悊涔�1,鍗�1淇℃伅
-                pre_close_price = round(float(limit_up_price) / 1.1, 2)
+                pre_close_price = round(float(limit_up_price) / tool.get_limit_up_rate(code), 2)
                 # 濡傛灉娑ㄥ箙澶т簬7%灏辫鍙栨澘鍧�
                 price_rate = (buy_1_price - pre_close_price) / pre_close_price
                 if price_rate > 0.07:
                     jx_blocks_info = self.get_jx_blocks_cache(code)
                     if not jx_blocks_info:
+                        start_time = time.time()
                         blocks = kpl_api.getCodeBlocks(code)
+                        async_log_util.info(logger_kpl_block_can_buy,
+                                            f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�-{blocks}  鑰楁椂:{int(time.time() - start_time)}s")
                         self.save_jx_blocks(code, blocks, current_limit_up_blocks)
-                        async_log_util.info(logger_kpl_block_can_buy, f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�-{blocks}")
                     else:
                         # 杩樻病娑ㄥ仠鐨勯渶瑕佹洿鏂扮簿閫夋澘鍧� 鏇存柊绮鹃�夋澘鍧�
                         if abs(float(buy_1_price) - float(limit_up_price)) >= 0.001:
@@ -116,20 +118,67 @@
                                     UPDATE_TIME_SPACE = 5 * 60
 
                             if time.time() - jx_blocks_info[1] > UPDATE_TIME_SPACE:
+                                start_time = time.time()
                                 # 璺濈涓婃鏇存柊鏃堕棿杩囧幓浜�5鍒嗛挓
                                 blocks = kpl_api.getCodeBlocks(code)
+                                async_log_util.info(logger_kpl_block_can_buy,
+                                                    f"{code}:鑾峰彇鍒扮簿閫夋澘鍧楋紙鏇存柊锛�-{blocks}  鑰楁椂:{int(time.time() - start_time)}s")
                                 self.save_jx_blocks(code, blocks, current_limit_up_blocks)
-                                async_log_util.info(logger_kpl_block_can_buy, f"{code}:鑾峰彇鍒扮簿閫夋澘鍧楋紙鏇存柊锛�-{blocks}")
+
 
                 elif price_rate > 0.03:
                     # 娣诲姞澶囩敤鏉垮潡
                     if not self.get_jx_blocks_cache(code, by=True):
+                        start_time = time.time()
                         blocks = kpl_api.getCodeBlocks(code)
                         self.save_jx_blocks(code, blocks, current_limit_up_blocks, by=True)
-                        async_log_util.info(logger_kpl_block_can_buy, f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�(澶囩敤)-{blocks}")
+                        async_log_util.info(logger_kpl_block_can_buy,
+                                            f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�(澶囩敤)-{blocks}  鑰楁椂:{int(time.time() - start_time)}s")
         except Exception as e:
             logger_kpl_block_can_buy.error(f"{code} 鑾峰彇鏉垮潡鍑洪敊")
             logger_kpl_block_can_buy.exception(e)
+
+
+# 绂佹涓嬪崟鐨勬澘鍧�
+class ForbiddenBlockManager:
+    __db = 3
+    __redisManager = redis_manager.RedisManager(3)
+    __instance = None
+    __forbidden_blocks = set()
+
+    def __new__(cls, *args, **kwargs):
+        if not cls.__instance:
+            cls.__instance = super(ForbiddenBlockManager, cls).__new__(cls, *args, **kwargs)
+            cls.__load_data()
+        return cls.__instance
+
+    @classmethod
+    def __get_redis(cls):
+        return cls.__redisManager.getRedis()
+
+    # 鍔犺浇鏁版嵁
+    @classmethod
+    def __load_data(cls):
+        blocks = cls.__get_redis().smembers("forbidden_blocks")
+        if blocks:
+            for b in blocks:
+                cls.__forbidden_blocks.add(b)
+
+    def add(self, block):
+        self.__forbidden_blocks.add(block)
+        RedisUtils.sadd_async(self.__db, "forbidden_blocks", block)
+        RedisUtils.expire_async(self.__db, "forbidden_blocks", tool.get_expire())
+
+    def remove(self, block):
+        if block in self.__forbidden_blocks:
+            self.__forbidden_blocks.remove(block)
+        RedisUtils.srem_async(self.__db, "forbidden_blocks", block)
+
+    def get_blocks(self):
+        return copy.deepcopy(self.__forbidden_blocks)
+
+    def is_in(self, block):
+        return block in self.__forbidden_blocks
 
 
 # 寮�鐩樺暒绂佹浜ゆ槗鏉垮潡绠$悊
@@ -172,7 +221,7 @@
 class LimitUpCodesPlateKeyManager:
     # 浠婃棩娑ㄥ仠鍘熷洜
     today_limit_up_reason_dict = {}
-    today_total_limit_up_reason_dict = {}
+    __today_total_limit_up_reason_dict = {}
     total_code_keys_dict = {}
     total_key_codes_dict = {}
     __redisManager = redis_manager.RedisManager(1)
@@ -180,7 +229,7 @@
     def __get_redis(self):
         return self.__redisManager.getRedis()
 
-    # 鑾峰彇浠婃棩娑ㄥ仠鏁版嵁锛屾牸寮忥細[(浠g爜,娑ㄥ仠鍘熷洜)]
+    # 鑾峰彇浠婃棩娑ㄥ仠鏁版嵁锛屾牸寮忥細[(浠g爜,娑ㄥ仠鍘熷洜,绮鹃�夋澘鍧楀垪琛�)]
     def set_today_limit_up(self, datas):
         temp_dict = {}
         if datas:
@@ -193,10 +242,17 @@
         self.set_today_total_limit_up(datas)
 
     # 璁剧疆浠婃棩鍘嗗彶娑ㄥ仠鏁版嵁
-    def set_today_total_limit_up(self, datas):
+    # 鏍煎紡锛�(浠g爜,娑ㄥ仠鍘熷洜,绮鹃�夋澘鍧楀垪琛�)
+    @classmethod
+    def set_today_total_limit_up(cls, datas):
         for item in datas:
             code = item[0]
-            self.today_total_limit_up_reason_dict[code] = item[1]
+            # 璁剧疆娑ㄥ仠浠g爜鐨勬澘鍧楀強鍘熷洜
+            cls.__today_total_limit_up_reason_dict[code] = (item[1], item[2])
+
+    @classmethod
+    def get_today_limit_up_reason(cls, code):
+        return cls.__today_total_limit_up_reason_dict.get(code)
 
     # 浠婃棩娑ㄥ仠鍘熷洜鍙樺寲
     def set_today_limit_up_reason_change(self, code, from_reason, to_reason):
@@ -429,10 +485,17 @@
 
     # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,绮鹃�夋澘鍧�
     def get_plate_keys(self, code):
+        """
+        鑾峰彇浠g爜鐨勬澘鍧�
+        @param code:
+        @return: 锛堟澘鍧楀叧閿瘝闆嗗悎,浠婃棩娑ㄥ仠鍘熷洜+娑ㄥ仠鎺ㄨ崘鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,绮鹃�夋澘鍧楋級
+        """
         keys = set()
         k1 = set()
-        if code in LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict:
-            k1 = {LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict[code]}
+
+        limit_up_reason_info = LimitUpCodesPlateKeyManager.get_today_limit_up_reason(code)
+        if limit_up_reason_info:
+            k1 = {limit_up_reason_info[0]} | set(limit_up_reason_info[1])
         # 鍔犺浇浠婃棩鍘嗗彶鍘熷洜,鏆傛椂涓嶉渶瑕佸巻鍙插師鍥犱簡
         k11 = set()  # RedisUtils.smembers(self.__get_redis(), f"kpl_limit_up_reason_his-{code}")
         k2 = self.__CodesPlateKeysManager.get_history_limit_up_reason_cache(code)
@@ -450,12 +513,14 @@
         if jingxuan_block_info:
             jingxuan_blocks = jingxuan_block_info[0]
             k4 |= set(jingxuan_blocks)  # set([x[1] for x in jingxuan_blocks])
-        for k in [k1, k11, k2, k3, k4]:
-            keys |= k
-
-        # 鎺掗櫎鏃犳晥鐨勬定鍋滃師鍥�
-        keys = keys - set(constant.KPL_INVALID_BLOCKS)
-
+        if k1:
+            # 娑ㄥ仠杩�
+            keys |= k1
+            keys = keys - set(constant.KPL_INVALID_BLOCKS)
+        if not keys:
+            # 鑾峰彇涓嶅埌娑ㄥ仠鍘熷洜
+            keys |= k4
+            keys = keys - set(constant.KPL_INVALID_BLOCKS)
         return keys, k1, k11, k2, k3, k4
 
 
@@ -482,13 +547,40 @@
         # 鏍规嵁韬綅绉婚櫎浠g爜
         # return
         # 涓嬭繃鍗曠殑浠g爜涓嶇Щ闄�
-        if trade_manager.CodesTradeStateManager().get_trade_state_cache(code) != trade_manager.TRADE_STATE_NOT_TRADE:
+        if trade_manager.CodesTradeStateManager().get_trade_state_cache(code) != trade_constant.TRADE_STATE_NOT_TRADE:
             # 鍙涓嬭繃鍗曠殑灏变笉绉婚櫎
             return
         l2_trade_util.forbidden_trade(code, msg=msg)
         logger_kpl_block_can_buy.info(msg)
 
-    # 杩斿洖鍐呭(鏄惁鍙拱, 鏄惁涓虹嫭鑻�, 鎻忚堪淇℃伅)
+    # 鏄惁闇�瑕佹縺杩涗拱
+    @classmethod
+    def __is_need_active_buy(cls, code, block, current_rank, open_limit_up_count):
+        """
+        鏉垮潡鏄惁闇�瑕佹縺杩涗拱鍏�
+        瑙勫垯锛氭牴鎹韩浣嶅垽鏂槸鍚﹂渶瑕佹縺杩涗拱锛屾牴鎹椂闂村垝鍒�
+        @param code: 浠g爜
+        @param block: 鏉垮潡鍚嶇О
+        @param current_rank: 鐩墠鍦ㄦ澘鍧椾腑鐨勮韩浣嶏紝浠�0寮�濮�
+        @param open_limit_up_count: 寮�1鐨勬暟閲�
+        @return:
+        """
+
+        real_current_rank = max(current_rank - open_limit_up_count, 0)
+
+        TIME_STR_RANGES = ["10:00:00", "10:30:00", "11:00:00", "13:00:00", "13:30:00", "14:00:00", "14:30:00",
+                           "15:00:00"]
+        TIME_INT_RANGES = [int(x.replace(':', '')) for x in TIME_STR_RANGES]
+        MAX_RANKS = [3, 3, 2, 2, 1, 0, 0, 0]
+        now_time_str = tool.get_now_time_str().replace(':', '')
+        for i in range(len(TIME_INT_RANGES)):
+            if int(now_time_str) <= TIME_INT_RANGES[i]:
+                if MAX_RANKS[i] > real_current_rank:
+                    return True
+                break
+        return False
+
+    # 杩斿洖鍐呭(鏄惁鍙拱, 鏄惁涓虹嫭鑻�, 鎻忚堪淇℃伅, 鏄惁涓哄己鍔夸富绾�, 鏄惁闇�瑕佹縺杩涗拱)
     @classmethod
     def __is_block_can_buy(cls, code, block, current_limit_up_datas, code_limit_up_reasons_dict,
                            yesterday_current_limit_up_codes, limit_up_record_datas, current_limit_up_block_codes_dict,
@@ -501,13 +593,14 @@
         block_codes = current_limit_up_block_codes_dict.get(block)
         if block_codes is None:
             block_codes = set()
+
         if not block_codes:
             # 楂樹綅鏉挎硾鍖栨澘鍧椾腑鏃犳澘鍧�
             if not high_level_block_codes.get(block):
-                return False, True, f"{block}:鏉垮潡鏃犳定鍋�", False
+                return False, True, f"{block}:鏉垮潡鏃犳定鍋�", False, False
         elif len(block_codes) == 1 and code in block_codes:
             if not high_level_block_codes.get(block):
-                return False, True, f"{block}:鏉垮潡鍙湁褰撳墠浠g爜娑ㄥ仠", False
+                return False, True, f"{block}:鏉垮潡鍙湁褰撳墠浠g爜娑ㄥ仠", False, False
         # 鍙互涔扮殑鏈�澶ф帓鍚�
         # open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas,
         #                                                                   code_limit_up_reason_dict)
@@ -555,6 +648,7 @@
 
         # 鑾峰彇浠g爜鐨勫垵娆℃定鍋滄椂闂�
         first_limit_up_time = time.time()
+        # if limit_up_record_datas:
         for r in limit_up_record_datas:
             if r[3] == code:
                 first_limit_up_time = int(r[5])
@@ -569,6 +663,9 @@
                                                                                                     current_open_limit_up_codes),
                                                                                                 shsz=True,
                                                                                                 limit_up_time=first_limit_up_time)
+        # 璁$畻鏄惁闇�瑕佹縺杩涗拱鍏�
+        is_active_buy = cls.__is_need_active_buy(code, block, current_shsz_rank, len(current_open_limit_up_codes))
+
         # record_shsz_rank, record_shsz_rank_codes = kpl_block_util.get_code_record_rank(code, block,
         #                                                                                limit_up_record_datas,
         #                                                                                code_limit_up_reason_dict,
@@ -576,23 +673,24 @@
         #                                                                                shsz=True)
         if int(tool.get_now_time_str().replace(":", "")) <= int("094000") and is_strong_block:
             # 寮哄娍涓荤嚎鍔犲己鍔�10鍒嗛挓
-            return True, False, f"銆恵block}銆戯細寮哄娍涓荤嚎+寮哄娍10鍒嗛挓", is_strong_block
+            return True, False, f"銆恵block}銆戯細寮哄娍涓荤嚎+寮哄娍10鍒嗛挓", is_strong_block, is_active_buy
 
         if current_shsz_rank < len(current_open_limit_up_codes) + max_rank:
-            return True, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{current_shsz_rank}", is_strong_block
+            return True, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{current_shsz_rank}", is_strong_block, is_active_buy
         else:
-            k_format = code_nature_analyse.CodeNatureRecordManager().get_k_format_cache(code)
-            if k_format and k_format[8][0]:
-                # 鍏锋湁杈ㄨ瘑搴�
-                return True, False, f"銆恵block}銆戝叿鏈夎鲸璇嗗害", is_strong_block
+            # k_format = code_nature_analyse.CodeNatureRecordManager().get_k_format_cache(code)
+            # if k_format and k_format[8][0]:
+            #     # 鍏锋湁杈ㄨ瘑搴�
+            #     return True, False, f"銆恵block}銆戝叿鏈夎鲸璇嗗害", is_strong_block
             # 鐪嬭嚜鐢辨祦閫氬競鍊兼槸鍚﹀皬浜�20浜�
             if is_strong_block and current_shsz_rank < len(current_open_limit_up_codes) + max_rank + 1:
-                zyltgb = global_util.zyltgb_map.get(code)
+                zyltgb_as_yi = round(global_util.zyltgb_map.get(code) / 100000000,
+                                     2) if code in global_util.zyltgb_map else None
                 situation = MarketSituationManager().get_situation_cache()
-                zylt_threshold = buy_condition_util.get_zyltgb_threshold(situation)
-                if zyltgb and zylt_threshold[1] <= zyltgb <= zylt_threshold[2]:
-                    return True, False, f"銆恵block}銆戝己鍔挎澘鍧� 鑷敱娴侀�氬競鍊�({zyltgb})澶т簬{zylt_threshold[1]//100000000}浜� 灏忎簬{zylt_threshold[2]//100000000}浜�", is_strong_block
-            return False, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{front_current_shsz_rank_codes} 瓒呰繃{len(current_open_limit_up_codes) + max_rank}涓�", is_strong_block
+                zylt_threshold_as_yi = buy_condition_util.get_zyltgb_threshold(situation)
+                if zyltgb_as_yi and zylt_threshold_as_yi[2] <= zyltgb_as_yi <= zylt_threshold_as_yi[3]:
+                    return True, False, f"銆恵block}銆戝己鍔挎澘鍧� 鑷敱娴侀�氬競鍊�({zyltgb_as_yi})澶т簬{zylt_threshold_as_yi[2]}浜� 灏忎簬{zylt_threshold_as_yi[3]}浜�", is_strong_block, is_active_buy
+            return False, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{front_current_shsz_rank_codes} 瓒呰繃{len(current_open_limit_up_codes) + max_rank}涓�", is_strong_block, is_active_buy
 
         # 杩囨椂鐨勪唬鐮�
         # if open_limit_up_codes:
@@ -612,6 +710,101 @@
         #             cls.__remove_from_l2(code, f"{code}鏍规嵁韬綅绂佹涔板叆锛氥�恵block}銆戝巻鍙茶韩浣峽record_shsz_rank}")
         #
         #         return False, f"鏉垮潡-{block}: top4娑ㄥ仠鏉垮潡锛岄潪涓绘澘寮�1,涓嶄负涓绘澘榫�1锛堝疄鏃惰韩浣�-{current_shsz_rank}:{front_current_shsz_rank_codes},鍘嗗彶韬綅-{record_shsz_rank}锛�"
+
+    @classmethod
+    def __is_block_can_buy_new(cls, code, block, current_limit_up_datas, code_limit_up_reasons_dict,
+                               yesterday_current_limit_up_codes, limit_up_record_datas,
+                               current_limit_up_block_codes_dict,
+                               high_level_code_blocks=None, high_level_block_codes=None):
+        """
+        璇ョエ鐨勬澘鍧楁槸鍚﹀彲浠ヤ拱
+        @param code:
+        @param block:
+        @param current_limit_up_datas:
+        @param code_limit_up_reasons_dict:
+        @param yesterday_current_limit_up_codes:
+        @param limit_up_record_datas:
+        @param current_limit_up_block_codes_dict:
+        @param high_level_code_blocks:
+        @param high_level_block_codes:
+        @return:
+        """
+        # 鐙嫍鍒ゆ柇
+        if high_level_code_blocks is None:
+            high_level_code_blocks = {}
+        if high_level_block_codes is None:
+            high_level_block_codes = {}
+        block_codes = current_limit_up_block_codes_dict.get(block)
+        if block_codes is None:
+            block_codes = set()
+        # 鍘嗗彶娑ㄥ仠浠g爜
+        block_codes_records = set()
+        if limit_up_record_datas:
+            for k in limit_up_record_datas:
+                if block in code_limit_up_reasons_dict.get(k[3]):
+                    block_codes_records.add(k[3])
+
+        if not block_codes:
+            # 楂樹綅鏉挎硾鍖栨澘鍧椾腑鏃犳澘鍧�
+            if not high_level_block_codes.get(block):
+                return False, True, f"{block}:鏉垮潡鏃犳定鍋�", False, False, 0, 0, 0
+        elif len(block_codes) == 1 and code in block_codes:
+            if not high_level_block_codes.get(block):
+                return False, True, f"{block}:鏉垮潡鍙湁褰撳墠浠g爜娑ㄥ仠", False, False, 0, 0, 0
+        # 鍙互涔扮殑鏈�澶ф帓鍚�
+        # open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas,
+        #                                                                   code_limit_up_reason_dict)
+        current_open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes_current(code, block,
+                                                                                          current_limit_up_datas)
+
+        is_strong_block = False
+
+        # 鏈�澶氫拱鑰佸嚑
+        RANKS = [5, 4, 3, 3, 3, 2, 2]
+        RANK_TIMES = ["10:00:00", "10:30:00", "11:00:00", "11:30:00", "13:30:00", "14:00:00", "15:00:00"]
+        now_time_str = tool.get_now_time_str()
+        max_rank = 2
+        for i in range(len(RANK_TIMES)):
+            if tool.trade_time_sub(now_time_str, RANK_TIMES[i]) <= 0:
+                max_rank = RANKS[i]
+                break
+
+        # 闇�瑕佹帓闄ょ殑鑰佸ぇ鐨勪唬鐮�
+        exclude_first_codes = set()
+
+        # 鑾峰彇涓绘澘寮�1鐨勪唬鐮�
+
+        # 鍓旈櫎楂樹綅鏉�
+        if current_open_limit_up_codes and yesterday_current_limit_up_codes:
+            current_open_limit_up_codes -= yesterday_current_limit_up_codes
+
+        # 鑾峰彇浠g爜鐨勫垵娆℃定鍋滄椂闂�
+        first_limit_up_time = time.time()
+        # if limit_up_record_datas:
+        for r in limit_up_record_datas:
+            if r[3] == code:
+                first_limit_up_time = int(r[5])
+
+        # 鑾峰彇涓绘澘瀹炴椂韬綅,鍓旈櫎楂樹綅鏉�
+        current_shsz_rank, front_current_shsz_rank_codes = kpl_block_util.get_code_current_rank(code, block,
+                                                                                                current_limit_up_datas,
+                                                                                                code_limit_up_reasons_dict,
+                                                                                                yesterday_current_limit_up_codes,
+                                                                                                exclude_first_codes,
+                                                                                                len(
+                                                                                                    current_open_limit_up_codes),
+                                                                                                shsz=True,
+                                                                                                limit_up_time=first_limit_up_time)
+
+        # 璁$畻鏄惁闇�瑕佹縺杩涗拱鍏�
+        is_active_buy = cls.__is_need_active_buy(code, block, current_shsz_rank, len(current_open_limit_up_codes))
+
+        if current_shsz_rank < len(current_open_limit_up_codes) + max_rank:
+            return True, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{current_shsz_rank}", is_strong_block, is_active_buy, current_shsz_rank, len(
+                block_codes), len(block_codes_records)
+        else:
+            return False, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{front_current_shsz_rank_codes} 瓒呰繃{len(current_open_limit_up_codes) + max_rank}涓�", is_strong_block, is_active_buy, current_shsz_rank, len(
+                block_codes), len(block_codes_records)
 
     # 鑾峰彇鍙互涔扮殑鏉垮潡
     # current_limit_up_datas: 浠婃棩瀹炴椂娑ㄥ仠
@@ -633,8 +826,9 @@
                         code_limit_up_reasons_dict[d[3]] = {list(before_blocks_dict.get(d[3]))[0]}
                     else:
                         code_limit_up_reasons_dict[d[3]] = {d[2]}
-                        if d[6]:
-                            code_limit_up_reasons_dict[d[3]] |= set(d[6].split("銆�"))
+                        # 涓嶅寘鍚帹鑽愬師鍥�
+                        # if d[6]:
+                        #     code_limit_up_reasons_dict[d[3]] |= set(d[6].split("銆�"))
             return code_limit_up_reasons_dict
 
         if current_limit_up_datas is None:
@@ -642,19 +836,6 @@
 
         # 鑾峰彇鐩爣浠g爜鏉垮潡
         keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code)
-        # log.logger_kpl_debug.info("{}鍏抽敭璇嶏細浠婃棩-{},浠婃棩鍘嗗彶-{},鍘嗗彶-{},浜岀骇琛屼笟-{},浠g爜鏉垮潡-{}", code, k1, k11, k2, k3, k4)
-        keys = set()
-        if k1:
-            for k in k1:
-                if k not in constant.KPL_INVALID_BLOCKS:
-                    keys.add(k)
-        # 濮嬬粓鑾峰彇绮鹃�夋澘鍧�
-        if True:
-            # 鑾峰彇
-            if k4:
-                # 褰撴定鍋滃師鍥犳病鏈夋椂鎵嶅彇绮鹃�夋澘鍧�
-                keys |= k4
-                keys = keys - constant.KPL_INVALID_BLOCKS
 
         # log.logger_kpl_debug.info("{}鏈�缁堝叧閿瘝锛歿}", code, keys)
 
@@ -666,28 +847,32 @@
         code_limit_up_reasons_dict = {}
         load_code_block()
         for block in keys:
-            can_buy, unique, msg, is_strong = cls.__is_block_can_buy(code, block, current_limit_up_datas,
-                                                                     code_limit_up_reasons_dict,
-                                                                     yesterday_current_limit_up_codes,
-                                                                     limit_up_record_datas,
-                                                                     current_limit_up_block_codes_dict,
-                                                                     high_level_code_blocks=high_level_general_code_blocks,
-                                                                     high_level_block_codes=high_level_general_block_codes)
-            fresults.append((block, can_buy, unique, msg, is_strong))
+            can_buy, unique, msg, is_strong, is_active_buy, current_rank, block_limit_up_count, block_limit_up_record_count = cls.__is_block_can_buy_new(
+                code, block,
+                current_limit_up_datas,
+                code_limit_up_reasons_dict,
+                yesterday_current_limit_up_codes,
+                limit_up_record_datas,
+                current_limit_up_block_codes_dict,
+                high_level_code_blocks=high_level_general_code_blocks,
+                high_level_block_codes=high_level_general_block_codes)
+            fresults.append((block, can_buy, unique, msg, is_strong, is_active_buy, current_rank, block_limit_up_count,
+                             block_limit_up_record_count))
         return fresults, keys
 
     # 鏄惁鍙互涓嬪崟
     # 杩斿洖锛氬彲浠ヤ拱鐨勬澘鍧�,鏄惁鐙嫍,娑堟伅
+    #  鍙拱鐨勬澘鍧�, 鏄惁鐙嫍, 娑堟伅, 鍙拱鐨勫己鍔挎澘鍧�, 鍏抽敭璇�, 婵�杩涗拱鐨勬澘鍧�
     @classmethod
     def can_buy(cls, code):
         if constant.TEST:
-            return ["娴嬭瘯"], True, cls.BLOCK_TYPE_NONE, [], set()
+            return [("娴嬭瘯", 0, 1, 1)], True, cls.BLOCK_TYPE_NONE, [], set(), ["鍖栧伐"]
         # if True:
         #     # 娴嬭瘯
         #     return True, "涓嶅垽鏂澘鍧楄韩浣�"
         return cls.__can_buy_compute_result_dict.get(code)
 
-    # 杩斿洖:(鍙互涔扮殑鏉垮潡鍒楄〃, 鏄惁鏄嫭鑻�, 娑堟伅绠�浠�,鍙拱鐨勫己鍔夸富绾�)
+    # 杩斿洖:(鍙互涔扮殑鏉垮潡鍒楄〃, 鏄惁鏄嫭鑻�, 娑堟伅绠�浠�,鍙拱鐨勫己鍔夸富绾�, 婵�杩涗拱鍏ユ澘鍧楀垪琛�)
     @classmethod
     def __compute_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas,
                                  yesterday_current_limit_up_codes, before_blocks_dict,
@@ -706,11 +891,11 @@
                                                              high_level_general_code_blocks,
                                                              high_level_general_block_codes)
         if not blocks_compute_results:
-            return False, True, f"娌℃湁鎵惧埌鏉垮潡", [], keys
+            return False, True, f"娌℃湁鎵惧埌鏉垮潡", [], keys, []
         codes_delegate = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache(
-            {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER}))
+            {trade_constant.TRADE_STATE_BUY_DELEGATED, trade_constant.TRADE_STATE_BUY_PLACE_ORDER}))
         codes_success = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache(
-            {trade_manager.TRADE_STATE_BUY_SUCCESS}))
+            {trade_constant.TRADE_STATE_BUY_SUCCESS}))
         codes = codes_delegate | codes_success
         # 缁熻鎴愪氦浠g爜鐨勬澘鍧�
         trade_codes_blocks_dict = {}
@@ -748,9 +933,9 @@
         can_buy_strong_blocks = []
         unique_count = 0
         msg_list = []
-
+        active_buy_blocks = []
         for r in blocks_compute_results:
-            # r鐨勬暟鎹粨鏋�(鏉垮潡,鏄惁鍙互涔�,鏄惁鐙嫍,娑堟伅,鏄惁鏄己鍔挎澘鍧�)
+            # r鐨勬暟鎹粨鏋�(鏉垮潡,鏄惁鍙互涔�,鏄惁鐙嫍,娑堟伅,鏄惁鏄己鍔挎澘鍧�, 婵�杩涗拱鍏ヤ俊鎭�)
             if r[2]:
                 # 鐙嫍
                 unique_count += 1
@@ -758,24 +943,31 @@
                 # 寮哄娍涓荤嚎鏈�澶氬悓鏃舵寕3鍙エ锛屾渶澶氭垚浜�2鍙エ
                 MAX_DELEGATE_COUNT = 3 if r[4] else 2
                 MAX_DEAL_COUNT = 2 if r[4] else 1
-                if r[0] in trade_success_blocks_count and len(trade_success_blocks_count[r[0]]) >= MAX_DEAL_COUNT:
-                    msg_list.append(f"銆恵r[0]}銆戞湁鎴愪氦浠g爜锛歿trade_success_blocks_count[r[0]]}")
-                    continue
-                if r[0] in trade_delegate_blocks_count and len(trade_delegate_blocks_count[r[0]]) >= MAX_DELEGATE_COUNT:
-                    msg_list.append(f"銆恵r[0]}銆戝凡鎸傚崟锛歿trade_delegate_blocks_count[r[0]]}")
-                    continue
-                can_buy_blocks.append(r[0])
+                # if r[0] in trade_success_blocks_count and len(trade_success_blocks_count[r[0]]) >= MAX_DEAL_COUNT:
+                #     msg_list.append(f"銆恵r[0]}銆戞湁鎴愪氦浠g爜锛歿trade_success_blocks_count[r[0]]}")
+                #     continue
+                # if r[0] in trade_delegate_blocks_count and len(trade_delegate_blocks_count[r[0]]) >= MAX_DELEGATE_COUNT:
+                #     msg_list.append(f"銆恵r[0]}銆戝凡鎸傚崟锛歿trade_delegate_blocks_count[r[0]]}")
+                #     continue
+                if len(r) > 8:
+                    can_buy_blocks.append((r[0], r[6], r[7], r[8]))
+                else:
+                    # 锛堟澘鍧楀悕绉�,韬綅,鏉垮潡娑ㄥ仠鏁伴噺锛�
+                    can_buy_blocks.append((r[0], 0, 1, 1))
                 if r[4]:
                     can_buy_strong_blocks.append(r[0])
                 if r[3]:
                     msg_list.append(r[3])
+                if r[5]:
+                    active_buy_blocks.append(r[0])
+                    msg_list.append(f"銆恵r[0]}銆戞縺杩涗拱鍏�({r[5]})")
             else:
                 if r[3]:
                     msg_list.append(r[3])
         # 鎵�鏈夋澘鍧楅兘鏄嫭鑻�
         if unique_count == len(blocks_compute_results):
-            return can_buy_blocks, True, ",".join(msg_list), can_buy_strong_blocks, keys
-        return can_buy_blocks, False, ",".join(msg_list), can_buy_strong_blocks, keys
+            return can_buy_blocks, True, ",".join(msg_list), can_buy_strong_blocks, keys, active_buy_blocks
+        return can_buy_blocks, False, ",".join(msg_list), can_buy_strong_blocks, keys, active_buy_blocks
 
     # 鏇存柊浠g爜鏉垮潡鍒ゆ柇鏄惁鍙互涔扮殑缁撴灉
     # high_level_general_code_blocks 楂樹綅娉涘寲鏉垮潡
@@ -810,15 +1002,16 @@
                         f_blocks.append(b)
                 high_level_general_code_blocks[r[0]] = f_blocks
 
-        can_buy_blocks, unique, msg, can_buy_strong_blocks, keys = cls.__compute_can_buy_blocks(code,
-                                                                                                current_limit_up_datas,
-                                                                                                limit_up_record_datas,
-                                                                                                yesterday_current_limit_up_codes,
-                                                                                                before_blocks_dict,
-                                                                                                current_limit_up_block_codes_dict,
-                                                                                                high_level_general_code_blocks)
+        can_buy_blocks, unique, msg, can_buy_strong_blocks, keys, active_buy_blocks = cls.__compute_can_buy_blocks(code,
+                                                                                                                   current_limit_up_datas,
+                                                                                                                   limit_up_record_datas,
+                                                                                                                   yesterday_current_limit_up_codes,
+                                                                                                                   before_blocks_dict,
+                                                                                                                   current_limit_up_block_codes_dict,
+                                                                                                                   high_level_general_code_blocks)
         # 淇濆瓨鏉垮潡璁$畻缁撴灉
-        cls.__can_buy_compute_result_dict[code] = (can_buy_blocks, unique, msg, can_buy_strong_blocks, keys)
+        cls.__can_buy_compute_result_dict[code] = (
+            can_buy_blocks, unique, msg, can_buy_strong_blocks, keys, active_buy_blocks)
 
 
 if __name__ == "__main__":

--
Gitblit v1.8.0