From 75304450cd47c427ade3ad40a7556fc8328a3f29 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 31 七月 2024 18:39:59 +0800
Subject: [PATCH] 板块轮动统计/G撤比例修改

---
 third_data/code_plate_key_manager.py |  207 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 205 insertions(+), 2 deletions(-)

diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py
index 5aa2814..7ecfb5a 100644
--- a/third_data/code_plate_key_manager.py
+++ b/third_data/code_plate_key_manager.py
@@ -10,9 +10,11 @@
 import constant
 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 third_data import kpl_block_util, kpl_api, kpl_util, kpl_limit_up_data_manager
 from settings.trade_setting import MarketSituationManager
-from utils import global_util, tool, buy_condition_util
+from third_data.history_k_data_manager import HistoryKDataManager
+from third_data.history_k_data_util import HistoryKDatasUtils
+from utils import global_util, tool, buy_condition_util, init_data_util
 from log_module import log, async_log_util
 from db import redis_manager_delegate as redis_manager
 
@@ -1016,5 +1018,206 @@
             can_buy_blocks, unique, msg, can_buy_strong_blocks, keys, active_buy_blocks)
 
 
+class LatestLimitUpBlockManager:
+    """
+    鏈�杩戞定鍋滅殑鏉垮潡绠$悊
+    """
+    # 鐪嬫渶杩�7澶�
+    __LATEST_DAY_COUNT = 7
+
+    __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):
+        # 鍔犺浇鏈�杩�7澶╃殑鏁版嵁
+        __days = HistoryKDatasUtils.get_latest_trading_date_cache(cls.__LATEST_DAY_COUNT - 1)
+        now_day = tool.get_now_date_str()
+        if __days[0] != now_day:
+            __days.insert(0, now_day)
+        cls.__days = __days
+        # 鍔犺浇涔嬪墠6澶╃殑娑ㄥ仠锛屾浘娑ㄥ仠锛屾浘娑ㄥ仠浠g爜鐨勬渶杩�6澶╃殑K绾�
+        for day in __days:
+            if day == now_day:
+                continue
+            limit_up_records = kpl_limit_up_data_manager.get_history_limit_up_datas(day)
+            cls.__history_limit_up_day_datas[day] = limit_up_records
+            current_limit_up_datas = kpl_limit_up_data_manager.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:
+            if day == now_day:
+                continue
+            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] = kpl_limit_up_data_manager.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]}
+        # history_code_dict = {d[3]: d for d in self.__history_limit_up_day_datas[day]}
+        block_codes_dict = {}
+        for h in cls.__history_limit_up_day_datas[day]:
+            cls.__code_name_dict[h[3]] = h[4]
+            if h[2] not in block_codes_dict:
+                block_codes_dict[h[2]] = set()
+            block_codes_dict[h[2]].add(h[3])
+        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:
+        """
+        # 鍙粺璁′粖澶╃殑鏁版嵁
+        now_day = tool.get_now_date_str()
+        block_dict = self.__statistics_limit_up_block_infos_by_day(now_day)
+        self.__block_day_datas[now_day] = block_dict
+        # 鏉垮潡鍑虹幇鐨勫ぉ鏁�
+        block_count_dict = {}
+        # 鏉垮潡鍑虹幇鐨勪唬鐮�
+        block_codes_dict = {}
+        for day in self.__block_day_datas:
+            for b in self.__block_day_datas[day]:
+                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: x[1], reverse=True)
+        block_count_list = block_count_list[:20]
+        # [(娑ㄥ仠鍘熷洜,绱娑ㄥ仠娆℃暟,杩炵画娆℃暟)]
+        fdatas = []
+        today_records_code_dict = {d[3]: d for d in self.__history_limit_up_day_datas.get(now_day)}
+        for d in block_count_list:
+            b = d[0]
+            fdata = [d[0], len(d[1])]
+            temp = []
+            max_continue_count = 0
+            for day in self.__days:
+                if d[0] in self.__block_day_datas[day]:
+                    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
+            for code in block_codes_dict[d[0]]:
+                if code in today_records_code_dict and today_records_code_dict[code][12] != '棣栨澘':
+                    limit_up_counts += 1
+            fdata.append(limit_up_counts)
+            # 鑾峰彇姣忓ぉ鐨勬暟閲�
+            days_datas = []
+            for day in self.__days:
+                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 - 1]
+                cls.__k_datas[code] = volumes_data
+        if not volumes_data:
+            volumes_data = init_data_util.get_volumns_by_code(code, cls.__LATEST_DAY_COUNT - 1)
+            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__":
     pass

--
Gitblit v1.8.0