| | |
| | | |
| | | import constant |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from third_data import kpl_block_util |
| | | from third_data import kpl_block_util, kpl_api |
| | | from utils import global_util, tool |
| | | from log_module import log |
| | | from log_module import log, async_log_util |
| | | from db import redis_manager_delegate as redis_manager |
| | | |
| | | from log_module.log import logger_kpl_limit_up, logger_kpl_block_can_buy, logger_kpl_debug |
| | | from log_module.log import logger_kpl_limit_up, logger_kpl_block_can_buy |
| | | from third_data.kpl_util import KPLPlatManager |
| | | from trade import trade_manager, l2_trade_util |
| | | |
| | | |
| | | # 代码精选板块管理 |
| | | class KPLCodeJXBlockManager: |
| | | __db = 3 |
| | | __redisManager = redis_manager.RedisManager(3) |
| | | __code_blocks = {} |
| | | # 备用 |
| | | __code_by_blocks = {} |
| | | |
| | | __instance = None |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(KPLCodeJXBlockManager, cls).__new__(cls, *args, **kwargs) |
| | | return cls.__instance |
| | | |
| | | def __get_redis(self): |
| | | return self.__redisManager.getRedis() |
| | |
| | | |
| | | # 保存前2条数据 |
| | | if by: |
| | | RedisUtils.setex(self.__get_redis(), f"kpl_jx_blocks_by-{code}", tool.get_expire(), json.dumps(blocks)) |
| | | RedisUtils.setex_async(self.__db, f"kpl_jx_blocks_by-{code}", tool.get_expire(), json.dumps(blocks)) |
| | | self.__code_by_blocks[code] = blocks |
| | | else: |
| | | RedisUtils.setex(self.__get_redis(), f"kpl_jx_blocks-{code}", tool.get_expire(), json.dumps(blocks)) |
| | | RedisUtils.setex_async(self.__db, f"kpl_jx_blocks-{code}", tool.get_expire(), json.dumps(blocks)) |
| | | self.__code_blocks[code] = blocks |
| | | |
| | | # 获取精选板块 |
| | |
| | | return self.__code_by_blocks.get(code) |
| | | else: |
| | | return self.__code_blocks.get(code) |
| | | |
| | | # 从网络上加载精选板块 |
| | | def load_jx_blocks(self, 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) |
| | | # 如果涨幅大于7%就读取板块 |
| | | price_rate = (buy_1_price - pre_close_price) / pre_close_price |
| | | if price_rate > 0.07: |
| | | if not self.get_jx_blocks_cache(code): |
| | | blocks = kpl_api.getCodeJingXuanBlocks(code) |
| | | self.save_jx_blocks(code, 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) |
| | | self.save_jx_blocks(code, blocks, by=True) |
| | | async_log_util.info(logger_kpl_block_can_buy, f"{code}:获取到精选板块(备用)-{blocks}") |
| | | |
| | | |
| | | |
| | | # 开盘啦禁止交易板块管理 |
| | |
| | | @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_cache() |
| | | reasons = reasons - forbidden_plates |
| | | temp_set = keys & reasons |
| | | log.logger_kpl_debug.debug("市场流入前5匹配结果:{}", temp_set) |
| | | if temp_set: |
| | | return True, temp_set |
| | | else: |
| | |
| | | self.__history_limit_up_reason_dict[code] = set(reasons) |
| | | RedisUtils.setex(self.__get_redis(), f"kpl_his_limit_up_reason-{code}", tool.get_expire(), |
| | | json.dumps(list(reasons))) |
| | | logger_kpl_debug.debug(f"设置历史涨停原因:{code}-{reasons}") |
| | | |
| | | # 如果返回值不为None表示已经加载过历史原因了 |
| | | def get_history_limit_up_reason(self, code): |
| | |
| | | @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): |
| | | log.logger_kpl_debug.info(f"判断板块是否可买:{block}") |
| | | # log.logger_kpl_debug.info(f"判断板块是否可买:{block}") |
| | | # is_top_8_record, top_8_record = kpl_block_util.is_record_top_block(code, block, limit_up_record_datas, |
| | | # yesterday_current_limit_up_codes, 50) |
| | | # is_top_4_current, top_4_current = kpl_block_util.is_current_top_block(code, block, current_limit_up_datas, |
| | |
| | | # msg_list.append(f"历史top20(涨停数量:{len(top_8_record)})") |
| | | |
| | | # 获取主板实时身位,剔除高位板 |
| | | current_shsz_rank = kpl_block_util.get_code_current_rank(code, block, current_limit_up_datas, |
| | | code_limit_up_reason_dict, |
| | | yesterday_current_limit_up_codes, shsz=True) |
| | | record_shsz_rank = kpl_block_util.get_code_record_rank(code, block, limit_up_record_datas, |
| | | code_limit_up_reason_dict, |
| | | yesterday_current_limit_up_codes, shsz=True) |
| | | current_shsz_rank, front_current_shsz_rank_codes = kpl_block_util.get_code_current_rank(code, block, |
| | | current_limit_up_datas, |
| | | code_limit_up_reason_dict, |
| | | yesterday_current_limit_up_codes, |
| | | shsz=True) |
| | | 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, |
| | | yesterday_current_limit_up_codes, |
| | | shsz=True) |
| | | # 获取主板历史身位 |
| | | |
| | | pen_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas, |
| | | code_limit_up_reason_dict) |
| | | if pen_limit_up_codes: |
| | | open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas, |
| | | code_limit_up_reason_dict) |
| | | if open_limit_up_codes: |
| | | # 主板开1 |
| | | if current_shsz_rank < len(pen_limit_up_codes) + 1 and record_shsz_rank < len(pen_limit_up_codes) + 1: |
| | | if current_shsz_rank < len(open_limit_up_codes) + 1 and record_shsz_rank < len(open_limit_up_codes) + 2: |
| | | # 属于龙1,龙2 |
| | | return True, f"{tool.get_now_time_str()} {block}:top10涨停板块,主板开1({pen_limit_up_codes}),属于主板前龙{len(pen_limit_up_codes) + 1}(实时身位-{current_shsz_rank}/{len(current_limit_up_datas)})" |
| | | return True, f"{tool.get_now_time_str()} {block}:top10涨停板块,主板开1({open_limit_up_codes}),属于主板前龙{len(open_limit_up_codes) + 1}(实时身位-{current_shsz_rank}/{len(current_limit_up_datas)})" |
| | | else: |
| | | if record_shsz_rank >= len(pen_limit_up_codes) + 1: |
| | | if record_shsz_rank >= len(open_limit_up_codes) + 1: |
| | | cls.__remove_from_l2(code, f"{code}根据身位禁止买入:【{block}】历史身位{record_shsz_rank}") |
| | | return False, f"板块-{block}: top4涨停板块,主板开1({pen_limit_up_codes}),不为主板前龙{len(pen_limit_up_codes) + 1}(实时身位-{current_shsz_rank},历史身位-{record_shsz_rank})" |
| | | return False, f"板块-{block}: top4涨停板块,主板开1({open_limit_up_codes}),不为主板前龙{len(open_limit_up_codes) + 1}(实时身位-{current_shsz_rank}:{front_current_shsz_rank_codes},历史身位-{record_shsz_rank})" |
| | | else: |
| | | if current_shsz_rank == 0 and record_shsz_rank < 2: |
| | | return True, f"{tool.get_now_time_str()} {block}:top4涨停板块,非主板开1,属于龙1,实时涨停列表数量({len(current_limit_up_datas)})" |
| | |
| | | if record_shsz_rank >= 2: |
| | | cls.__remove_from_l2(code, f"{code}根据身位禁止买入:【{block}】历史身位{record_shsz_rank}") |
| | | |
| | | return False, f"板块-{block}: top4涨停板块,非主板开1,不为主板龙1(实时身位-{current_shsz_rank},历史身位-{record_shsz_rank})" |
| | | return False, f"板块-{block}: top4涨停板块,非主板开1,不为主板龙1(实时身位-{current_shsz_rank}:{front_current_shsz_rank_codes},历史身位-{record_shsz_rank})" |
| | | |
| | | # 获取可以买的板块 |
| | | # current_limit_up_datas: 今日实时涨停 |
| | |
| | | |
| | | # 获取目标代码板块 |
| | | keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code) |
| | | log.logger_kpl_debug.info("{}关键词:今日-{},今日历史-{},历史-{},二级行业-{},代码板块-{}", code, k1, k11, k2, k3, k4) |
| | | # log.logger_kpl_debug.info("{}关键词:今日-{},今日历史-{},历史-{},二级行业-{},代码板块-{}", code, k1, k11, k2, k3, k4) |
| | | keys = set() |
| | | if k1: |
| | | for k in k1: |
| | |
| | | keys |= k4 |
| | | keys = keys - constant.KPL_INVALID_BLOCKS |
| | | |
| | | log.logger_kpl_debug.info("{}最终关键词:{}", code, keys) |
| | | # log.logger_kpl_debug.info("{}最终关键词:{}", code, keys) |
| | | |
| | | # 涨停列表中匹配关键词,返回(板块:代码集合),代码集合中已经排除自身 |
| | | if not keys: |
| | |
| | | before_blocks_dict) |
| | | if not blocks: |
| | | return False, block_msg |
| | | log.logger_kpl_debug.info(f"{code}:获取委托/买入代码") |
| | | codes_delegate = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache( |
| | | {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER})) |
| | | codes_success = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache( |
| | |
| | | trade_codes_blocks_dict = {} |
| | | # 已经成交的板块 |
| | | trade_success_blocks_count = {} |
| | | log.logger_kpl_debug.info(f"{code}:获取代码板块") |
| | | for c in codes: |
| | | keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(c) |
| | | # 实时涨停原因 |
| | | trade_codes_blocks_dict[c] = k1_ | k4_ |
| | | # 统计板块中的代码 |
| | | log.logger_kpl_debug.info(f"{code}:统计板块中的代码") |
| | | trade_block_codes_dict = {} |
| | | for c in trade_codes_blocks_dict: |
| | | for b in trade_codes_blocks_dict[c]: |
| | |
| | | trade_block_codes_dict[b].add(c) |
| | | |
| | | # ---------------------------------加载已经下单/成交的代码信息------------end------------- |
| | | log.logger_kpl_debug.info(f"{code}:开始计算是否可以买") |
| | | msg_list = [] |
| | | for key in blocks: |
| | | # 板块中已经有成交的就不下单了 |