| | |
| | | import json |
| | | |
| | | import constant |
| | | from code_attribute.code_nature_analyse import HighIncreaseCodeManager |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from third_data import kpl_block_util, kpl_api |
| | | from utils import global_util, tool |
| | |
| | | l2_trade_util.forbidden_trade(code, msg=msg) |
| | | logger_kpl_block_can_buy.info(msg) |
| | | |
| | | # 返回内容(是否可买, 是否为独苗, 描述信息) |
| | | @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}") |
| | | # 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, |
| | | # yesterday_current_limit_up_codes, 50) |
| | | # is_top_4 = is_top_8_record and is_top_4_current |
| | | # msg_list.append(f"\n实时top10(涨停数量:{len(current_limit_up_datas)})") |
| | | # msg_list.append(f"历史top20(涨停数量:{len(top_8_record)})") |
| | | yesterday_current_limit_up_codes, limit_up_record_datas, current_limit_up_block_codes_dict): |
| | | |
| | | # 需要排除的老大的代码 |
| | | exclude_first_codes = HighIncreaseCodeManager().list_all() |
| | | |
| | | # 获取主板开1的代码 |
| | | # 独苗判断 |
| | | block_codes = current_limit_up_block_codes_dict.get(block) |
| | | if not block_codes: |
| | | return False, True, "" |
| | | elif len(block_codes) == 1 and code in block_codes: |
| | | return False, True, "" |
| | | # 可以买的最大排名 |
| | | open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas, |
| | | code_limit_up_reason_dict) |
| | | current_open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes_current(code, block, |
| | | current_limit_up_datas) |
| | | |
| | | max_rank = 2 |
| | | # 如果是强势板块(有1个开1/有非主板涨停/有高位板)可以买到老三 |
| | | msg_list = [] |
| | | for bc in block_codes: |
| | | if bc in current_open_limit_up_codes: |
| | | max_rank = 3 |
| | | msg_list.append(f"{bc}开1") |
| | | break |
| | | elif bc in yesterday_current_limit_up_codes: |
| | | max_rank = 3 |
| | | msg_list.append(f"{bc}高位板") |
| | | break |
| | | elif bc.find("00") != 0 and bc.find("60") != 0: |
| | | max_rank = 3 |
| | | msg_list.append(f"{bc}创业板/科创板") |
| | | break |
| | | |
| | | # 判断身位 |
| | | |
| | | # 需要排除的老大的代码 |
| | | exclude_first_codes = set() # HighIncreaseCodeManager().list_all() |
| | | |
| | | # 获取主板开1的代码 |
| | | |
| | | # 剔除高位板 |
| | | if open_limit_up_codes and yesterday_current_limit_up_codes: |
| | | open_limit_up_codes -= yesterday_current_limit_up_codes |
| | | |
| | | |
| | | # 获取主板实时身位,剔除高位板 |
| | | 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, |
| | | exclude_first_codes, |
| | | len(open_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, |
| | | # exclude_first_codes, |
| | | # len( |
| | | # open_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) |
| | | |
| | | if open_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({open_limit_up_codes}),属于主板前龙{len(open_limit_up_codes) + 1}(实时身位-{current_shsz_rank}:{front_current_shsz_rank_codes}/{len(current_limit_up_datas)})" |
| | | else: |
| | | 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({open_limit_up_codes}),不为主板前龙{len(open_limit_up_codes) + 1}(实时身位-{current_shsz_rank}:{front_current_shsz_rank_codes},历史身位-{record_shsz_rank})" |
| | | if record_shsz_rank < len(open_limit_up_codes) + max_rank: |
| | | return True, False, f"【{block}】前排代码:{record_shsz_rank_codes}" |
| | | 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)})" |
| | | else: |
| | | if record_shsz_rank >= 2: |
| | | cls.__remove_from_l2(code, f"{code}根据身位禁止买入:【{block}】历史身位{record_shsz_rank}") |
| | | return True, False, f"【{block}】前排代码:{record_shsz_rank_codes} 超过{len(open_limit_up_codes) + max_rank}个" |
| | | |
| | | return False, f"板块-{block}: top4涨停板块,非主板开1,不为主板龙1(实时身位-{current_shsz_rank}:{front_current_shsz_rank_codes},历史身位-{record_shsz_rank})" |
| | | # 过时的代码 |
| | | # if open_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({open_limit_up_codes}),属于主板前龙{len(open_limit_up_codes) + 1}(实时身位-{current_shsz_rank}:{front_current_shsz_rank_codes}/{len(current_limit_up_datas)})" |
| | | # else: |
| | | # 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({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)})" |
| | | # else: |
| | | # 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}:{front_current_shsz_rank_codes},历史身位-{record_shsz_rank})" |
| | | |
| | | # 获取可以买的板块 |
| | | # current_limit_up_datas: 今日实时涨停 |
| | |
| | | # limit_up_record_datas:今日历史涨停 |
| | | # yesterday_current_limit_up_codes : 昨日涨停代码 |
| | | # before_blocks_dict:历史涨停原因 |
| | | # 返回板块的计算结果[(板块名称,是否可买,是否是独苗,信息)] |
| | | |
| | | @classmethod |
| | | def get_can_buy_block(cls, code, current_limit_up_datas, limit_up_record_datas, yesterday_current_limit_up_codes, |
| | | before_blocks_dict): |
| | | before_blocks_dict, current_limit_up_block_codes_dict): |
| | | # 加载涨停代码的目标板块 |
| | | def load_code_block(): |
| | | if limit_up_record_datas: |
| | |
| | | # log.logger_kpl_debug.info("{}最终关键词:{}", code, keys) |
| | | |
| | | # 涨停列表中匹配关键词,返回(板块:代码集合),代码集合中已经排除自身 |
| | | |
| | | fresults = [] |
| | | if not keys: |
| | | return None, "尚未找到涨停原因" |
| | | return fresults |
| | | code_limit_up_reason_dict = {} |
| | | load_code_block() |
| | | msg_list = [] |
| | | |
| | | can_buy_blocks = [] |
| | | for block in keys: |
| | | |
| | | can_buy, msg = cls.__is_block_can_buy(code, block, current_limit_up_datas, code_limit_up_reason_dict, |
| | | yesterday_current_limit_up_codes, limit_up_record_datas) |
| | | if can_buy: |
| | | can_buy_blocks.append((block, msg)) |
| | | else: |
| | | msg_list.append(msg) |
| | | if len(can_buy_blocks) == len(keys): |
| | | blocks = [x[0] for x in can_buy_blocks] |
| | | blocks_msg = "\n".join([x[1] for x in can_buy_blocks]) |
| | | return blocks, blocks_msg |
| | | |
| | | return None, "\n".join(msg_list) |
| | | can_buy, unique, msg = cls.__is_block_can_buy(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) |
| | | fresults.append((block, can_buy, unique, msg)) |
| | | return fresults |
| | | |
| | | # 是否可以下单 |
| | | # 返回:是否可以下单,消息,板块类型 |
| | | @classmethod |
| | | def can_buy(cls, code): |
| | | if constant.TEST: |
| | | return True, cls.BLOCK_TYPE_NONE |
| | | return ["测试"], True, cls.BLOCK_TYPE_NONE |
| | | # if True: |
| | | # # 测试 |
| | | # 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): |
| | | |
| | | blocks, block_msg = cls.get_can_buy_block(code, current_limit_up_datas, |
| | | limit_up_record_datas, yesterday_current_limit_up_codes, |
| | | before_blocks_dict) |
| | | if not blocks: |
| | | return False, block_msg |
| | | yesterday_current_limit_up_codes, before_blocks_dict, |
| | | current_limit_up_block_codes_dict): |
| | | blocks_compute_results = 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) |
| | | if not blocks_compute_results: |
| | | return False, True, "没有找到板块" |
| | | 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_manager.TRADE_STATE_BUY_SUCCESS})) |
| | | |
| | | codes = codes_delegate | codes_success |
| | | |
| | | # 统计成交代码的板块 |
| | | trade_codes_blocks_dict = {} |
| | | # 已经成交的板块 |
| | | trade_success_blocks_count = {} |
| | | trade_delegate_blocks_count = {} |
| | | for c in codes: |
| | | keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(c) |
| | | # 实时涨停原因 |
| | | trade_codes_blocks_dict[c] = k1_ | k4_ |
| | | # 实时涨停原因 + 推荐原因 |
| | | if not k1_: |
| | | trade_codes_blocks_dict[c] = k4_ |
| | | else: |
| | | trade_codes_blocks_dict[c] = k1_ |
| | | # 统计板块中的代码 |
| | | trade_block_codes_dict = {} |
| | | for c in trade_codes_blocks_dict: |
| | |
| | | if b not in trade_success_blocks_count: |
| | | trade_success_blocks_count[b] = set() |
| | | trade_success_blocks_count[b].add(c) |
| | | if c in codes_delegate: |
| | | if b not in trade_delegate_blocks_count: |
| | | trade_delegate_blocks_count[b] = set() |
| | | trade_delegate_blocks_count[b].add(c) |
| | | |
| | | if b not in trade_block_codes_dict: |
| | | trade_block_codes_dict[b] = set() |
| | | trade_block_codes_dict[b].add(c) |
| | | |
| | | # ---------------------------------加载已经下单/成交的代码信息------------end------------- |
| | | msg_list = [] |
| | | for key in blocks: |
| | | # 板块中已经有成交的就不下单了 |
| | | if key in trade_success_blocks_count: |
| | | success_codes_count = len(trade_success_blocks_count[key]) |
| | | if success_codes_count >= 1: |
| | | msg_list.append(f"【{key}】中已经有{success_codes_count}个成交代码") |
| | | log.logger_kpl_debug.debug(f"{code}:板块({key})已经有成交【{trade_success_blocks_count[key]}】") |
| | | continue |
| | | return True, block_msg |
| | | |
| | | return False, ",".join(msg_list) |
| | | # |
| | | can_buy_blocks = [] |
| | | unique_count = 0 |
| | | msg_list = [] |
| | | for r in blocks_compute_results: |
| | | # r的数据结构(板块,是否可以买,是否独苗,消息) |
| | | if r[2]: |
| | | unique_count += 1 |
| | | if r[1]: |
| | | if r[0] in trade_success_blocks_count and len(trade_success_blocks_count[r[0]]) > 0: |
| | | msg_list.append(f"【{r[0]}】有成交代码:{trade_success_blocks_count[r[0]]}") |
| | | continue |
| | | if r[0] in trade_delegate_blocks_count and len(trade_delegate_blocks_count[r[0]]) >= 2: |
| | | msg_list.append(f"【{r[0]}】已挂单:{trade_delegate_blocks_count[r[0]]}") |
| | | continue |
| | | can_buy_blocks.append(r[0]) |
| | | msg_list.append(r[3]) |
| | | # 所有板块都是独苗 |
| | | if unique_count == len(blocks_compute_results): |
| | | return can_buy_blocks, True, ",".join(msg_list) |
| | | return can_buy_blocks, False, ",".join(msg_list) |
| | | |
| | | # 更新代码板块判断是否可以买的结果 |
| | | @classmethod |
| | | def update_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas, |
| | | yesterday_current_limit_up_codes, |
| | | before_blocks_dict): |
| | | can_buy, msg = cls.__compute_can_buy_blocks(code, current_limit_up_datas, limit_up_record_datas, |
| | | yesterday_current_limit_up_codes, |
| | | before_blocks_dict) |
| | | before_blocks_dict, current_limit_up_block_codes_dict): |
| | | can_buy_blocks, unique, msg = 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) |
| | | # 保存板块计算结果 |
| | | cls.__can_buy_compute_result_dict[code] = (can_buy, msg) |
| | | |
| | | # 判断是否为真老大 |
| | | @classmethod |
| | | def __is_real_first_limit_up(cls, code, block, current_limit_up_datas, limit_up_record_datas, |
| | | yesterday_current_limit_up_codes, |
| | | before_blocks_dict): |
| | | # 加载涨停代码的目标板块 |
| | | def load_code_block(): |
| | | if limit_up_record_datas: |
| | | for d in limit_up_record_datas: |
| | | if d[2] in constant.KPL_INVALID_BLOCKS and d[3] in before_blocks_dict: |
| | | code_limit_up_reason_dict[d[3]] = list(before_blocks_dict.get(d[3]))[0] |
| | | else: |
| | | code_limit_up_reason_dict[d[3]] = d[2] |
| | | return code_limit_up_reason_dict |
| | | |
| | | if current_limit_up_datas is None: |
| | | current_limit_up_datas = [] |
| | | if limit_up_record_datas is None: |
| | | limit_up_record_datas = [] |
| | | code_limit_up_reason_dict = {} |
| | | load_code_block() |
| | | can_buy, msg = cls.__is_block_can_buy(code, block, current_limit_up_datas, code_limit_up_reason_dict, |
| | | yesterday_current_limit_up_codes, limit_up_record_datas) |
| | | return can_buy, msg |
| | | |
| | | @classmethod |
| | | def is_need_cancel(cls, code, limit_up_reason, current_limit_up_datas, limit_up_record_datas, |
| | | yesterday_current_limit_up_codes, |
| | | before_blocks_dict): |
| | | can_buy, msg = cls.__is_real_first_limit_up(code, limit_up_reason, current_limit_up_datas, |
| | | limit_up_record_datas, |
| | | yesterday_current_limit_up_codes, |
| | | before_blocks_dict) |
| | | if not can_buy: |
| | | logger_kpl_block_can_buy.warning(f"{code} 根据涨停原因({limit_up_reason})匹配不能买") |
| | | return not can_buy |
| | | cls.__can_buy_compute_result_dict[code] = (can_buy_blocks, unique, msg) |
| | | |
| | | |
| | | if __name__ == "__main__": |