From ae8d76a456b64c1c6c4ebf11b6ec33b7df217b1a Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期一, 18 八月 2025 13:43:46 +0800
Subject: [PATCH] bug修复

---
 third_data/kpl_limit_up_data_manager.py |  305 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 305 insertions(+), 0 deletions(-)

diff --git a/third_data/kpl_limit_up_data_manager.py b/third_data/kpl_limit_up_data_manager.py
index 350664e..3a100e9 100644
--- a/third_data/kpl_limit_up_data_manager.py
+++ b/third_data/kpl_limit_up_data_manager.py
@@ -1,7 +1,13 @@
 """
 寮�鐩樺暒娑ㄥ仠鏁版嵁绠$悊
 """
+import copy
+
 from third_data import kpl_util, kpl_data_manager
+from third_data.history_k_data_manager import HistoryKDataManager
+from third_data.history_k_data_util import HistoryKDatasUtils
+from third_data.kpl_data_constant import LimitUpCodesBlockRecordManager
+from utils import tool, init_data_util
 
 
 def get_current_limit_up_datas(day):
@@ -30,3 +36,302 @@
     @return:
     """
     return kpl_data_manager.KPLLimitUpDataRecordManager.total_datas
+
+
+class CodeLimitUpSequenceManager:
+    """
+    浠g爜韬綅绠$悊
+    """
+    # 棣栨澘韬綅
+    __first_block_sequence_dict = {}
+
+    @classmethod
+    def __get_code_blocks(cls, code):
+        blocks = LimitUpCodesBlockRecordManager().get_radical_buy_blocks(code)
+        if not blocks:
+            blocks = set()
+        return blocks
+
+    @classmethod
+    def set_current_limit_up_datas(cls, current_limit_up_datas):
+        """
+        璁剧疆鐩墠鐨勬定鍋滀唬鐮�
+        @param current_limit_up_datas:
+        @return:
+        """
+        records = get_today_history_limit_up_datas_cache()
+        # 鎸変唬鐮佹帓搴�
+        # {"浠g爜":锛堜唬鐮侊紝娑ㄥ仠鍘熷洜, 娑ㄥ仠鏃堕棿, 鍑犵増锛墋
+        current_code_block_dict = {x[0]: (x[0], x[5], x[2], x[4]) for x in current_limit_up_datas}
+        record_code_block_dict = {x[3]: (x[3], x[2], x[5], x[12]) for x in records}
+        # 鏍规嵁娑ㄥ仠鍘熷洜缁熻
+        # {"鏉垮潡":{浠g爜}}
+        block_codes = {}
+        limit_up_codes = set()
+        for code in current_code_block_dict:
+            bs = cls.__get_code_blocks(code)
+            for b in bs:
+                if b not in block_codes:
+                    block_codes[b] = set()
+                block_codes[b].add(code)
+            limit_up_codes.add(code)
+        for code in record_code_block_dict:
+            bs = cls.__get_code_blocks(code)
+            for b in bs:
+                if b not in block_codes:
+                    block_codes[b] = set()
+                block_codes[b].add(code)
+        # 鑾峰彇涓婁釜浜ゆ槗鏃ユ定鍋滅殑浠g爜
+        yesterday_codes = kpl_data_manager.get_yesterday_limit_up_codes()
+        if yesterday_codes is None:
+            yesterday_codes = set()
+        temp_block_sequence_dict = {}
+        for code in limit_up_codes:
+            bs = cls.__get_code_blocks(code)
+            for b in bs:
+                # 璁$畻韬綅
+                codes = block_codes[b]
+                total_count = len(codes)
+                # 缁熻鐪熸娑ㄥ仠鏁�
+                limit_up_count = 0
+                limit_up_codes_list = []
+                for c in codes:
+                    if c in limit_up_codes:
+                        limit_up_count += 1
+                        if c not in yesterday_codes:
+                            limit_up_codes_list.append((c, current_code_block_dict[c][2]))
+                # 鑾峰彇棣栨澘浠g爜鐨勬帓浣�
+                limit_up_codes_list.sort(key=lambda x: x[1])
+                index = 1
+                for i in range(0, len(limit_up_codes_list)):
+                    if limit_up_codes_list[i][0] == code:
+                        index = i + 1
+                        break
+                if code not in temp_block_sequence_dict:
+                    temp_block_sequence_dict[code] = []
+                temp_block_sequence_dict[code].append((b, index, total_count, limit_up_count))
+        cls.__first_block_sequence_dict = temp_block_sequence_dict
+
+    @classmethod
+    def get_current_limit_up_sequence(cls, code):
+        """
+        鑾峰彇浠g爜褰撳墠鐨勬澘鍧楄韩浣�
+        @param code:
+        @return:[锛堟澘鍧楀悕绉�,韬綅,鎬绘定鍋滄暟閲忥紝鐩墠娑ㄥ仠鏁伴噺锛塢
+        """
+        return cls.__first_block_sequence_dict.get(code)
+
+
+class LatestLimitUpBlockManager:
+    """
+    鏈�杩戞定鍋滅殑鏉垮潡绠$悊
+    """
+    # 鐪嬫渶杩�2澶╋紝涓嶅寘鍚粖澶�
+    __LATEST_DAY_COUNT = 2
+
+    __days = []
+    # 鐩墠娑ㄥ仠
+    __current_limit_up_day_datas = {}
+    # 鏇炬定鍋�
+    __history_limit_up_day_datas = {}
+
+    # K绾挎暟鎹�
+    __k_datas = {}
+
+    # 浠g爜鐨勬渶楂樻定骞�
+    __k_max_rate = {}
+
+    __code_name_dict = {}
+
+    # 缁熻鏉垮潡鏁版嵁:{"day":{"鏉垮潡":[(娑ㄥ仠鏁�,鐮存澘鏁�, 浠g爜闆嗗悎)]}}
+    __block_day_datas = {}
+
+    __instance = None
+
+    def __new__(cls, *args, **kwargs):
+        if not cls.__instance:
+            cls.__instance = super(LatestLimitUpBlockManager, cls).__new__(cls, *args, **kwargs)
+            cls.__load_datas()
+        return cls.__instance
+
+    @classmethod
+    def __load_datas(cls):
+        # 鍔犺浇鏈�杩戝嚑澶╃殑鏁版嵁
+        __days = HistoryKDatasUtils.get_latest_trading_date_cache(cls.__LATEST_DAY_COUNT)
+        __days = copy.deepcopy(__days)
+        # 涓嶈兘鍖呭惈浠婂ぉ
+        if __days[0] == tool.get_now_date_str():
+            __days.pop(0)
+        cls.__days = __days
+        # 鍔犺浇涔嬪墠6澶╃殑娑ㄥ仠锛屾浘娑ㄥ仠锛屾浘娑ㄥ仠浠g爜鐨勬渶杩�6澶╃殑K绾�
+        for day in __days:
+            limit_up_records = get_history_limit_up_datas(day)
+            cls.__history_limit_up_day_datas[day] = limit_up_records
+            current_limit_up_datas = get_current_limit_up_datas(day)
+            cls.__current_limit_up_day_datas[day] = current_limit_up_datas
+        # 鑾峰彇浠g爜鐨刱绾�
+        __total_codes = set()
+        for d in cls.__current_limit_up_day_datas:
+            __total_codes |= set([dd[3] for dd in cls.__history_limit_up_day_datas[d]])
+        # 鑾峰彇鏈�杩�7澶╃殑k绾挎儏鍐�
+        for code in __total_codes:
+            cls.__get_bars(code)
+        # 缁熻鍓�6澶╃殑鏉垮潡淇℃伅
+        for day in __days:
+            cls.__block_day_datas[day] = cls.__statistics_limit_up_block_infos_by_day(day)
+
+    def set_current_limit_up_data(self, day, datas):
+        self.__current_limit_up_day_datas[day] = datas
+        self.__history_limit_up_day_datas[day] = get_today_history_limit_up_datas_cache()
+        # 鍔犺浇浠g爜K绾挎暟鎹�
+        __total_codes = set([d[0] for d in datas])
+        __total_codes |= set([d[3] for d in self.__history_limit_up_day_datas[day]])
+        for code in __total_codes:
+            self.__get_bars(code)
+
+    @classmethod
+    def __statistics_limit_up_block_infos_by_day(cls, day):
+        """
+        缁熻娑ㄥ仠浠g爜淇℃伅
+        @return:{"鏉垮潡":(娑ㄥ仠鏁�, 鐐告澘鏁�, {鍖呭惈鐨勪唬鐮亇)}
+        """
+        # 缁熻鏉垮潡鐨�
+        current_code_dict = {d[0]: d for d in cls.__current_limit_up_day_datas[day]}
+        block_codes_dict = {}
+        for h in cls.__history_limit_up_day_datas[day]:
+            # 灏嗘澘鍧楁墍鍖呭惈鐨勪唬鐮佸綊绫�
+            code = h[3]
+            cls.__code_name_dict[code] = h[4]
+            blocks = {h[2]} #LimitUpCodesBlockRecordManager().get_radical_buy_blocks(code)
+            if blocks:
+                for b in blocks:
+                    if b not in block_codes_dict:
+                        block_codes_dict[b] = set()
+                    block_codes_dict[b].add(code)
+        fdata = {}
+        for b in block_codes_dict:
+            limit_up_count = 0
+            open_limit_up_count = 0
+            for code in block_codes_dict[b]:
+                if code in current_code_dict:
+                    limit_up_count += 1
+                else:
+                    open_limit_up_count += 1
+
+            fdata[b] = (limit_up_count, open_limit_up_count, block_codes_dict[b])
+        return fdata
+
+    def statistics_limit_up_block_infos(self):
+        """
+        缁熻娑ㄥ仠鏉垮潡鏁版嵁
+        @return:
+        """
+        # 鏉垮潡鍑虹幇鐨勫ぉ鏁�
+        block_count_dict = {}
+        # 鏉垮潡鍑虹幇鐨勪唬鐮�
+        block_codes_dict = {}
+        for day in self.__block_day_datas:
+            for b in self.__block_day_datas[day]:
+                finally_limit_up_count = self.__block_day_datas[day][b][0]
+                if finally_limit_up_count < 3:
+                    continue
+                # 鏉垮潡娑ㄥ仠涓暟
+                if b not in block_count_dict:
+                    block_count_dict[b] = set()
+                if b not in block_codes_dict:
+                    block_codes_dict[b] = set()
+                block_count_dict[b].add(day)
+                block_codes_dict[b] |= self.__block_day_datas[day][b][2]
+        # [(鏉垮潡,娑ㄥ仠鏃ユ湡闆嗗悎)]
+        block_count_list = [(k, block_count_dict[k]) for k in block_count_dict]
+        block_count_list.sort(key=lambda x: len(x[1]), reverse=True)
+        block_count_list = block_count_list[:50]
+        # [(娑ㄥ仠鍘熷洜,绱娑ㄥ仠娆℃暟,杩炵画娆℃暟)]
+        fdatas = []
+        for d in block_count_list:
+            b = d[0]
+            fdata = [b, len(d[1])]
+            temp = []
+            max_continue_count = 0
+            for day in self.__days:
+                if day not in self.__block_day_datas:
+                    continue
+                if b in self.__block_day_datas[day]:
+                    finally_limit_up_count = self.__block_day_datas[day][b][0]
+                else:
+                    finally_limit_up_count = 0
+                if b in self.__block_day_datas[day] and finally_limit_up_count >= 3:
+                    # 鏉垮潡浠g爜鏁伴噺>=3涓�
+                    temp.append(day)
+                else:
+                    c = len(temp)
+                    if c > max_continue_count:
+                        max_continue_count = c
+                    temp.clear()
+            c = len(temp)
+            if c > max_continue_count:
+                max_continue_count = c
+            temp.clear()
+            # 鏈�澶ц繛缁鏁�
+            fdata.append(max_continue_count)
+            # 鏈�楂樻澘
+            max_rate_info = None
+            for code in block_codes_dict[d[0]]:
+                if max_rate_info is None:
+                    max_rate_info = (code, self.__k_max_rate.get(code), self.__code_name_dict.get(code))
+                if max_rate_info[1] < self.__k_max_rate.get(code):
+                    max_rate_info = (code, self.__k_max_rate.get(code), self.__code_name_dict.get(code))
+            fdata.append(max_rate_info)
+
+            # 缁熻浠婂ぉ杩欎釜鏉垮潡涓ぇ浜庝簩鏉跨殑浠g爜鏁伴噺
+            limit_up_counts = 0
+            fdata.append(limit_up_counts)
+            # 鑾峰彇姣忓ぉ鐨勬暟閲�
+            days_datas = []
+            for day in self.__days:
+                if day not in self.__block_day_datas:
+                    continue
+                binfo = self.__block_day_datas[day].get(b)
+                if not binfo:
+                    days_datas.append((0, 0))
+                else:
+                    days_datas.append((binfo[0], binfo[1]))
+            fdata.append(days_datas)
+            fdatas.append(fdata)
+        return fdatas
+
+    @classmethod
+    def __get_bars(cls, code):
+        """
+        鑾峰彇K绾�
+        @param code:
+        @return:
+        """
+        if code in cls.__k_datas:
+            return cls.__k_datas[code]
+        volumes_data = None
+        if cls.__days:
+            volumes_data = HistoryKDataManager().get_history_bars(code, cls.__days[1])
+            if volumes_data:
+                volumes_data = volumes_data[:cls.__LATEST_DAY_COUNT]
+                cls.__k_datas[code] = volumes_data
+        if not volumes_data:
+            volumes_data = init_data_util.get_volumns_by_code(code, cls.__LATEST_DAY_COUNT)
+            if volumes_data:
+                cls.__k_datas[code] = volumes_data
+        # 鑾峰彇鏈�澶ф定骞�
+        min_price = volumes_data[-1]["low"]
+        for d in volumes_data:
+            if min_price > d["low"]:
+                min_price = d["low"]
+        rate = int((volumes_data[0]["close"] - min_price) * 100 / min_price)
+        cls.__k_max_rate[code] = rate
+        return cls.__k_datas.get(code)
+
+
+if __name__ == "__main__":
+    # datas = LatestLimitUpBlockManager().statistics_limit_up_block_infos()
+    # print(datas)
+    code = "600126"
+    blocks = LimitUpCodesBlockRecordManager().get_radical_buy_blocks(code)
+    print(blocks)
\ No newline at end of file

--
Gitblit v1.8.0