| | |
| | | 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} 耗时:{int(time.time() - start_time)}s") |
| | | 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 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) |
| | |
| | | def __get_redis(self): |
| | | return self.__redisManager.getRedis() |
| | | |
| | | # 获取今日涨停数据,格式:[(代码,涨停原因)] |
| | | # 获取今日涨停数据,格式:[(代码,涨停原因,精选板块列表)] |
| | | def set_today_limit_up(self, datas): |
| | | temp_dict = {} |
| | | if datas: |
| | |
| | | self.set_today_total_limit_up(datas) |
| | | |
| | | # 设置今日历史涨停数据 |
| | | def set_today_total_limit_up(self, datas): |
| | | # 格式:(代码,涨停原因,精选板块列表) |
| | | @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] |
| | | # 设置涨停代码的板块及原因 |
| | | 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): |
| | |
| | | def get_plate_keys(self, code): |
| | | 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) |
| | |
| | | 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 |
| | | |
| | | |
| | |
| | | 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_limit_up_datas, limit_up_record_datas, |
| | | yesterday_current_limit_up_codes): |
| | | code_limit_up_reason_dict = {} |
| | | for d in limit_up_record_datas: |
| | | code_limit_up_reason_dict[d[3]] = d[2] |
| | | |
| | | # 前面有高位板/非主板涨停且曾涨停主板排老大则激进买 |
| | | block_codes_info = [] |
| | | contains_code = False |
| | | for d in current_limit_up_datas: |
| | | reason = kpl_util.get_current_limit_up_reason(d) |
| | | if reason != block: |
| | | continue |
| | | if d[0] == code: |
| | | contains_code = True |
| | | # 格式:[代码,涨停时间,是否高位板, 自由流通市值] |
| | | block_codes_info.append((d[0], d[2], d[0] in yesterday_current_limit_up_codes, d[7])) |
| | | if not contains_code: |
| | | block_codes_info.append((code, int(time.time()), False, global_util.zyltgb_map.get(code))) |
| | | block_codes_info.sort(key=lambda x: x[1]) |
| | | # 暂存在当前代码之前涨停的高位板/非主板代码 |
| | | before_codes = [] |
| | | for info in block_codes_info: |
| | | if info[0] == code: |
| | | break |
| | | if info[2] or not tool.is_shsz_code(info[0]): |
| | | before_codes.append(info[0]) |
| | | if before_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, |
| | | yesterday_current_limit_up_codes, |
| | | shsz=True) |
| | | if record_shsz_rank == 0: |
| | | # 历史主板老大 |
| | | return True, f"主板老大且前面有非主板/高位板涨停,先于涨停代码:{before_codes}" |
| | | # 排除自己有3个涨停,且这4个中自由流通市值最小,且涨停过的代码中主板排名前4 |
| | | if len(block_codes_info) >= 4: |
| | | temp_codes = [] |
| | | for info in block_codes_info: |
| | | # 不是高位板且是主板 |
| | | if not info[2] and tool.is_shsz_code(info[0]): |
| | | # 格式:(代码, 自由流通市值) |
| | | temp_codes.append((info[0], info[3])) |
| | | # 判断是否为主板中自由市值最小 |
| | | temp_codes.sort(key=lambda x: x[1]) |
| | | if temp_codes[0][0] == code: |
| | | # 自由市值最小 |
| | | # 判断历史身位 |
| | | 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) |
| | | if record_shsz_rank < 4: |
| | | return True, f"板块有大于3票涨停,且自由流通市值最小,身位({record_shsz_rank})前4" |
| | | |
| | | 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, |
| | |
| | | block_codes = current_limit_up_block_codes_dict.get(block) |
| | | if block_codes is None: |
| | | block_codes = set() |
| | | # 计算是否需要激进买入 |
| | | active_buy = cls.__is_need_active_buy(code, block, current_limit_up_datas, |
| | | limit_up_record_datas, |
| | | yesterday_current_limit_up_codes) |
| | | |
| | | if not block_codes: |
| | | # 高位板泛化板块中无板块 |
| | | if not high_level_block_codes.get(block): |
| | | return False, True, f"{block}:板块无涨停", False |
| | | return False, True, f"{block}:板块无涨停", False, active_buy |
| | | elif len(block_codes) == 1 and code in block_codes: |
| | | if not high_level_block_codes.get(block): |
| | | return False, True, f"{block}:板块只有当前代码涨停", False |
| | | return False, True, f"{block}:板块只有当前代码涨停", False, active_buy |
| | | # 可以买的最大排名 |
| | | # open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas, |
| | | # code_limit_up_reason_dict) |
| | |
| | | # 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, 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, 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 |
| | | # 看自由流通市值是否小于20亿 |
| | | if is_strong_block and current_shsz_rank < len(current_open_limit_up_codes) + max_rank + 1: |
| | | zyltgb_as_yi = round(global_util.zyltgb_map.get(code) / 100000000, 2) if code in global_util.zyltgb_map else None |
| | | 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_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 |
| | | return False, False, f"【{block}】前排代码:{front_current_shsz_rank_codes} 超过{len(current_open_limit_up_codes) + max_rank}个", is_strong_block |
| | | return True, False, f"【{block}】强势板块 自由流通市值({zyltgb_as_yi})大于{zylt_threshold_as_yi[2]}亿 小于{zylt_threshold_as_yi[3]}亿", is_strong_block, active_buy |
| | | return False, False, f"【{block}】前排代码:{front_current_shsz_rank_codes} 超过{len(current_open_limit_up_codes) + max_rank}个", is_strong_block, active_buy |
| | | |
| | | # 过时的代码 |
| | | # if open_limit_up_codes: |
| | |
| | | |
| | | # 获取目标代码板块 |
| | | keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code) |
| | | # log.logger_kpl_debug.info("{}关键词:今日-{},今日历史-{},历史-{},二级行业-{},代码板块-{}", 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) |
| | | |
| | |
| | | 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, active_buy_info = 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, active_buy_info)) |
| | | return fresults, keys |
| | | |
| | | # 是否可以下单 |
| | | # 返回:可以买的板块,是否独苗,消息 |
| | | # 可买的板块, 是否独苗, 消息, 可买的强势板块, 关键词, 激进买的板块 |
| | | @classmethod |
| | | def can_buy(cls, code): |
| | | if constant.TEST: |
| | |
| | | # 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, |
| | |
| | | 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 |
| | |
| | | can_buy_strong_blocks.append(r[0]) |
| | | if r[3]: |
| | | msg_list.append(r[3]) |
| | | if r[5][0]: |
| | | active_buy_blocks.append(r[0]) |
| | | msg_list.append(f"【{r[0]}】激进买入({r[5][1]})") |
| | | |
| | | 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 |
| | | |
| | | # 更新代码板块判断是否可以买的结果 |
| | | # high_level_general_code_blocks 高位泛化板块 |
| | |
| | | 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__": |