Administrator
2023-12-21 81f328532e366eef171b71810b221a9294dda78f
third_data/code_plate_key_manager.py
@@ -43,11 +43,8 @@
        final_blocks = copy.deepcopy(blocks)
        if len(blocks) > 2:
            final_blocks.clear()
            # 根据涨幅排序
            blocks.sort(key=lambda x: x[2])
            blocks.reverse()
            for b in blocks:
                if b[1] not in constant.KPL_INVALID_BLOCKS:
                if b not in constant.KPL_INVALID_BLOCKS:
                    final_blocks.append(b)
            if len(final_blocks) < 2:
                final_blocks = blocks
@@ -100,7 +97,7 @@
                if price_rate > 0.07:
                    jx_blocks_info = self.get_jx_blocks_cache(code)
                    if not jx_blocks_info:
                        blocks = kpl_api.getCodeJingXuanBlocks(code)
                        blocks = kpl_api.getCodeBlocks(code)
                        self.save_jx_blocks(code, blocks, current_limit_up_blocks)
                        async_log_util.info(logger_kpl_block_can_buy, f"{code}:获取到精选板块-{blocks}")
                    else:
@@ -118,14 +115,14 @@
                            if time.time() - jx_blocks_info[1] > UPDATE_TIME_SPACE:
                                # 距离上次更新时间过去了5分钟
                                blocks = kpl_api.getCodeJingXuanBlocks(code)
                                blocks = kpl_api.getCodeBlocks(code)
                                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):
                        blocks = kpl_api.getCodeJingXuanBlocks(code)
                        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}")
        except Exception as e:
@@ -450,7 +447,7 @@
            jingxuan_block_info = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code, by=True)
        if jingxuan_block_info:
            jingxuan_blocks = jingxuan_block_info[0]
            k4 |= set([x[1] for x in jingxuan_blocks])
            k4 |= set(jingxuan_blocks)  # set([x[1] for x in jingxuan_blocks])
        for k in [k1, k11, k2, k3, k4]:
            keys |= k
@@ -492,13 +489,21 @@
    # 返回内容(是否可买, 是否为独苗, 描述信息)
    @classmethod
    def __is_block_can_buy(cls, code, block, current_limit_up_datas, code_limit_up_reason_dict,
                           yesterday_current_limit_up_codes, limit_up_record_datas, current_limit_up_block_codes_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):
        # 独苗判断
        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 not block_codes:
            return False, True, f"{block}:板块无涨停", False
            # 高位板泛化板块中无板块
            if not high_level_block_codes.get(block):
                return False, True, f"{block}:板块无涨停", False
        elif len(block_codes) == 1 and code in block_codes:
            return False, True, f"{block}:板块只有当前代码涨停", False
            if not high_level_block_codes.get(block):
                return False, True, f"{block}:板块只有当前代码涨停", 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)
@@ -509,7 +514,10 @@
        is_strong_block = False
        for d in current_limit_up_datas:
            if d[5] != block:
                continue
                general_blocks = high_level_code_blocks.get(d[0])
                if not general_blocks or block not in general_blocks:
                    # 没在泛化板块中
                    continue
            if d[4].find("连板") > 0:
                if d[4].replace("连板", "").isdigit():
                    count = int(d[4].replace("连板", ""))
@@ -517,6 +525,11 @@
                        is_strong_block = True
                        break
        if not is_strong_block:
            temp_block_codes = set(copy.deepcopy(block_codes))
            temp_block_codes.discard(code)
            if len(temp_block_codes) >= 3:
                is_strong_block = True
        max_rank = 2
        #  强势板块买老四
        if is_strong_block:
@@ -531,6 +544,12 @@
        if current_open_limit_up_codes and yesterday_current_limit_up_codes:
            current_open_limit_up_codes -= yesterday_current_limit_up_codes
        # 获取代码的初次涨停时间
        first_limit_up_time = time.time()
        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,
@@ -539,7 +558,8 @@
                                                                                                exclude_first_codes,
                                                                                                len(
                                                                                                    current_open_limit_up_codes),
                                                                                                shsz=True)
                                                                                                shsz=True,
                                                                                                limit_up_time=first_limit_up_time)
        # 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,
