| | |
| | | from settings.trade_setting import MarketSituationManager |
| | | from third_data.history_k_data_manager import HistoryKDataManager |
| | | from third_data.history_k_data_util import HistoryKDatasUtils |
| | | from third_data.kpl_data_constant import LimitUpDataConstant |
| | | 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 |
| | |
| | | cls.__can_buy_compute_result_dict[code] = ( |
| | | can_buy_blocks, unique, msg, can_buy_strong_blocks, keys, active_buy_blocks) |
| | | |
| | | |
| | | class RadicalBuyBlockManager: |
| | | """ |
| | | 激进买板块管理 |
| | | """ |
| | | __TargetCodePlateKeyManager = TargetCodePlateKeyManager() |
| | | |
| | | @classmethod |
| | | def compute_open_limit_up_code_dict_for_radical_buy(cls, current_limit_up_datas): |
| | | """ |
| | |
| | | @param current_limit_up_datas: |
| | | @return: |
| | | """ |
| | | timestamp_start = time.mktime( |
| | | time.strptime(datetime.datetime.now().strftime("%Y-%m-%d") + " 09:25:00", '%Y-%m-%d %H:%M:%S')) |
| | | timestamp_end = time.mktime(time.strptime(datetime.datetime.now().strftime("%Y-%m-%d") + " 09:30:00", '%Y-%m-%d %H:%M:%S')) |
| | | timestamp_start, timestamp_end = kpl_block_util.open_limit_up_time_range |
| | | temp_dict = {} |
| | | for d in current_limit_up_datas: |
| | | code = d[0] |
| | |
| | | # 计算是否开1 |
| | | if int(d[2]) >= timestamp_end or int(d[2]) < timestamp_start: |
| | | continue |
| | | # 剔除5板以上的 |
| | | if d[4].find("连板") > 0 and int(d[4].replace("连板", "")) >= 5: |
| | | continue |
| | | buy1_money = huaxin_l1_data_manager.get_buy1_money(code) |
| | | if not buy1_money or buy1_money < 1e8: |
| | | # 买1是否大于5000w |
| | | if not buy1_money or buy1_money < 5e7: |
| | | continue |
| | | if not tool.is_can_buy_code(code): |
| | | continue |
| | | # 买1是否大于1亿 |
| | | blocks = {d[5]} |
| | | if d[6]: |
| | | blocks |= set(d[6].split("、")) |
| | | blocks -= constant.KPL_INVALID_BLOCKS |
| | | temp_dict[code] = (kpl_util.get_high_level_count(d[4]), d[6]) |
| | | temp_dict[code] = (kpl_util.get_high_level_count(d[4]), blocks) |
| | | kpl_data_constant.open_limit_up_code_dict_for_radical_buy = temp_dict |
| | | |
| | | @classmethod |
| | | def is_radical_buy(cls, code): |
| | | def __get_current_index(cls, code, block, yesterday_limit_up_codes): |
| | | """ |
| | | 获取当前涨停身位 |
| | | @param code: |
| | | @param block: |
| | | @param yesterday_limit_up_codes: |
| | | @return: 索引,前排代码信息([(代码, 涨停时间)]) |
| | | """ |
| | | current_index = 0 |
| | | block_codes_infos = [] |
| | | timestamp_start, timestamp_end = kpl_block_util.open_limit_up_time_range |
| | | limit_up_time = time.time() |
| | | for k in LimitUpDataConstant.current_limit_up_datas: |
| | | _code = k[0] |
| | | blocks = LimitUpDataConstant.get_blocks_with_history(_code) |
| | | if not blocks: |
| | | blocks = set() |
| | | if _code == code: |
| | | # 获取当前代码涨停时间 |
| | | limit_up_time = int(k[2]) |
| | | continue |
| | | # 不是这个板块 |
| | | if block not in blocks: |
| | | continue |
| | | if not tool.is_can_buy_code(_code): |
| | | continue |
| | | # 剔除开1的数据 |
| | | if timestamp_start <= int(k[2]) < timestamp_end: |
| | | continue |
| | | # 剔除高位板 |
| | | if _code in yesterday_limit_up_codes: |
| | | continue |
| | | # 代码.涨停时间 |
| | | block_codes_infos.append((_code, int(k[2]))) |
| | | block_codes_infos.append((code, limit_up_time)) |
| | | block_codes_infos.sort(key=lambda x: x[1]) |
| | | before_codes_info = [] |
| | | for i in range(0, len(block_codes_infos)): |
| | | if block_codes_infos[i][0] == code: |
| | | current_index = i |
| | | break |
| | | else: |
| | | before_codes_info.append(block_codes_infos[i]) |
| | | |
| | | return current_index, before_codes_info |
| | | |
| | | @classmethod |
| | | def __get_history_index(cls, code, block, yesterday_limit_up_codes): |
| | | """ |
| | | 获取历史涨停身位 |
| | | @param code: |
| | | @param block: |
| | | @param current_limit_up_datas: |
| | | @return: |
| | | """ |
| | | history_index = 0 |
| | | block_codes_infos = [] |
| | | timestamp_start, timestamp_end = kpl_block_util.open_limit_up_time_range |
| | | limit_up_time = time.time() |
| | | for k in LimitUpDataConstant.history_limit_up_datas: |
| | | _code = k[3] |
| | | blocks = LimitUpDataConstant.get_blocks_with_history(_code) |
| | | if _code == code: |
| | | # 获取当前代码涨停时间 |
| | | limit_up_time = int(k[5]) |
| | | continue |
| | | # 不是这个板块 |
| | | if block not in blocks: |
| | | continue |
| | | if not tool.is_can_buy_code(_code): |
| | | continue |
| | | # 剔除开1的数据 |
| | | if timestamp_start <= int(k[5]) < timestamp_end: |
| | | continue |
| | | # 剔除高位板 |
| | | if _code in yesterday_limit_up_codes: |
| | | continue |
| | | # 代码.涨停时间 |
| | | block_codes_infos.append((_code, int(k[5]))) |
| | | block_codes_infos.append((code, limit_up_time)) |
| | | block_codes_infos.sort(key=lambda x: x[1]) |
| | | before_codes_info = [] |
| | | for i in range(0, len(block_codes_infos)): |
| | | if block_codes_infos[i][0] == code: |
| | | history_index = i |
| | | break |
| | | else: |
| | | before_codes_info.append(block_codes_infos[i]) |
| | | return history_index, before_codes_info |
| | | |
| | | @classmethod |
| | | def __is_radical_buy_with_open_limitup(cls, code, block, yesterday_limit_up_codes): |
| | | """ |
| | | 是否需要激进买(某个板块开1) |
| | | 1.有>=2个开1买老2 |
| | | 2.有1个开1的买老3 |
| | | @param code: |
| | | @param block: |
| | | @param yesterday_limit_up_codes 昨日涨停代码 |
| | | @return: |
| | | """ |
| | | # 10点之前才能买入 |
| | | if int(tool.get_now_time_str().replace(":", "")) > 100000: |
| | | return False, "超过生效时间" |
| | | |
| | | # 根据板块聚合数据 |
| | | open_limit_up_block_codes = {} |
| | | for c in kpl_data_constant.open_limit_up_code_dict_for_radical_buy: |
| | | blocks = kpl_data_constant.open_limit_up_code_dict_for_radical_buy[c][1] |
| | | for b in blocks: |
| | | if b not in open_limit_up_block_codes: |
| | | open_limit_up_block_codes[b] = set() |
| | | open_limit_up_block_codes[b].add(c) |
| | | if block not in open_limit_up_block_codes: |
| | | return False, "板块未开1" |
| | | |
| | | count = len(open_limit_up_block_codes.get(block)) |
| | | # ----获取历史身位---- |
| | | history_index, history_before_codes_info = cls.__get_history_index(code, block, yesterday_limit_up_codes) |
| | | # ----获取实时身位---- |
| | | current_index, current_before_codes_info = cls.__get_current_index(code, block, yesterday_limit_up_codes) |
| | | if count >= 2: |
| | | # 买老2 |
| | | if history_index == 0 and current_index == 0: |
| | | return True, f"开1数量:{count}" |
| | | else: |
| | | return False, f"开1数量:{count},身位不匹配:历史-{history_index} 实时-{current_index}" |
| | | else: |
| | | # 买老3 |
| | | if history_index == 1 and current_index == 1: |
| | | return True, f"开1数量:{count}" |
| | | else: |
| | | return False, f"开1数量:{count},身位不匹配:历史-{history_index} 实时-{current_index}" |
| | | |
| | | @classmethod |
| | | def __is_radical_buy_with_block_up(cls, code, block, yesterday_limit_up_codes): |
| | | """ |
| | | 是否激进买(板块突然涨起来) |
| | | 老大和老二的涨停时间相差5分钟内 |
| | | 老三的涨停时间距离老大涨停在10分钟内就买 |
| | | @param code: |
| | | @param block: |
| | | @param yesterday_limit_up_codes: |
| | | @return: |
| | | """ |
| | | # 获取当前的板块 |
| | | current_index, current_before_codes_info = cls.__get_current_index(code, block, set()) |
| | | if current_index != 2: |
| | | return False, f"只能买老3,当前身位-{current_index + 1}" |
| | | history_index, history_before_codes_info = cls.__get_history_index(code, block, set()) |
| | | if history_index != current_index or len(current_before_codes_info) != len(history_before_codes_info): |
| | | return False, f"前排代码有炸板" |
| | | if len(current_before_codes_info) < 2: |
| | | return False, f"前排代码小于2个" |
| | | # 老大,老二必须相隔5分钟内 |
| | | if current_before_codes_info[0][1] >= kpl_block_util.open_limit_up_time_range[1]: |
| | | return False, f"有开1" |
| | | |
| | | if current_before_codes_info[1][1] - current_before_codes_info[0][1] < 5 * 60: |
| | | # 获取当前代码的涨停时间 |
| | | limit_up_timestamp = LimitUpDataConstant.get_first_limit_up_time(code) |
| | | if not limit_up_timestamp: |
| | | limit_up_timestamp = time.time() |
| | | if limit_up_timestamp - current_before_codes_info[0][1] < 10 * 60: |
| | | return True, f"前排涨停:{current_before_codes_info}" |
| | | return False, "前排涨停时间间隔不满足条件" |
| | | |
| | | @classmethod |
| | | def is_radical_buy(cls, code, yesterday_limit_up_codes): |
| | | """ |
| | | 是否是激进买 |
| | | @param code: |
| | | @return: |
| | | @return: {激进买的板块}, 原因 |
| | | """ |
| | | # 获取今日开一的代码,剔除5板以上的 |
| | | current_limit_up_datas = kpl_data_constant.current_limit_up_datas |
| | | # 计算 |
| | | |
| | | # 获取开1的板块 |
| | | open_limit_up_code_dict = kpl_data_constant.open_limit_up_code_dict_for_radical_buy |
| | | if not open_limit_up_code_dict: |
| | | return False, "没有首板开1的数据" |
| | | open_limit_up_blocks = set() |
| | | for c in open_limit_up_code_dict: |
| | | open_limit_up_blocks |= open_limit_up_code_dict[c][1] |
| | | # 获取代码的板块 |
| | | keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(code, contains_today=False) |
| | | # 获取 |
| | | match_blocks = open_limit_up_blocks & keys_ |
| | | if not match_blocks: |
| | | return False, "没匹配到开1的板块" |
| | | can_buy_blocks = set() |
| | | msges = [] |
| | | for b in match_blocks: |
| | | # 判断板块是否该激进买 |
| | | result = cls.__is_radical_buy_with_open_limitup(code, b, yesterday_limit_up_codes) |
| | | if result[0]: |
| | | can_buy_blocks.add(b) |
| | | msges.append(f"【{b}】:{result}") |
| | | if can_buy_blocks: |
| | | return can_buy_blocks, "开1满足条件:" + ",".join(msges) |
| | | if not can_buy_blocks: |
| | | msges.clear() |
| | | for b in match_blocks: |
| | | # 板块快速启动 |
| | | result = cls.__is_radical_buy_with_block_up(code, b, yesterday_limit_up_codes) |
| | | if result[0]: |
| | | can_buy_blocks.add(b) |
| | | msges.append(f"【{b}】:{result}") |
| | | return can_buy_blocks, "开1不满足条件,板块快速启动:" + ",".join(msges) |
| | | |
| | | |
| | | if __name__ == "__main__": |