From 65afea1ba534b51f947cbe7989d7f4d650bbc9e6 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期一, 04 十一月 2024 18:29:00 +0800 Subject: [PATCH] 代码异常保护 --- third_data/code_plate_key_manager.py | 596 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 452 insertions(+), 144 deletions(-) diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py index e4f33a0..09a40b6 100644 --- a/third_data/code_plate_key_manager.py +++ b/third_data/code_plate_key_manager.py @@ -12,14 +12,16 @@ from db.redis_manager_delegate import RedisUtils from third_data import kpl_block_util, kpl_api, kpl_util, kpl_data_constant, huaxin_l1_data_manager 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 third_data.third_blocks_manager import BlockMapManager, CodeThirdBlocksManager +from trade.buy_money_count_setting import RadicalBuyBlockCodeCountManager +from trade.order_statistic import DealAndDelegateWithBuyModeDataManager +from trade.radical_buy_data_manager import RedicalBuyDataManager +from utils import global_util, tool, buy_condition_util from log_module import log, async_log_util from db import redis_manager_delegate as redis_manager -from log_module.log import logger_kpl_block_can_buy +from log_module.log import logger_kpl_block_can_buy, logger_debug, logger_kpl_jx_out from third_data.kpl_util import KPLPlatManager from trade import trade_manager, l2_trade_util, trade_constant @@ -33,16 +35,41 @@ __code_blocks = {} # 澶囩敤 __code_by_blocks = {} + # 婵�杩涗拱鐨勪唬鐮佹澘鍧� + __code_blocks_for_radical_buy = {} __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = super(KPLCodeJXBlockManager, cls).__new__(cls, *args, **kwargs) + cls.__load_data() return cls.__instance - def __get_redis(self): - return self.__redisManager.getRedis() + @classmethod + def __load_data(cls): + keys = RedisUtils.keys(cls.__get_redis(), "kpl_jx_blocks_by-*") + if keys: + for k in keys: + val = RedisUtils.get(cls.__get_redis(), k) + val = json.loads(val) + cls.__code_by_blocks[k.split("-")[1]] = (val, time.time()) + keys = RedisUtils.keys(cls.__get_redis(), "kpl_jx_blocks-*") + if keys: + for k in keys: + val = RedisUtils.get(cls.__get_redis(), k) + val = json.loads(val) + cls.__code_blocks[k.split("-")[1]] = (val, time.time()) + keys = RedisUtils.keys(cls.__get_redis(), "kpl_jx_blocks_radical-*") + if keys: + for k in keys: + val = RedisUtils.get(cls.__get_redis(), k) + val = json.loads(val) + cls.__code_blocks_for_radical_buy[k.split("-")[1]] = (val, time.time()) + + @classmethod + def __get_redis(cls): + return cls.__redisManager.getRedis() def save_jx_blocks(self, code, blocks: list, current_limit_up_blocks: set, by=False): if not blocks: @@ -63,28 +90,18 @@ RedisUtils.setex_async(self.__db, f"kpl_jx_blocks-{code}", tool.get_expire(), json.dumps(final_blocks)) self.__code_blocks[code] = (final_blocks, time.time()) - # 鑾峰彇绮鹃�夋澘鍧� - def get_jx_blocks(self, code, by=False): - if by: - if code in self.__code_by_blocks: - return self.__code_by_blocks[code] - val = RedisUtils.get(self.__get_redis(), f"kpl_jx_blocks_by-{code}") - if val is None: - return None - else: - val = json.loads(val) - self.__code_by_blocks[code] = val - return self.__code_by_blocks[code] - else: - if code in self.__code_blocks: - return self.__code_blocks[code] - val = RedisUtils.get(self.__get_redis(), f"kpl_jx_blocks-{code}") - if val is None: - return None - else: - val = json.loads(val) - self.__code_blocks[code] = val - return self.__code_blocks[code] + def save_jx_blocks_for_radical_buy(self, code, blocks: list): + if not blocks: + return + RedisUtils.setex_async(self.__db, f"kpl_jx_blocks_radical-{code}", tool.get_expire(), json.dumps(blocks)) + self.__code_blocks_for_radical_buy[code] = (blocks, time.time()) + + # 鑾峰彇绮鹃�夋澘鍧楋紙婵�杩涗拱锛� + def get_jx_blocks_radical(self, code): + blocks_info = self.__code_blocks_for_radical_buy.get(code) + if blocks_info: + return set(blocks_info[0]) + return None def get_jx_blocks_cache(self, code, by=False): if by: @@ -109,6 +126,8 @@ async_log_util.info(logger_kpl_block_can_buy, f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�-{blocks} 鑰楁椂:{int(time.time() - start_time)}s") self.save_jx_blocks(code, blocks, current_limit_up_blocks) + # 璺熼殢绮鹃�夋澘鍧椾竴璧锋洿鏂� + self.load_jx_blocks_radical(code) else: # 杩樻病娑ㄥ仠鐨勯渶瑕佹洿鏂扮簿閫夋澘鍧� 鏇存柊绮鹃�夋澘鍧� if abs(float(buy_1_price) - float(limit_up_price)) >= 0.001: @@ -129,8 +148,8 @@ async_log_util.info(logger_kpl_block_can_buy, f"{code}:鑾峰彇鍒扮簿閫夋澘鍧楋紙鏇存柊锛�-{blocks} 鑰楁椂:{int(time.time() - start_time)}s") self.save_jx_blocks(code, blocks, current_limit_up_blocks) - - + # 璺熼殢绮鹃�夋澘鍧椾竴璧锋洿鏂� + self.load_jx_blocks_radical(code) elif price_rate > 0.03: # 娣诲姞澶囩敤鏉垮潡 if not self.get_jx_blocks_cache(code, by=True): @@ -139,9 +158,24 @@ 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") + # 璺熼殢绮鹃�夋澘鍧椾竴璧锋洿鏂� + self.load_jx_blocks_radical(code) + + if price_rate > 0.03: + if not self.__code_blocks_for_radical_buy.get(code): + self.load_jx_blocks_radical(code) except Exception as e: logger_kpl_block_can_buy.error(f"{code} 鑾峰彇鏉垮潡鍑洪敊") logger_kpl_block_can_buy.exception(e) + + def load_jx_blocks_radical(self, code): + start_time = time.time() + blocks = kpl_api.getCodeJingXuanBlocks(code, jx=False) + blocks = set([b[1] for b in blocks]) + # fblocks = BlockMapManager().filter_blocks(blocks) + async_log_util.info(logger_kpl_block_can_buy, + f"{code}:鑾峰彇鍒版澘鍧�(婵�杩涗拱) 杩囨护鍓�-{blocks} 鑰楁椂:{int(time.time() - start_time)}s") + self.save_jx_blocks_for_radical_buy(code, list(blocks)) # 绂佹涓嬪崟鐨勬澘鍧� @@ -259,12 +293,6 @@ 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): - RedisUtils.sadd(self.__get_redis(), f"kpl_limit_up_reason_his-{code}", from_reason) - RedisUtils.expire(self.__get_redis(), f"kpl_limit_up_reason_his-{code}", tool.get_expire()) - self.__set_total_keys(code) - # 璁剧疆浠g爜鐨勪粖鏃ユ定鍋滃師鍥� def __set_total_keys(self, code): keys = set() @@ -303,6 +331,8 @@ return fresult + + # 瀹炴椂寮�鐩樺暒甯傚満鏁版嵁 class RealTimeKplMarketData: # 绮鹃�夊墠5 @@ -316,31 +346,54 @@ __KPLPlateForbiddenManager = KPLPlateForbiddenManager() __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager() __KPLPlatManager = KPLPlatManager() + # 绮鹃�夋祦鍏ュ墠鍑� + __top_jx_blocks = set() + # 绮鹃�夋祦鍑哄墠鍑� + __top_jx_out_blocks = set() @classmethod - def set_top_5_reasons(cls, datas): - temp_list = [] - for d in datas: - cls.total_reason_dict[d[1]] = d - # 鎺掑簭 - for i in range(0, len(datas)): - if datas[i][1] not in constant.KPL_INVALID_BLOCKS: - # 锛堝悕绉�,鍑�娴佸叆閲戦,鎺掑悕锛� - temp_list.append((datas[i][1], datas[i][3], len(temp_list))) - # 鍙幏鍙栧墠10涓� - if len(temp_list) > 10: - break - if datas[i][3] < 3 * 10000 * 10000: - break + def set_market_jingxuan_blocks(cls, datas): + """ + 璁剧疆绮鹃�夋祦鍏ユ暟鎹� + @param datas: + @return: + """ + blocks = set() + for data in datas: + if data[3] <= 0: + break + blocks.add(data[1]) + cls.__top_jx_blocks = blocks - for temp in temp_list: - names = cls.__KPLPlatManager.get_same_plat_names_by_id(temp[0]) - for name in names: - if name == temp[1]: - continue - temp_list.append((name, temp[1], temp[2])) - cls.top_5_reason_list = temp_list - cls.__reset_top_5_dict() + @classmethod + def set_market_jingxuan_out_blocks(cls, datas): + """ + 璁剧疆绮鹃�夋祦鍑烘暟鎹� + @param datas: + @return: + """ + blocks = set() + for i in range(0, len(datas)): + if i >= 10 and int(tool.get_now_time_str().replace(":", "")) < int("100000"): + # 10鐐瑰墠鐪嬪墠10锛屽崄鐐瑰悗涓嶇湅鍓�10 + break + data = datas[i] + if data[3] > 0 - 5e7: + # 杩囨护5鍗冧竾浠ヤ笂鐨� + break + blocks.add(kpl_util.filter_block(data[1])) + + # 璁板綍绮鹃�夋祦鍑烘棩蹇� + async_log_util.info(logger_kpl_jx_out, f"鍘熸暟鎹細{datas[:10]} 鏉垮潡锛歿blocks}") + cls.__top_jx_out_blocks = blocks + + @classmethod + def get_top_market_jingxuan_blocks(cls): + return cls.__top_jx_blocks + + @classmethod + def get_top_market_jingxuan_out_blocks(cls): + return cls.__top_jx_out_blocks @classmethod def set_top_5_industry(cls, datas): @@ -373,35 +426,6 @@ temp_set = cls.top_5_key_dict.keys() return temp_set - # 閫氳繃鍏抽敭瀛楀垽鏂兘涔扮殑浠g爜鏁伴噺 - @classmethod - def get_can_buy_codes_count(cls, code, key): - # 鍒ゆ柇琛屼笟娑ㄥ仠绁ㄦ暟閲忥紝闄ゅ紑鑷繁蹇呴』澶т簬1涓� - temp_codes = LimitUpCodesPlateKeyManager.total_key_codes_dict.get(key) - if temp_codes is None: - temp_codes = set() - else: - temp_codes = set(temp_codes) - temp_codes.discard(code) - if len(temp_codes) < 1: - # 鍚庢帓鎵嶈兘鎸傚崟 - return 0, "韬綅涓嶄负鍚庢帓" - - forbidden_plates = cls.__KPLPlateForbiddenManager.list_all_cache() - if key in forbidden_plates: - return 0, "涓嶄拱璇ユ澘鍧�" - - # 10:30浠ュ墠鍙互鎸�2涓崟 - if int(tool.get_now_time_str().replace(':', '')) < int("100000"): - return 2, "10:00浠ュ墠鍙互鎸�2涓崟" - # 10:30浠ュ悗 - if key not in cls.top_5_key_dict: - return 0, "鍑�娴佸叆娌″湪鍓�5" - if cls.top_5_key_dict[key][1] > 3 * 10000 * 10000: - return 2, "鍑�娴佸叆鍦ㄥ墠5涓斿ぇ浜�3浜�" - else: - return 1, "鍑�娴佸叆鍦ㄥ墠5" - @classmethod def is_in_top(cls, keys): reasons = cls.get_can_buy_key_set() @@ -421,6 +445,14 @@ __history_limit_up_reason_dict = {} # 鏉垮潡 __blocks_dict = {} + + __instance = None + + def __new__(cls, *args, **kwargs): + if not cls.__instance: + cls.__instance = super(CodesHisReasonAndBlocksManager, cls).__new__(cls, *args, **kwargs) + + return cls.__instance def __get_redis(self): return self.__redisManager.getRedis() @@ -490,6 +522,9 @@ return self.__history_blocks_dict_cache.get(code) try: kpl_results = KPLLimitUpDataUtil.get_latest_block_infos(code=code) + # 鍙栨渶杩�2鏉℃暟鎹� + if kpl_results and len(kpl_results) > 2: + kpl_results = kpl_results[-2:] keys = set() if kpl_results: keys |= set([x[2] for x in kpl_results]) @@ -501,6 +536,14 @@ except: pass return set() + + def get_history_blocks_cache(self, code): + """ + 鑾峰彇180澶╃殑鍘嗗彶娑ㄥ仠鍘熷洜缂撳瓨 + @param code: + @return: + """ + return self.__history_blocks_dict_cache.get(code) # 鐩爣浠g爜鏉垮潡鍏抽敭璇嶇鐞� @@ -555,6 +598,13 @@ keys = keys - set(constant.KPL_INVALID_BLOCKS) return keys, k1, k11, k2, k3, k4 + def get_plate_keys_for_radical_buy(self, code): + """ + 婵�杩涗拱鍏ョ殑鏉垮潡 + @param code: + @return: + """ + class CodePlateKeyBuyManager: # 鏃犳澘鍧� @@ -585,12 +635,12 @@ 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_rank, open_limit_up_count): """ - 鏉垮潡鏄惁闇�瑕佹縺杩涗拱鍏� - 瑙勫垯锛氭牴鎹韩浣嶅垽鏂槸鍚﹂渶瑕佹縺杩涗拱锛屾牴鎹椂闂村垝鍒� + 鏉垮潡鏄惁闇�瑕佺Н鏋佷拱鍏� + 瑙勫垯锛氭牴鎹韩浣嶅垽鏂槸鍚﹂渶瑕佺Н鏋佷拱锛屾牴鎹椂闂村垝鍒� @param code: 浠g爜 @param block: 鏉垮潡鍚嶇О @param current_rank: 鐩墠鍦ㄦ澘鍧椾腑鐨勮韩浣嶏紝浠�0寮�濮� @@ -612,7 +662,7 @@ break 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, @@ -695,7 +745,7 @@ current_open_limit_up_codes), shsz=True, limit_up_time=first_limit_up_time) - # 璁$畻鏄惁闇�瑕佹縺杩涗拱鍏� + # 璁$畻鏄惁闇�瑕佺Н鏋佷拱鍏� is_active_buy = cls.__is_need_active_buy(code, block, current_shsz_rank, len(current_open_limit_up_codes)) # record_shsz_rank, record_shsz_rank_codes = kpl_block_util.get_code_record_rank(code, block, @@ -828,7 +878,7 @@ shsz=True, limit_up_time=first_limit_up_time) - # 璁$畻鏄惁闇�瑕佹縺杩涗拱鍏� + # 璁$畻鏄惁闇�瑕佺Н鏋佷拱鍏� is_active_buy = cls.__is_need_active_buy(code, block, current_shsz_rank, len(current_open_limit_up_codes)) if current_shsz_rank < len(current_open_limit_up_codes) + max_rank: @@ -873,7 +923,8 @@ current_limit_up_datas = [] # 鑾峰彇鐩爣浠g爜鏉垮潡 - keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code) + # keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code) + keys, k1 = RadicalBuyBlockManager.get_code_blocks(code) # log.logger_kpl_debug.info("{}鏈�缁堝叧閿瘝锛歿}", code, keys) @@ -882,6 +933,7 @@ fresults = [] if not keys: return fresults, set() + code_limit_up_reasons_dict = {} load_code_block() for block in keys: @@ -900,7 +952,7 @@ # 鏄惁鍙互涓嬪崟 # 杩斿洖锛氬彲浠ヤ拱鐨勬澘鍧�,鏄惁鐙嫍,娑堟伅 - # 鍙拱鐨勬澘鍧�, 鏄惁鐙嫍, 娑堟伅, 鍙拱鐨勫己鍔挎澘鍧�, 鍏抽敭璇�, 婵�杩涗拱鐨勬澘鍧� + # 鍙拱鐨勬澘鍧�, 鏄惁鐙嫍, 娑堟伅, 鍙拱鐨勫己鍔挎澘鍧�, 鍏抽敭璇�, 绉瀬涔扮殑鏉垮潡 @classmethod def can_buy(cls, code): if constant.TEST: @@ -910,7 +962,7 @@ # 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, @@ -973,7 +1025,7 @@ msg_list = [] active_buy_blocks = [] for r in blocks_compute_results: - # r鐨勬暟鎹粨鏋�(鏉垮潡,鏄惁鍙互涔�,鏄惁鐙嫍,娑堟伅,鏄惁鏄己鍔挎澘鍧�, 婵�杩涗拱鍏ヤ俊鎭�) + # r鐨勬暟鎹粨鏋�(鏉垮潡,鏄惁鍙互涔�,鏄惁鐙嫍,娑堟伅,鏄惁鏄己鍔挎澘鍧�, 绉瀬涔板叆淇℃伅) if r[2]: # 鐙嫍 unique_count += 1 @@ -998,7 +1050,7 @@ msg_list.append(r[3]) if r[5]: active_buy_blocks.append(r[0]) - msg_list.append(f"銆恵r[0]}銆戞縺杩涗拱鍏�({r[5]})") + msg_list.append(f"銆恵r[0]}銆戠Н鏋佷拱鍏�({r[5]})") else: if r[3]: msg_list.append(r[3]) @@ -1054,9 +1106,49 @@ class RadicalBuyBlockManager: """ - 婵�杩涗拱鏉垮潡绠$悊 + 鎵叆涔版澘鍧楃鐞� """ __TargetCodePlateKeyManager = TargetCodePlateKeyManager() + # 涓婃鐨勬定鍋滀唬鐮� + __last_limit_up_codes = set() + # 璁板綍浠g爜鐨勬定鍋滄椂闂� + __limit_up_time_dict = {} + # 鐐告澘浠g爜鐨勬�绘定鍋滄椂闂� + __total_limit_up_space_dict = {} + + # 褰撳墠娑ㄥ仠鐨勪唬鐮� + __current_limit_up_codes = set() + + @classmethod + def set_current_limit_up_datas(cls, current_limit_up_datas): + # 鏌ヨ褰撳墠鐨勬定鍋滀唬鐮侀泦鍚� + codes = set([d[0] for d in current_limit_up_datas]) + cls.__current_limit_up_codes = codes + try: + # 鐐告澘浠g爜 + break_limit_up_codes = cls.__last_limit_up_codes - codes + # 鏂版定鍋滅殑浠g爜 + new_limit_up_codes = codes - cls.__last_limit_up_codes + if new_limit_up_codes: + for code in new_limit_up_codes: + if code not in cls.__limit_up_time_dict: + cls.__limit_up_time_dict[code] = time.time() + if break_limit_up_codes: + # 璁板綍鎬绘定鍋滄椂闂� + for bc in break_limit_up_codes: + if bc in cls.__limit_up_time_dict: + space = tool.trade_time_sub(tool.get_now_time_str(), + tool.to_time_str(cls.__limit_up_time_dict[bc])) + if bc not in cls.__total_limit_up_space_dict: + cls.__total_limit_up_space_dict[bc] = 0 + cls.__total_limit_up_space_dict[bc] = cls.__total_limit_up_space_dict[bc] + space + logger_debug.info(f"鐐告澘浠g爜娑ㄥ仠鏃堕棿锛歿bc}-{cls.__total_limit_up_space_dict[bc]}") + cls.__limit_up_time_dict.pop(bc) + except Exception as e: + logger_debug.exception(e) + finally: + cls.__last_limit_up_codes = codes + cls.compute_open_limit_up_code_dict_for_radical_buy(current_limit_up_datas) @classmethod def compute_open_limit_up_code_dict_for_radical_buy(cls, current_limit_up_datas): @@ -1084,11 +1176,13 @@ if d[6]: blocks |= set(d[6].split("銆�")) blocks -= constant.KPL_INVALID_BLOCKS + # 杩囨护 + blocks = BlockMapManager().filter_blocks(blocks) 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 __get_current_index(cls, code, block, yesterday_limit_up_codes): + def __get_current_index(cls, code, block, yesterday_limit_up_codes, exclude_codes=None, limit_up_time=None): """ 鑾峰彇褰撳墠娑ㄥ仠韬綅 @param code: @@ -1096,15 +1190,25 @@ @param yesterday_limit_up_codes: @return: 绱㈠紩,鍓嶆帓浠g爜淇℃伅锛圼(浠g爜, 娑ㄥ仠鏃堕棿)]锛� """ + if exclude_codes is None: + exclude_codes = set() current_index = 0 block_codes_infos = [] timestamp_start, timestamp_end = kpl_block_util.open_limit_up_time_range - limit_up_time = time.time() + if limit_up_time is None: + limit_up_time = time.time() for k in LimitUpDataConstant.current_limit_up_datas: _code = k[0] + # 鍓旈櫎4鏉夸互涓婄殑鏉� + if kpl_util.get_high_level_count(k[4]) >= 4: + continue + + if _code in exclude_codes: + continue blocks = LimitUpDataConstant.get_blocks_with_history(_code) if not blocks: blocks = set() + blocks = BlockMapManager().filter_blocks(blocks) if _code == code: # 鑾峰彇褰撳墠浠g爜娑ㄥ仠鏃堕棿 limit_up_time = int(k[2]) @@ -1135,21 +1239,34 @@ return current_index, before_codes_info @classmethod - def __get_history_index(cls, code, block, yesterday_limit_up_codes): + def __get_history_index(cls, code, block, yesterday_limit_up_codes, exclude_codes=None): """ 鑾峰彇鍘嗗彶娑ㄥ仠韬綅 @param code: @param block: - @param current_limit_up_datas: + @param current_limit_up_datas: 鏄ㄦ棩娑ㄥ仠浠g爜 + @param current_limit_up_codes: 鐩墠鐨勬定鍋滀唬鐮� @return: """ + if exclude_codes is None: + exclude_codes = set() history_index = 0 block_codes_infos = [] + # 寮�1鏃堕棿鑼冨洿 timestamp_start, timestamp_end = kpl_block_util.open_limit_up_time_range limit_up_time = time.time() + limit_up_space_ge_60s_codes = set() for k in LimitUpDataConstant.history_limit_up_datas: _code = k[3] + + # 鍓旈櫎4鏉夸互涓婄殑鏉� + if kpl_util.get_high_level_count(k[12]) >= 4: + continue + + if _code in exclude_codes: + continue blocks = LimitUpDataConstant.get_blocks_with_history(_code) + blocks = BlockMapManager().filter_blocks(blocks) if _code == code: # 鑾峰彇褰撳墠浠g爜娑ㄥ仠鏃堕棿 limit_up_time = int(k[5]) @@ -1165,7 +1282,13 @@ # 鍓旈櫎楂樹綅鏉� if _code in yesterday_limit_up_codes: continue - # 浠g爜.娑ㄥ仠鏃堕棿 + # 鍓旈櫎鐐告澘浠g爜鎸佺画娑ㄥ仠鏃堕棿灏忎簬1鍒嗛挓鐨勪唬鐮� 涓� 鍙兘鐢ㄤ簬涓嶆帓闄ゅ墠2鏉℃暟鎹� + if _code not in cls.__current_limit_up_codes and _code in cls.__total_limit_up_space_dict and \ + cls.__total_limit_up_space_dict[_code] < 60 and not exclude_codes and len( + limit_up_space_ge_60s_codes) < 3: + limit_up_space_ge_60s_codes.add(_code) + continue + # 浠g爜,娑ㄥ仠鏃堕棿 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]) @@ -1189,11 +1312,11 @@ @param yesterday_limit_up_codes 鏄ㄦ棩娑ㄥ仠浠g爜 @return: """ - # 10鐐逛箣鍓嶆墠鑳戒拱鍏� - if not constant.TEST: - if int(tool.get_now_time_str().replace(":", "")) > 100000: - return False, "瓒呰繃鐢熸晥鏃堕棿" - + # 9:45鐐逛箣鍓嶆定鍋滅殑鎵嶈兘涔板叆 + # 鑾峰彇褰撳墠浠g爜鐨勬定鍋滄椂闂� + limit_up_timestamp = cls.__get_limit_up_timestamp(code) + if int(tool.timestamp_format(limit_up_timestamp, "%H%M%S")) > 94500: + return False, "瓒呰繃鐢熸晥鏃堕棿" # 鏍规嵁鏉垮潡鑱氬悎鏁版嵁 open_limit_up_block_codes_dict = {} for c in kpl_data_constant.open_limit_up_code_dict_for_radical_buy: @@ -1209,54 +1332,226 @@ # ----鑾峰彇鍘嗗彶韬綅---- 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) + current_index, current_before_codes_info = cls.__get_current_index(code, block, yesterday_limit_up_codes, + limit_up_time=limit_up_timestamp) + exclude_codes = set() if count >= 2 or ( count == 1 and kpl_data_constant.open_limit_up_code_dict_for_radical_buy[open_limit_up_block_codes[0]][ 0] == 2): # 寮�濮嬫暟閲忓ぇ浜�2涓垨鑰呭彧鏈変竴涓�2鏉垮紑1 - if history_index == 0 and current_index == 0: - return True, f"寮�1鏁伴噺锛歿count}" - else: - return False, f"寮�1鏁伴噺锛歿count}锛岄潪寮�1棣栨澘韬綅涓嶅尮閰嶏細鍘嗗彶-{history_index+1} 瀹炴椂-{current_index+1}" + exclude_codes.clear() else: - # 涔拌��3 - if history_index == 1 and current_index == 1: - return True, f"寮�1鏁伴噺锛歿count}" + # 鑾峰彇鍖呭惈楂樹綅鏉跨殑韬綅 + # ----鑾峰彇鍘嗗彶韬綅---- + history_index, history_before_codes_info = cls.__get_history_index(code, block, set()) + # ----鑾峰彇瀹炴椂韬綅---- + current_index, current_before_codes_info = cls.__get_current_index(code, block, set(), + limit_up_time=limit_up_timestamp) + if history_before_codes_info and current_before_codes_info and history_before_codes_info[0][0] == \ + current_before_codes_info[0][0]: + # 鍓嶆帓绗竴涓厓绱犳棤鐐告澘 + exclude_codes = {history_before_codes_info[0][0]} else: - return False, f"寮�1鏁伴噺锛歿count}锛岄潪寮�1棣栨澘韬綅涓嶅尮閰嶏細鍘嗗彶-{history_index+1} 瀹炴椂-{current_index+1}" + return False, f"寮�1鏁伴噺锛歿count}锛屽巻鍙�-{history_index + 1} 瀹炴椂-{current_index + 1}" + + # 鑾峰彇涓绘澘鐨勮韩浣� + history_index, history_before_codes_info = cls.__get_history_index(code, block, + yesterday_limit_up_codes, + exclude_codes=exclude_codes) + # 涔伴鏉胯�佸ぇ/鑰佷簩 + # 棣栨澘鑰佸ぇ涓嶈兘涔版椂鍙拱鑰佷簩 + if history_index > 1: + return False, f"寮�1鏁伴噺锛歿count}锛岄潪寮�1棣栨澘韬綅涓嶅尮閰嶏細鍘嗗彶-{history_index + 1} 瀹炴椂-{current_index + 1}" + if history_index == 1: + # 褰撳墠浠g爜涓鸿��2锛岃鍒ゆ柇鑰佸ぇ鏄惁鍙拱 + if RedicalBuyDataManager.can_buy(history_before_codes_info[0][0], + DealAndDelegateWithBuyModeDataManager().get_deal_codes())[0]: + return False, f"寮�1鏁伴噺锛歿count}锛屽墠鎺掍唬鐮佸彲涔帮細{history_before_codes_info[0]}" + return True, f"寮�1鏁伴噺锛歿count}锛屽墠鎺掍唬鐮佷笉鍙拱锛歿history_before_codes_info[0]}锛屽巻鍙插墠鎺�-{history_before_codes_info}锛屽紑1浠g爜-{open_limit_up_block_codes}" + return True, f"寮�1鏁伴噺锛歿count}锛屽巻鍙�-{history_index + 1} 瀹炴椂-{current_index + 1}锛� 鍓嶆帓浠g爜-{current_before_codes_info}, 寮�1浠g爜-{open_limit_up_block_codes}" @classmethod def __is_radical_buy_with_block_up(cls, code, block, yesterday_limit_up_codes): """ 鏄惁婵�杩涗拱锛堟澘鍧楃獊鐒舵定璧锋潵锛� - 鑰佸ぇ鍜岃�佷簩鐨勬定鍋滄椂闂寸浉宸�5鍒嗛挓鍐� - 鑰佷笁鐨勬定鍋滄椂闂磋窛绂昏�佸ぇ娑ㄥ仠鍦�10鍒嗛挓鍐呭氨涔� + 1.鑰佷簩鍜岃�佷笁鐨勬定鍋滄椂闂寸浉宸�5鍒嗛挓鍐� + 2.鑰佷笁涓嶈兘涔伴『浣嶅埌鑰佸洓锛堣�佸洓涓庤�佷笁鐩稿樊10鍒嗛挓鍐咃級 + 3.鍓�2涓エ涓嶈兘鐐告澘锛堝巻鍙茶韩浣嶄笌鐜板湪韬綅涓�鑷达級 + 4.闄ゅ紑鍓嶄袱涓唬鐮佸彲涔拌��1涓庤��2 + 5.涔拌��2鐨勬儏鍐碉細鑰�1涓嶆弧瓒充拱鍏ユ潯浠� + @param code: @param block: @param yesterday_limit_up_codes: @return: """ - # 鑾峰彇褰撳墠鐨勬澘鍧� - current_index, current_before_codes_info = cls.__get_current_index(code, block, set()) - if len(current_before_codes_info) < 2: - return False, f"鍓嶆帓浠g爜灏忎簬2涓�" - if current_before_codes_info[0][1] >= kpl_block_util.open_limit_up_time_range[1]: - return False, f"鏈夊紑1" - 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"鍓嶆帓浠g爜鏈夌偢鏉�" - # 鑰佸ぇ锛岃�佷簩蹇呴』鐩搁殧5鍒嗛挓鍐� - if current_before_codes_info[1][1] - current_before_codes_info[0][1] < 5 * 60: - # 鑾峰彇褰撳墠浠g爜鐨勬定鍋滄椂闂� - 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, "鍓嶆帓娑ㄥ仠鏃堕棿闂撮殧涓嶆弧瓒虫潯浠�" + # 鑾峰彇褰撳墠浠g爜鐨勬定鍋滄椂闂� + limit_up_timestamp = cls.__get_limit_up_timestamp(code) + + # 鑾峰彇褰撳墠鐨勬澘鍧� + current_index, current_before_codes_info = cls.__get_current_index(code, block, set(), + limit_up_time=limit_up_timestamp) + current_before_codes = [x[0] for x in current_before_codes_info] + + if len(current_before_codes_info) < 2: + return False, f"鍓嶆帓浠g爜灏忎簬2涓細{current_before_codes_info}" + + # 褰撳墠浠g爜寮�1涓嶈兘涔� + if limit_up_timestamp < kpl_block_util.open_limit_up_time_range[1]: + return False, f"褰撳墠浠g爜寮�1" + + if tool.trade_time_sub(tool.timestamp_format(limit_up_timestamp, '%H:%M:%S'), + tool.timestamp_format(current_before_codes_info[-1][1], '%H:%M:%S')) >= 10 * 60: + return False, f"璺濈涓婁釜浠g爜娑ㄥ仠宸茶繃鍘�10鍒嗛挓锛坽current_before_codes_info[0]}锛�" + + history_index, history_before_codes_info = cls.__get_history_index(code, block, set()) + history_before_codes = [x[0] for x in history_before_codes_info] + # 鍓嶄袱涓唬鐮佹槸鍚︽湁鐐告澘 + dif_codes = set(history_before_codes[:2]) - set(current_before_codes[:2]) + if dif_codes: + return False, f"鍓�2浠g爜鏈夌偢鏉匡細{dif_codes}" + # 涓嶈绠楀墠2鐨勪唬鐮� + + exclude_codes = set() + for x in current_before_codes_info: + if x[1] < kpl_block_util.open_limit_up_time_range[1]: + exclude_codes.add(x[0]) + # 闄ゅ幓鍓嶄簩浠g爜涓庡紑1浠g爜涔嬪悗鏄惁涓洪鏉胯�佸ぇ锛氭墍鏈夊紑1鐨勮涓�1涓� + open_count = len(exclude_codes) + if open_count > 0 and open_count + 1 <= len(current_before_codes): + # 鍓嶆帓鏈夊紑1 + exclude_codes |= set(current_before_codes[open_count:open_count + 1]) + else: + exclude_codes |= set(current_before_codes[:2]) + + open_limit_up_code_dict = kpl_data_constant.open_limit_up_code_dict_for_radical_buy + if open_limit_up_code_dict: + exclude_codes |= set(open_limit_up_code_dict.keys()) + history_index, history_before_codes_info = cls.__get_history_index(code, block, yesterday_limit_up_codes, + exclude_codes) + # 鑾峰彇鏈澘鍧椾拱鍏ヤ唬鐮佺殑鏈�澶ф暟閲� + max_count = RadicalBuyBlockCodeCountManager().get_block_code_count(block) + if history_index > max_count: + return False, f"鎺掗櫎鍓�2锛岀洰鏍囦唬鐮佷綅浜庡巻鍙茶韩浣�-{history_index + 1}锛屽墠鎺掍唬鐮侊細{history_before_codes_info}, 鏉垮潡鏈�澶氬彲涔皗max_count}" + + if max_count == 1: + if history_index == 1: + # 棣栨澘鑰�2锛屽垽鏂墠闈㈢殑鑰佸ぇ鏄惁鏄睘浜庝笉鑳戒拱鐨勮寖鐣� + pre_code = history_before_codes_info[0][0] + # pre_code涓嶈兘涔帮紝鎵嶈兘涔� + if RedicalBuyDataManager.can_buy(pre_code, DealAndDelegateWithBuyModeDataManager().get_deal_codes())[0]: + return False, f"鍓嶆帓浠g爜鍙拱锛歿pre_code}" + # 璺濈鍓嶄竴涓槸鍚﹀湪10鍒嗛挓鍐� + if tool.trade_time_sub(tool.timestamp_format(limit_up_timestamp, '%H:%M:%S'), + tool.timestamp_format(history_before_codes_info[-1][1], '%H:%M:%S')) >= 10 * 60: + return False, f"璺濈涓婁釜涓嶈兘涔扮殑浠g爜娑ㄥ仠宸茶繃鍘�10鍒嗛挓锛坽history_before_codes_info[0]}锛�" + else: + # 璺濈涓婁釜浠g爜娑ㄥ仠5鍒嗛挓浠ュ唴 + if tool.trade_time_sub(tool.timestamp_format(limit_up_timestamp, '%H:%M:%S'), + tool.timestamp_format(current_before_codes_info[-1][1], '%H:%M:%S')) >= 10 * 60: + return False, f"璺濈涓婁釜浠g爜娑ㄥ仠宸茶繃鍘�10鍒嗛挓锛坽current_before_codes_info[-1]}锛�" + else: + if tool.trade_time_sub(tool.timestamp_format(limit_up_timestamp, '%H:%M:%S'), + tool.timestamp_format(current_before_codes_info[-1][1], '%H:%M:%S')) >= 10 * 60: + return False, f"璺濈涓婁釜浠g爜娑ㄥ仠宸茶繃鍘�10鍒嗛挓锛坽current_before_codes_info[-1]}锛�" + + return True, f"婊¤冻涔板叆闇�姹�: 鍓嶆帓浠g爜-{current_before_codes_info}" + + @classmethod + def __is_re_limit_up(cls, code, block): + """ + 鏄惁鏄偢鏉垮洖灏佸彲涔� + @param code: + @param block: + @return: + """ + # 鑾峰彇韬綅 + current_index, current_before_codes_info = cls.__get_current_index(code, block, set(), + limit_up_time=cls.__get_limit_up_timestamp( + code)) + history_index, history_before_codes_info = cls.__get_history_index(code, block, set()) + if current_index != history_index: + return False, f"鏈夊叾浠栫偢鏉�" + if current_index > 1: + return False, f"涓嶆槸鍓�2鐨勬澘鍧�" + history_codes = set() + # 鑾峰彇鏉垮潡鐐告澘鎯呭喌 + for k in LimitUpDataConstant.history_limit_up_datas: + _code = k[3] + blocks = LimitUpDataConstant.get_blocks_with_history(_code) + blocks = BlockMapManager().filter_blocks(blocks) + # 涓嶆槸杩欎釜鏉垮潡 + if block in blocks: + history_codes.add(_code) + if len(history_codes) <= 4: + return False, f"鏉垮潡鍘嗗彶娑ㄥ仠灏忎簬4涓細{history_codes}" + # 鑾峰彇褰撳墠娑ㄥ仠鏁伴噺 + current_codes = set() + for k in LimitUpDataConstant.current_limit_up_datas: + _code = k[0] + blocks = LimitUpDataConstant.get_blocks_with_history(_code) + if not blocks: + blocks = set() + blocks = BlockMapManager().filter_blocks(blocks) + # 涓嶆槸杩欎釜鏉垮潡 + if block in blocks: + current_codes.add(_code) + current_codes.add(code) + diff = history_codes - current_codes + if diff: + return False, f"鏉垮潡鐐告澘涓嶆褰撳墠绁細{diff}" + return True, "" + + @classmethod + def __get_limit_up_timestamp(cls, code): + """ + 鑾峰彇浠g爜鐨勬定鍋滄椂闂达紝榛樿褰撳墠鏃堕棿 + @param code: + @return: + """ + limit_up_timestamp = LimitUpDataConstant.get_first_limit_up_time(code) + if not limit_up_timestamp: + limit_up_timestamp = time.time() + return limit_up_timestamp + + @classmethod + def get_code_kpl_blocks(cls, code): + blocks = KPLCodeJXBlockManager().get_jx_blocks_radical(code) + if blocks is None: + blocks = set() + # 灏嗚幏鍙栨定鍋滃師鍥犱笌娑ㄥ仠鎺ㄨ崘 + keys = TargetCodePlateKeyManager().get_plate_keys(code, contains_today=True) + if keys and keys[0]: + blocks |= set(keys[0]) + return blocks + + @classmethod + def get_code_blocks(cls, code): + """ + 鑾峰彇鐩爣浠g爜鐨勬澘鍧� + @param code: + @return: 杩囨护鍚庣殑鏉垮潡,杩囨护鍓嶇殑鏉垮潡 + """ + blocks = cls.get_code_kpl_blocks(code) + match_blocks, info = CodeThirdBlocksManager().get_intersection_blocks_info(code, blocks) + match_blocks -= constant.KPL_INVALID_BLOCKS + fblocks = match_blocks & RealTimeKplMarketData.get_top_market_jingxuan_blocks() + if not fblocks: + fblocks = set() + match_blocks_3, info = CodeThirdBlocksManager().get_intersection_blocks_info(code, blocks, same_count=3) + if match_blocks_3: + match_blocks_3 -= constant.KPL_INVALID_BLOCKS + fblocks |= match_blocks_3 + # 鑾峰彇寮�鐩樺暒鍘嗗彶娑ㄥ仠鍘熷洜 + kpl_history_blocks = CodesHisReasonAndBlocksManager().get_history_blocks_cache(code) + if kpl_history_blocks: + fblocks |= BlockMapManager().filter_blocks(kpl_history_blocks) + jx_out_blocks = RealTimeKplMarketData.get_top_market_jingxuan_out_blocks() + if jx_out_blocks: + fblocks -= jx_out_blocks + + return fblocks, match_blocks @classmethod def is_radical_buy(cls, code, yesterday_limit_up_codes): @@ -1273,7 +1568,10 @@ for c in open_limit_up_code_dict: open_limit_up_blocks |= open_limit_up_code_dict[c][1] # 鑾峰彇浠g爜鐨勬澘鍧� - keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(code, contains_today=False) + keys_, info = cls.get_code_blocks(code) + if not keys_: + return set(), "娌¤幏鍙栧埌鏉垮潡浜ら泦" + match_blocks = open_limit_up_blocks & keys_ can_buy_blocks = set() fmsges = [] @@ -1283,17 +1581,27 @@ 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}") + msges.append(f"銆恵b}銆�:{result[1]}") fmsges.append("寮�1鍒ゆ柇##" + ",".join(msges)) if not can_buy_blocks: msges.clear() - for b in match_blocks: + for b in keys_: # 鏉垮潡蹇�熷惎鍔� 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}") + + msges.append(f"銆恵b}銆�:{result[1]}") fmsges.append("鏉垮潡蹇�熷惎鍔ㄥ垽鏂�##" + ",".join(msges)) + + if not can_buy_blocks: + msges.clear() + for b in keys_: + result = cls.__is_re_limit_up(code, b) + if result[0]: + can_buy_blocks.add(b) + msges.append(f"銆恵b}銆�:{result[1]}") + fmsges.append("鏉垮潡鍥炲皝鍒ゆ柇##" + ",".join(msges)) return can_buy_blocks, " **** ".join(fmsges) -- Gitblit v1.8.0