@@ -552,6 +572,11 @@
        if current_shsz_rank < len(current_open_limit_up_codes) + max_rank:
            return True, False, f"【{block}】前排代码:{current_shsz_rank}", is_strong_block
        else:
            # 看自由流通市值是否小于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)
                if zyltgb and zyltgb < 20 * 100000000:
                    return True, False, f"【{block}】强势板块 自由流通市值({zyltgb})小于20亿", is_strong_block
            return False, False, f"【{block}】前排代码:{front_current_shsz_rank_codes} 超过{len(current_open_limit_up_codes) + max_rank}个", is_strong_block
        # 过时的代码
@@ -583,7 +608,8 @@
    @classmethod
    def get_can_buy_block(cls, code, current_limit_up_datas, limit_up_record_datas, yesterday_current_limit_up_codes,
                          before_blocks_dict, current_limit_up_block_codes_dict):
                          before_blocks_dict, current_limit_up_block_codes_dict, high_level_general_code_blocks,
                          high_level_general_block_codes):
        # 加载涨停代码的目标板块
        def load_code_block():
            if limit_up_record_datas:
@@ -608,7 +634,7 @@
        # 始终获取精选板块
        if True:
            # 获取
            if k4 and not keys:
            if k4:
                # 当涨停原因没有时才取精选板块
                keys |= k4
                keys = keys - constant.KPL_INVALID_BLOCKS
@@ -627,7 +653,9 @@
                                                                     code_limit_up_reason_dict,
                                                                     yesterday_current_limit_up_codes,
                                                                     limit_up_record_datas,
                                                                     current_limit_up_block_codes_dict)
                                                                     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))
        return fresults, keys
@@ -646,10 +674,20 @@
    @classmethod
    def __compute_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas,
                                 yesterday_current_limit_up_codes, before_blocks_dict,
                                 current_limit_up_block_codes_dict):
                                 current_limit_up_block_codes_dict, high_level_general_code_blocks):
        # 根据代码泛化板块获取泛化板块的代码集合
        high_level_general_block_codes = {}
        for c in high_level_general_code_blocks:
            blocks = high_level_general_code_blocks[c]
            for b in blocks:
                if b not in high_level_general_block_codes:
                    high_level_general_block_codes[b] = set()
                high_level_general_block_codes[b].add(c)
        blocks_compute_results, keys = cls.get_can_buy_block(code, current_limit_up_datas,
                                                             limit_up_record_datas, yesterday_current_limit_up_codes,
                                                             before_blocks_dict, current_limit_up_block_codes_dict)
                                                             before_blocks_dict, current_limit_up_block_codes_dict,
                                                             high_level_general_code_blocks,
                                                             high_level_general_block_codes)
        if not blocks_compute_results:
            return False, True, f"没有找到板块", [], keys
        codes_delegate = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache(
@@ -723,16 +761,47 @@
        return can_buy_blocks, False, ",".join(msg_list), can_buy_strong_blocks, keys
    # 更新代码板块判断是否可以买的结果
    # high_level_general_code_blocks 高位泛化板块
    @classmethod
    def update_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas,
                              yesterday_current_limit_up_codes,
                              latest_current_limit_up_records,
                              before_blocks_dict, current_limit_up_block_codes_dict):
        yesterday_current_limit_up_codes = set()
        yesterday_current_limit_up_records_dict = {}
        yesterday_current_limit_up_records = latest_current_limit_up_records[0][1]
        if yesterday_current_limit_up_records:
            for r in yesterday_current_limit_up_records:
                yesterday_current_limit_up_codes.add(r[0])
                yesterday_current_limit_up_records_dict[r[0]] = r
        high_level_general_code_blocks = {}
        # 是否是3板及以上的高位板
        for r in current_limit_up_datas:
            if r[4].find("连板") > 0:
                if r[4].replace("连板", "").isdigit():
                    count = int(r[4].replace("连板", ""))
                    if count >= 3:
                        latest_datas = latest_current_limit_up_records[:count-1]
                        # 是高位板
                        # 当日精选
                        blocks = set(r[6].split("、"))
                        for d in latest_datas:
                            for dd in d[1]:
                                if dd[0] == r[0]:
                                    blocks.add(dd[5])
                                    break
                        f_blocks = []
                        for b in blocks:
                            if b:
                                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)
                                                                                                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)