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 | 1660 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 1,341 insertions(+), 319 deletions(-) diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py index 3f09e63..09a40b6 100644 --- a/third_data/code_plate_key_manager.py +++ b/third_data/code_plate_key_manager.py @@ -3,38 +3,264 @@ """ # 娑ㄥ仠浠g爜鍏抽敭璇嶆澘鍧楃鐞� +import copy +import datetime import json +import time import constant -from third_data import kpl_block_util -from utils import global_util, tool -from log_module import log -from db import redis_manager +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.kpl_data_constant import LimitUpDataConstant +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_limit_up, logger_kpl_block_can_buy, logger_kpl_debug +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 +from trade import trade_manager, l2_trade_util, trade_constant + +# 浠g爜绮鹃�夋澘鍧楃鐞� +from utils.kpl_data_db_util import KPLLimitUpDataUtil + + +class KPLCodeJXBlockManager: + __db = 3 + __redisManager = redis_manager.RedisManager(3) + __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 + + @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: + return + final_blocks = copy.deepcopy(blocks) + if len(blocks) > 2: + final_blocks.clear() + for b in blocks: + if b not in constant.KPL_INVALID_BLOCKS: + final_blocks.append(b) + if len(final_blocks) < 2: + final_blocks = blocks + # 淇濆瓨鍓�2鏉℃暟鎹� + if by: + RedisUtils.setex_async(self.__db, f"kpl_jx_blocks_by-{code}", tool.get_expire(), json.dumps(final_blocks)) + self.__code_by_blocks[code] = (final_blocks, time.time()) + else: + 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 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: + 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, current_limit_up_blocks): + try: + # logger_kpl_block_can_buy.info(f"鍑嗗鏇存柊绮鹃�夋澘鍧楋細{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) / tool.get_limit_up_rate(code), 2) + # 濡傛灉娑ㄥ箙澶т簬7%灏辫鍙栨澘鍧� + price_rate = (buy_1_price - pre_close_price) / pre_close_price + if price_rate > 0.07: + jx_blocks_info = self.get_jx_blocks_cache(code) + if not jx_blocks_info: + start_time = time.time() + blocks = kpl_api.getCodeBlocks(code) + 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: + # 闈炴定鍋滅姸鎬� + UPDATE_TIME_SPACE = 5 * 60 + time_diff = tool.trade_time_sub(tool.get_now_time_str(), "09:30:00") + if time_diff < 0: + UPDATE_TIME_SPACE = 60 * 60 + else: + UPDATE_TIME_SPACE = int(time_diff / 30) + 60 + if UPDATE_TIME_SPACE > 5 * 60: + UPDATE_TIME_SPACE = 5 * 60 + + if time.time() - jx_blocks_info[1] > UPDATE_TIME_SPACE: + start_time = time.time() + # 璺濈涓婃鏇存柊鏃堕棿杩囧幓浜�5鍒嗛挓 + blocks = kpl_api.getCodeBlocks(code) + 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): + 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") + # 璺熼殢绮鹃�夋澘鍧椾竴璧锋洿鏂� + 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)) + + +# 绂佹涓嬪崟鐨勬澘鍧� +class ForbiddenBlockManager: + __db = 3 + __redisManager = redis_manager.RedisManager(3) + __instance = None + __forbidden_blocks = set() + + def __new__(cls, *args, **kwargs): + if not cls.__instance: + cls.__instance = super(ForbiddenBlockManager, cls).__new__(cls, *args, **kwargs) + cls.__load_data() + return cls.__instance + + @classmethod + def __get_redis(cls): + return cls.__redisManager.getRedis() + + # 鍔犺浇鏁版嵁 + @classmethod + def __load_data(cls): + blocks = cls.__get_redis().smembers("forbidden_blocks") + if blocks: + for b in blocks: + cls.__forbidden_blocks.add(b) + + def add(self, block): + self.__forbidden_blocks.add(block) + RedisUtils.sadd_async(self.__db, "forbidden_blocks", block) + RedisUtils.expire_async(self.__db, "forbidden_blocks", tool.get_expire()) + + def remove(self, block): + if block in self.__forbidden_blocks: + self.__forbidden_blocks.remove(block) + RedisUtils.srem_async(self.__db, "forbidden_blocks", block) + + def get_blocks(self): + return copy.deepcopy(self.__forbidden_blocks) + + def is_in(self, block): + return block in self.__forbidden_blocks # 寮�鐩樺暒绂佹浜ゆ槗鏉垮潡绠$悊 class KPLPlateForbiddenManager: __redisManager = redis_manager.RedisManager(3) + __kpl_forbidden_plates_cache = set() - def __get_redis(self): - return self.__redisManager.getRedis() + __instance = None + + def __new__(cls, *args, **kwargs): + if not cls.__instance: + cls.__instance = super(KPLPlateForbiddenManager, cls).__new__(cls, *args, **kwargs) + cls.__load_datas() + return cls.__instance + + @classmethod + def __load_datas(cls): + __redis = cls.__get_redis() + try: + __kpl_forbidden_plates_cache = RedisUtils.smembers(__redis, "kpl_forbidden_plates") + finally: + RedisUtils.realse(__redis) + + @classmethod + def __get_redis(cls): + return cls.__redisManager.getRedis() def save_plate(self, plate): - self.__get_redis().sadd("kpl_forbidden_plates", plate) - self.__get_redis().expire("kpl_forbidden_plates", tool.get_expire()) + self.__kpl_forbidden_plates_cache.add(plate) + RedisUtils.sadd(self.__get_redis(), "kpl_forbidden_plates", plate) + RedisUtils.expire(self.__get_redis(), "kpl_forbidden_plates", tool.get_expire()) def list_all(self): - return self.__get_redis().smembers("kpl_forbidden_plates") + return RedisUtils.smembers(self.__get_redis(), "kpl_forbidden_plates") + + def list_all_cache(self): + return self.__kpl_forbidden_plates_cache 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) @@ -42,7 +268,7 @@ def __get_redis(self): return self.__redisManager.getRedis() - # 鑾峰彇浠婃棩娑ㄥ仠鏁版嵁锛屾牸寮忥細[(浠g爜,娑ㄥ仠鍘熷洜)] + # 鑾峰彇浠婃棩娑ㄥ仠鏁版嵁锛屾牸寮忥細[(浠g爜,娑ㄥ仠鍘熷洜,绮鹃�夋澘鍧楀垪琛�)] def set_today_limit_up(self, datas): temp_dict = {} if datas: @@ -55,30 +281,35 @@ self.set_today_total_limit_up(datas) # 璁剧疆浠婃棩鍘嗗彶娑ㄥ仠鏁版嵁 - def set_today_total_limit_up(self, datas): + # 鏍煎紡锛�(浠g爜,娑ㄥ仠鍘熷洜,绮鹃�夋澘鍧楀垪琛�) + @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] + # 璁剧疆娑ㄥ仠浠g爜鐨勬澘鍧楀強鍘熷洜 + cls.__today_total_limit_up_reason_dict[code] = (item[1], item[2]) - # 浠婃棩娑ㄥ仠鍘熷洜鍙樺寲 - def set_today_limit_up_reason_change(self, code, from_reason, to_reason): - self.__get_redis().sadd(f"kpl_limit_up_reason_his-{code}", from_reason) - self.__get_redis().expire(f"kpl_limit_up_reason_his-{code}", tool.get_expire()) - self.__set_total_keys(code) + @classmethod + def get_today_limit_up_reason(cls, code): + return cls.__today_total_limit_up_reason_dict.get(code) + # 璁剧疆浠g爜鐨勪粖鏃ユ定鍋滃師鍥� def __set_total_keys(self, code): keys = set() - keys_his = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}") - keys |= keys_his + # keys_his = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}") + # keys |= keys_his if code in self.today_limit_up_reason_dict: - keys.add(self.today_limit_up_reason_dict.get(code)) + if self.today_limit_up_reason_dict.get(code) not in constant.KPL_INVALID_BLOCKS: + keys.add(self.today_limit_up_reason_dict.get(code)) self.total_code_keys_dict[code] = keys for k in keys: if k not in self.total_key_codes_dict: self.total_key_codes_dict[k] = set() self.total_key_codes_dict[k].add(code) - logger_kpl_limit_up.info("{}鏉垮潡鍏抽敭璇�:{}", code, keys) + # logger_kpl_limit_up.info("{}鏉垮潡鍏抽敭璇�:{}", code, keys) + + # 鏍规嵁浼犲叆鐨勫叧閿瘝涓庢定鍋滀唬鐮佷俊鎭尮閰嶈韩浣� def get_codes_by_key_without_mine(self, key, code): # 鍙瘮杈冧粖鏃ユ定鍋滃師鍥� @@ -100,6 +331,8 @@ return fresult + + # 瀹炴椂寮�鐩樺暒甯傚満鏁版嵁 class RealTimeKplMarketData: # 绮鹃�夊墠5 @@ -113,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): @@ -170,43 +426,12 @@ 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() - 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() - log.logger_kpl_debug.debug("甯傚満娴佸叆鍓�5:{}", reasons) - forbidden_plates = cls.__KPLPlateForbiddenManager.list_all() + 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: @@ -221,20 +446,28 @@ # 鏉垮潡 __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() def set_history_limit_up_reason(self, code, reasons): self.__history_limit_up_reason_dict[code] = set(reasons) - self.__get_redis().setex(f"kpl_his_limit_up_reason-{code}", tool.get_expire(), json.dumps(list(reasons))) - logger_kpl_debug.debug(f"璁剧疆鍘嗗彶娑ㄥ仠鍘熷洜锛歿code}-{reasons}") + RedisUtils.setex(self.__get_redis(), f"kpl_his_limit_up_reason-{code}", tool.get_expire(), + json.dumps(list(reasons))) # 濡傛灉杩斿洖鍊间笉涓篘one琛ㄧず宸茬粡鍔犺浇杩囧巻鍙插師鍥犱簡 def get_history_limit_up_reason(self, code): reasons = self.__history_limit_up_reason_dict.get(code) if reasons is None: # 浠庡唴瀛樹腑鍔犺浇 - val = self.__get_redis().get(f"kpl_his_limit_up_reason-{code}") + val = RedisUtils.get(self.__get_redis(), f"kpl_his_limit_up_reason-{code}") if val is not None: val = set(json.loads(val)) self.__history_limit_up_reason_dict[code] = val @@ -245,15 +478,19 @@ else: return reasons + def get_history_limit_up_reason_cache(self, code): + reasons = self.__history_limit_up_reason_dict.get(code) + return reasons + def set_blocks(self, code, blocks): self.__blocks_dict[code] = set(blocks) - self.__get_redis().setex(f"kpl_blocks-{code}", tool.get_expire(), json.dumps(list(blocks))) + RedisUtils.setex(self.__get_redis(), f"kpl_blocks-{code}", tool.get_expire(), json.dumps(list(blocks))) def get_blocks(self, code): reasons = self.__blocks_dict.get(code) if reasons is None: # 浠庡唴瀛樹腑鍔犺浇 - val = self.__get_redis().get(f"kpl_blocks-{code}") + val = RedisUtils.get(self.__get_redis(), f"kpl_blocks-{code}") if val is not None: val = set(json.loads(val)) self.__blocks_dict[code] = val @@ -273,41 +510,100 @@ blocks = set() return reasons | blocks + __history_blocks_dict_cache = {} + + def get_history_blocks(self, code): + """ + 鑾峰彇180澶╃殑鍘嗗彶娑ㄥ仠鍘熷洜 + @param code: + @return: + """ + if code in self.__history_blocks_dict_cache: + 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]) + for r in kpl_results: + if r[3]: + keys |= set(r[3].split("銆�")) + self.__history_blocks_dict_cache[code] = keys + return keys + except: + pass + return set() + + def get_history_blocks_cache(self, code): + """ + 鑾峰彇180澶╃殑鍘嗗彶娑ㄥ仠鍘熷洜缂撳瓨 + @param code: + @return: + """ + return self.__history_blocks_dict_cache.get(code) + # 鐩爣浠g爜鏉垮潡鍏抽敭璇嶇鐞� class TargetCodePlateKeyManager: __redisManager = redis_manager.RedisManager(1) __CodesPlateKeysManager = CodesHisReasonAndBlocksManager() + __KPLCodeJXBlockManager = KPLCodeJXBlockManager() def __get_redis(self): return self.__redisManager.getRedis() - # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,鏉垮潡 - def get_plate_keys(self, code): + # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,绮鹃�夋澘鍧� + def get_plate_keys(self, code, contains_today=True): + """ + 鑾峰彇浠g爜鐨勬澘鍧�: 锛�180澶╃殑娑ㄥ仠鍘熷洜+鎺ㄨ崘鍘熷洜锛�+浠婃棩娑ㄥ仠鍘熷洜+浠婃棩娑ㄥ仠鎺ㄨ崘鍘熷洜+浠婃棩鎺ㄨ崘鍘熷洜 + @param code: + @return: 锛堟澘鍧楀叧閿瘝闆嗗悎,浠婃棩娑ㄥ仠鍘熷洜+娑ㄥ仠鎺ㄨ崘鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,绮鹃�夋澘鍧楋級 + """ keys = set() k1 = set() - if code in LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict: - k1 = {LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict[code]} - # 鍔犺浇浠婃棩鍘嗗彶鍘熷洜 - k11 = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}") - k2 = self.__CodesPlateKeysManager.get_history_limit_up_reason(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 k2 is None: k2 = set() - k3 = set() - industry = global_util.code_industry_map.get(code) - if industry: - k3 = {industry} - k4 = self.__CodesPlateKeysManager.get_blocks(code) - if k4 is None: - k4 = set() - for k in [k1, k11, k2, k3, k4]: - keys |= k + k3 = self.__CodesPlateKeysManager.get_history_blocks(code) + if k3: + keys |= k3 + # industry = global_util.code_industry_map.get(code) + # if industry: + # k3 = {industry} - # 鎺掗櫎鏃犳晥鐨勬定鍋滃師鍥� + k4 = set() + jingxuan_block_info = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code) + if not jingxuan_block_info: + jingxuan_block_info = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code, by=True) + if jingxuan_block_info: + jingxuan_blocks = jingxuan_block_info[0] + k4 |= set(jingxuan_blocks) # set([x[1] for x in jingxuan_blocks]) + if k1 and contains_today: + # 娑ㄥ仠杩� + keys |= k1 + + # 鑾峰彇涓嶅埌娑ㄥ仠鍘熷洜 + if contains_today: + keys |= k4 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: @@ -325,221 +621,384 @@ __TargetCodePlateKeyManager = TargetCodePlateKeyManager() __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager() __CodesHisReasonAndBlocksManager = CodesHisReasonAndBlocksManager() + __CodesTradeStateManager = trade_manager.CodesTradeStateManager() + __can_buy_compute_result_dict = {} + + @classmethod + def __remove_from_l2(cls, code, msg): + # 鏍规嵁韬綅绉婚櫎浠g爜 + # return + # 涓嬭繃鍗曠殑浠g爜涓嶇Щ闄� + if trade_manager.CodesTradeStateManager().get_trade_state_cache(code) != trade_constant.TRADE_STATE_NOT_TRADE: + # 鍙涓嬭繃鍗曠殑灏变笉绉婚櫎 + return + 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寮�濮� + @param open_limit_up_count: 寮�1鐨勬暟閲� + @return: + """ + + real_current_rank = max(current_rank - open_limit_up_count, 0) + + TIME_STR_RANGES = ["10:00:00", "10:30:00", "11:00:00", "13:00:00", "13:30:00", "14:00:00", "14:30:00", + "15:00:00"] + TIME_INT_RANGES = [int(x.replace(':', '')) for x in TIME_STR_RANGES] + MAX_RANKS = [3, 3, 2, 2, 1, 0, 0, 0] + now_time_str = tool.get_now_time_str().replace(':', '') + for i in range(len(TIME_INT_RANGES)): + if int(now_time_str) <= TIME_INT_RANGES[i]: + if MAX_RANKS[i] > real_current_rank: + return True + 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, + high_level_code_blocks=None, high_level_block_codes=None): + # 鐙嫍鍒ゆ柇 + if high_level_code_blocks is None: + high_level_code_blocks = {} + if high_level_block_codes is None: + high_level_block_codes = {} + block_codes = current_limit_up_block_codes_dict.get(block) + if block_codes is None: + block_codes = set() + + if not block_codes: + # 楂樹綅鏉挎硾鍖栨澘鍧椾腑鏃犳澘鍧� + if not high_level_block_codes.get(block): + return False, True, f"銆恵block}銆�:鏉垮潡鏃犳定鍋�", False, False + elif len(block_codes) == 1 and code in block_codes: + if not high_level_block_codes.get(block): + return False, True, f"{block}:鏉垮潡鍙湁褰撳墠浠g爜娑ㄥ仠", False, False + # 鍙互涔扮殑鏈�澶ф帓鍚� + # 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) + + # ---------------------------鍒ゆ柇寮哄娍涓荤嚎------------------------- + is_strong_block = False + for d in current_limit_up_datas: + bs = kpl_util.get_current_limit_up_reasons(d) + if block not in bs: + general_blocks = high_level_code_blocks.get(d[0]) + if not general_blocks or block not in general_blocks: + # 娌″湪娉涘寲鏉垮潡涓� + continue + count = kpl_util.get_high_level_count(d[4]) + if count >= 3: + if d[4].find("杩炴澘") > 0: + is_strong_block = True + break + elif d[0] in yesterday_current_limit_up_codes and len(block_codes) >= 2: + # 鍑犲ぉ鍑犳澘锛屼笖鏈�杩�2杩炴澘 + # 鐪嬫槸鍚︽湁棣栨澘鍚庢帓 + is_strong_block = True + break + + if not is_strong_block: + temp_block_codes = set(copy.deepcopy(block_codes)) + temp_block_codes.discard(code) + if len(temp_block_codes) >= 3: + is_strong_block = True + max_rank = 2 + # 寮哄娍鏉垮潡涔拌�佸洓 + if is_strong_block: + max_rank = 3 + + # 闇�瑕佹帓闄ょ殑鑰佸ぇ鐨勪唬鐮� + exclude_first_codes = set() # HighIncreaseCodeManager().list_all() + + # 鑾峰彇涓绘澘寮�1鐨勪唬鐮� + + # 鍓旈櫎楂樹綅鏉� + if current_open_limit_up_codes and yesterday_current_limit_up_codes: + current_open_limit_up_codes -= yesterday_current_limit_up_codes + + # 鑾峰彇浠g爜鐨勫垵娆℃定鍋滄椂闂� + first_limit_up_time = time.time() + # if limit_up_record_datas: + for r in limit_up_record_datas: + if r[3] == code: + first_limit_up_time = int(r[5]) + + # 鑾峰彇涓绘澘瀹炴椂韬綅,鍓旈櫎楂樹綅鏉� + current_shsz_rank, front_current_shsz_rank_codes = kpl_block_util.get_code_current_rank(code, block, + current_limit_up_datas, + code_limit_up_reasons_dict, + yesterday_current_limit_up_codes, + exclude_first_codes, + len( + 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, + # limit_up_record_datas, + # code_limit_up_reason_dict, + # yesterday_current_limit_up_codes, + # 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, is_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, is_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 + 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, is_active_buy + return False, False, f"銆恵block}銆戝墠鎺掍唬鐮侊細{front_current_shsz_rank_codes} 瓒呰繃{len(current_open_limit_up_codes) + max_rank}涓�", is_strong_block, is_active_buy + + # 杩囨椂鐨勪唬鐮� + # 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}锛歵op10娑ㄥ仠鏉垮潡锛屼富鏉垮紑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}锛歵op4娑ㄥ仠鏉垮潡锛岄潪涓绘澘寮�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}锛�" + + @classmethod + def __is_block_can_buy_new(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, + high_level_code_blocks=None, high_level_block_codes=None): + """ + 璇ョエ鐨勬澘鍧楁槸鍚﹀彲浠ヤ拱 + @param code: + @param block: + @param current_limit_up_datas: + @param code_limit_up_reasons_dict: + @param yesterday_current_limit_up_codes: + @param limit_up_record_datas: + @param current_limit_up_block_codes_dict: + @param high_level_code_blocks: + @param high_level_block_codes: + @return: + """ + # 鐙嫍鍒ゆ柇 + if high_level_code_blocks is None: + high_level_code_blocks = {} + if high_level_block_codes is None: + high_level_block_codes = {} + block_codes = current_limit_up_block_codes_dict.get(block) + if block_codes is None: + block_codes = set() + # 鍘嗗彶娑ㄥ仠浠g爜 + block_codes_records = set() + if limit_up_record_datas: + for k in limit_up_record_datas: + if block in code_limit_up_reasons_dict.get(k[3]): + block_codes_records.add(k[3]) + + if not block_codes: + # 楂樹綅鏉挎硾鍖栨澘鍧椾腑鏃犳澘鍧� + if not high_level_block_codes.get(block): + return False, True, f"銆恵block}銆�:鏉垮潡鏃犳定鍋�", False, False, 0, 0, 0 + elif len(block_codes) == 1 and code in block_codes: + if not high_level_block_codes.get(block): + return False, True, f"{block}:鏉垮潡鍙湁褰撳墠浠g爜娑ㄥ仠", False, False, 0, 0, 0 + # 鍙互涔扮殑鏈�澶ф帓鍚� + # 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) + + is_strong_block = False + + # 鏈�澶氫拱鑰佸嚑 + RANKS = [6, 5, 4, 4, 3, 3, 2] + RANK_TIMES = ["10:00:00", "10:30:00", "11:00:00", "11:30:00", "13:30:00", "14:00:00", "15:00:00"] + now_time_str = tool.get_now_time_str() + max_rank = 2 + for i in range(len(RANK_TIMES)): + if tool.trade_time_sub(now_time_str, RANK_TIMES[i]) <= 0: + max_rank = RANKS[i] + break + + # 闇�瑕佹帓闄ょ殑鑰佸ぇ鐨勪唬鐮� + exclude_first_codes = set() + + # 鑾峰彇涓绘澘寮�1鐨勪唬鐮� + + # 鍓旈櫎楂樹綅鏉� + if current_open_limit_up_codes and yesterday_current_limit_up_codes: + current_open_limit_up_codes -= yesterday_current_limit_up_codes + + # 鑾峰彇浠g爜鐨勫垵娆℃定鍋滄椂闂� + first_limit_up_time = time.time() + # if limit_up_record_datas: + for r in limit_up_record_datas: + if r[3] == code: + first_limit_up_time = int(r[5]) + + # 鑾峰彇涓绘澘瀹炴椂韬綅,鍓旈櫎楂樹綅鏉� + current_shsz_rank, front_current_shsz_rank_codes = kpl_block_util.get_code_current_rank(code, block, + current_limit_up_datas, + code_limit_up_reasons_dict, + yesterday_current_limit_up_codes, + exclude_first_codes, + len( + 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)) + + if current_shsz_rank < len(current_open_limit_up_codes) + max_rank: + return True, len(block_codes | { + code}) <= 1, f"銆恵block}銆戝墠鎺掍唬鐮侊細{current_shsz_rank}", is_strong_block, is_active_buy, current_shsz_rank, len( + block_codes), len(block_codes_records) + else: + return False, len(block_codes | { + code}) <= 1, f"銆恵block}銆戝墠鎺掍唬鐮侊細{front_current_shsz_rank_codes} 瓒呰繃{len(current_open_limit_up_codes) + max_rank}涓�", is_strong_block, is_active_buy, current_shsz_rank, len( + block_codes), len(block_codes_records) # 鑾峰彇鍙互涔扮殑鏉垮潡 # current_limit_up_datas: 浠婃棩瀹炴椂娑ㄥ仠 # latest_2_day_limit_up_datas锛氭渶杩�2澶╃殑瀹炴椂娑ㄥ仠锛堜笉鍚粖鏃ワ級 # limit_up_record_datas锛氫粖鏃ュ巻鍙叉定鍋� + # yesterday_current_limit_up_codes 锛� 鏄ㄦ棩娑ㄥ仠浠g爜 + # before_blocks_dict锛氬巻鍙叉定鍋滃師鍥� + # 杩斿洖鏉垮潡鐨勮绠楃粨鏋淸(鏉垮潡鍚嶇О,鏄惁鍙拱,鏄惁鏄嫭鑻�,淇℃伅)] + @classmethod - def get_can_buy_block(cls, code, current_limit_up_datas, latest_2_day_limit_up_datas, limit_up_record_datas): - now_time = int(tool.get_now_time_str().replace(":", "")) - times = [100000, 103000, 110000, 133000, 150000] - time_index = 0 - for i in range(len(times)): - if now_time < times[i]: - time_index = i - break - # 鑾峰彇鏉垮潡 - keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code) - log.logger_kpl_debug.info("{}鍏抽敭璇嶏細浠婃棩-{},浠婃棩鍘嗗彶-{},鍘嗗彶-{},浜岀骇琛屼笟-{},浠g爜鏉垮潡-{}", code, k1, k11, k2, k3, k4) - keys = set() - if k3: - # 鍖归厤浜岀骇琛屼笟 - keys |= k3 - if k1: - # 鏈変粖鏃ユ定鍋滃師鍥� - keys |= k1 - pass - elif k2: - # 浠婃棩鏃犳定鍋滀絾鏈夊巻鍙叉定鍋� - keys |= k2 - else: - if k4: - keys |= k4 - log.logger_kpl_debug.info("{}鏈�缁堝叧閿瘝锛歿}", code, keys) + def get_can_buy_block(cls, 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, + high_level_general_block_codes): + # 鍔犺浇娑ㄥ仠浠g爜鐨勭洰鏍囨澘鍧� + def load_code_block(): + if limit_up_record_datas: + # 鑾峰彇浠婃棩9:30浠ュ墠鐨勬椂闂� + time_str = datetime.datetime.now().strftime("%Y-%m-%d") + " 09:30:00" + timestamp = time.mktime(time.strptime(time_str, '%Y-%m-%d %H:%M:%S')) + + 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_reasons_dict[d[3]] = {list(before_blocks_dict.get(d[3]))[0]} + else: + code_limit_up_reasons_dict[d[3]] = {d[2]} + # 寮�1鎵嶈兘鍖呭惈鎺ㄨ崘鍘熷洜 + if d[6] and int(d[5]) < timestamp: + code_limit_up_reasons_dict[d[3]] |= set(d[6].split("銆�")) + return code_limit_up_reasons_dict + + if current_limit_up_datas is None: + current_limit_up_datas = [] + + # 鑾峰彇鐩爣浠g爜鏉垮潡 + # 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) # 娑ㄥ仠鍒楄〃涓尮閰嶅叧閿瘝锛岃繑鍥烇紙鏉垮潡:浠g爜闆嗗悎锛夛紝浠g爜闆嗗悎涓凡缁忔帓闄よ嚜韬� - match_limit_up_result = cls.__LimitUpCodesPlateKeyManager.match_limit_up_reason_keys(code, keys) - log.logger_kpl_debug.info("{}鍏抽敭璇嶈韩浣嶅尮閰嶇粨鏋滐細{}", code, match_limit_up_result) - if not match_limit_up_result: - return cls.BLOCK_TYPE_NONE, None, "鏈湪娑ㄥ仠鍒楄〃涓湭鍖归厤鍒版定鍋滃師鍥�" - # 鑾峰彇鏉垮潡褰掔被 - for block in match_limit_up_result: - # 鑾峰彇寮哄娍鏉垮潡 - strong_result = kpl_block_util.is_strong_block(block, current_limit_up_datas, latest_2_day_limit_up_datas) - # 鑾峰彇鐚涙媺鏉垮潡 - soon_limit_up_result = kpl_block_util.is_soon_limit_up(code, block, limit_up_record_datas) - # 鑾峰彇韬綅 - rank = kpl_block_util.get_code_rank(code, block, limit_up_record_datas) - # 涓绘澘韬綅 - sh_sz_rank = kpl_block_util.get_sh_sz_code_rank(code, block, limit_up_record_datas) - # 鏄惁鍚庢帓 - is_back_row = kpl_block_util.is_back_row(code, block, current_limit_up_datas) + fresults = [] + if not keys: + return fresults, set() - # 鏄惁婊¤冻甯傚満娴佸叆鍓嶅嚑 - is_in_top_input = RealTimeKplMarketData.is_in_top(set([block]))[0] - - log.logger_kpl_debug.info("{}-{} 鏉垮潡鍒ゆ柇缁撴灉锛氬己鍔挎澘鍧�-{} 鐚涙媺鏉垮潡-{} 韬綅-{} 涓绘澘韬綅-{} 鏄惁鍚庢帓-{} 鏄惁鍦ㄦ祦鍏ュ墠鎺�-{}", code, block, - strong_result, soon_limit_up_result, rank, sh_sz_rank, is_back_row, - is_in_top_input) - - if time_index == 0: - # 09:30:00 - 10:00:00 - if strong_result[0]: - # 寮哄娍鏉垮潡 - # 涔颁富鏉块緳1,2,3,4 涔板悗鎺� - if is_back_row and sh_sz_rank <= 2: - return cls.BLOCK_TYPE_STRONG, block, f"{block} 寮哄娍鏉垮潡:涔颁富鏉块緳1,2,3 涔板悗鎺�" - - if soon_limit_up_result[0]: - # 鐚涙媺鏉垮潡 - # 鍙拱榫�2 涔板悗鎺� - if is_back_row and rank == 1: - return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�" - # 鍏朵粬鏉垮潡 - if is_in_top_input and sh_sz_rank <= 1 and is_back_row: - # 鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺� - return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡:鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺�" - - elif time_index == 1: - # 10:00:00 - 10:30:00 - if strong_result[0]: - # 寮哄娍鏉垮潡 - # 涔颁富鏉块緳1,2,3 涔板悗鎺� - if is_back_row and sh_sz_rank <= 2: - return cls.BLOCK_TYPE_STRONG, block, f"{block} 寮哄娍鏉垮潡:涔颁富鏉块緳1,2,3 涔板悗鎺�" - - if soon_limit_up_result[0]: - # 鐚涙媺鏉垮潡 - # 鍙拱榫�2 涔板悗鎺� - if is_back_row and rank == 1: - return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�" - # 鍏朵粬鏉垮潡 - if is_in_top_input and sh_sz_rank <= 1 and is_back_row: - # 鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺� - return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡:鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺�" - elif time_index == 2: - # 10:30:00 - 11:00:00 - if strong_result[0]: - # 寮哄娍鏉垮潡 - # 涔颁富鏉块緳1,2 涔板悗鎺� - if is_back_row and sh_sz_rank <= 1: - return cls.BLOCK_TYPE_STRONG, block, f"{block} 寮哄娍鏉垮潡:涔颁富鏉块緳1,2 涔板悗鎺�" - - if soon_limit_up_result[0]: - # 鐚涙媺鏉垮潡 - # 鍙拱榫�2 涔板悗鎺� - if is_back_row and rank == 1: - return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�" - # 鍏朵粬鏉垮潡 - if is_in_top_input and sh_sz_rank <= 1 and is_back_row: - # 鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺� - return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡:鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺�" - elif time_index == 3: - # 11:00:00 - 13:30:00 - if soon_limit_up_result[0]: - # 鐚涙媺鏉垮潡 - # 鍙拱榫�2 涔板悗鎺� - if is_back_row and rank == 1: - return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�" - # 鍏朵粬鏉垮潡 - if is_in_top_input and sh_sz_rank <= 1 and is_back_row: - # 鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺� - return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡锛氱湅绮鹃��/琛屼笟娴佸叆,涔伴緳涓绘澘1,2 ,涔板悗鎺�" - elif time_index == 4: - # 13:30:00 - 15:00:00 - if soon_limit_up_result[0]: - # 鐚涙媺鏉垮潡 - # 鍙拱榫�2 涔板悗鎺� - if is_back_row and rank == 1: - return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡锛氬彧涔伴緳2,涔板悗鎺�" - # 鍏朵粬鏉垮潡 - if is_in_top_input: - # 绮鹃��/琛屼笟娴佸叆绗﹀悎 - if sh_sz_rank <= 1 and is_back_row: - # 鐪嬬簿閫�/琛屼笟娴佸叆 涔伴緳涓绘澘1,2 涔板悗鎺� - return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡锛氱簿閫�/琛屼笟娴佸叆绗﹀悎,涔伴緳涓绘澘1,2,涔板悗鎺�" - else: - if sh_sz_rank == 0 and not is_back_row: - return cls.BLOCK_TYPE_START_UP, block, f"{block} 鍏朵粬鏉垮潡: 涔颁富鏉块緳1,涔颁富鏉跨嫭鑻�" - - return cls.BLOCK_TYPE_NONE, None, f"鏉垮潡({match_limit_up_result.keys()})涓嶇鍚堜拱鍏ユ潯浠�" + code_limit_up_reasons_dict = {} + load_code_block() + for block in keys: + can_buy, unique, msg, is_strong, is_active_buy, current_rank, block_limit_up_count, block_limit_up_record_count = cls.__is_block_can_buy_new( + 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, is_active_buy, current_rank, block_limit_up_count, + block_limit_up_record_count)) + return fresults, keys # 鏄惁鍙互涓嬪崟 - # 杩斿洖锛氭槸鍚﹀彲浠ヤ笅鍗�,娑堟伅,鏉垮潡绫诲瀷 + # 杩斿洖锛氬彲浠ヤ拱鐨勬澘鍧�,鏄惁鐙嫍,娑堟伅 + # 鍙拱鐨勬澘鍧�, 鏄惁鐙嫍, 娑堟伅, 鍙拱鐨勫己鍔挎澘鍧�, 鍏抽敭璇�, 绉瀬涔扮殑鏉垮潡 @classmethod - def can_buy(cls, code, current_limit_up_datas, latest_2_day_limit_up_datas, limit_up_record_datas): + def can_buy(cls, code): if constant.TEST: - return True, "", cls.BLOCK_TYPE_NONE - block_type, block, block_msg = cls.get_can_buy_block(code, current_limit_up_datas, latest_2_day_limit_up_datas, - limit_up_record_datas) - if block_type == cls.BLOCK_TYPE_NONE: - return False, block_msg, block_type + return [("娴嬭瘯", 0, 1, 1)], True, cls.BLOCK_TYPE_NONE, [], set(), ["鍖栧伐"] + # if True: + # # 娴嬭瘯 + # return True, "涓嶅垽鏂澘鍧楄韩浣�" + return cls.__can_buy_compute_result_dict.get(code) - # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------start------------ - # 鍒ゆ柇鍖归厤鍑虹殑娑ㄥ仠鍘熷洜锛屽垽鏂槸鍚︽湁宸茬粡涓嬪崟鐨勭エ - # reason_need_buy_dict = {} - # for k in match_limit_up_result: - # codes = match_limit_up_result[k] - # final_codes_keys = [keys] - # for code_ in codes: - # temp_key_set = set() - # temp_key_set |= cls.__CodesHisReasonAndBlocksManager.get_total_keys(code_) - # temp = cls.__LimitUpCodesPlateKeyManager.total_code_keys_dict.get(code_) - # if temp: - # temp_key_set |= temp - # # 浜岀骇 - # industry = global_util.code_industry_map.get(code_) - # if industry: - # temp_key_set.add(industry) - # - # final_codes_keys.append(temp_key_set) - # # 姹傚叡鍚岀殑鍏抽敭璇� - # intersection = set(final_codes_keys[0]) - # for s in final_codes_keys: - # intersection &= s - # log.logger_kpl_debug.info("{}鐨勬澘鍧楁眰浜ら泦锛歿}-{}", code, k, intersection) - # - # # 姹傚叕鍏辩殑鏉垮潡鏄惁鍦ㄦ祦鍏ュ墠5涓� - # is_in, valid_keys = RealTimeKplMarketData.is_in_top(intersection) - # if is_in: - # reason_need_buy_dict[k] = (is_in, valid_keys) - # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------end------------ - - # 鑾峰彇鏉垮潡鍙互涓嬪崟鐨勪釜鏁� - # can_buy_codes_count_dict = {} - # - # for key__ in match_limit_up_result: - # can_buy_count, msg = RealTimeKplMarketData.get_can_buy_codes_count(code, key__) - # can_buy_codes_count_dict[key__] = can_buy_count - - # has_available_key = False - # for key in can_buy_codes_count_dict: - # if can_buy_codes_count_dict[key] > 0: - # has_available_key = True - # break - # if not has_available_key: - # return False, f"鍖归厤鍒扮殑銆恵','.join(match_limit_up_result.keys())}銆戞病鍦ㄧ簿閫�/琛屼笟鍙互涔板叆鐨勬澘鍧椾腑" - - # ---------------------------------鍔犺浇宸茬粡涓嬪崟/鎴愪氦鐨勪唬鐮佷俊鎭�------------start------------- - # match_reasons = match_limit_up_result.keys() - # 鍒ゆ柇鍖归厤鍒扮殑鍘熷洜鏄惁宸茬粡鏈変笅鍗�/涔板叆鎴愬姛鐨勪唬鐮� - codes_delegate = set(trade_manager.get_codes_by_trade_states( - {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER})) - codes_success = set(trade_manager.get_codes_by_trade_states( - {trade_manager.TRADE_STATE_BUY_SUCCESS})) - + # 杩斿洖:(鍙互涔扮殑鏉垮潡鍒楄〃, 鏄惁鏄嫭鑻�, 娑堟伅绠�浠�,鍙拱鐨勫己鍔夸富绾�, 绉瀬涔板叆鏉垮潡鍒楄〃) + @classmethod + def __compute_can_buy_blocks(cls, 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): + # 鏍规嵁浠g爜娉涘寲鏉垮潡鑾峰彇娉涘寲鏉垮潡鐨勪唬鐮侀泦鍚� + high_level_general_block_codes = {} + for c in high_level_general_code_blocks: + blocks = high_level_general_code_blocks[c] + for b in blocks: + if b not in high_level_general_block_codes: + high_level_general_block_codes[b] = set() + high_level_general_block_codes[b].add(c) + blocks_compute_results, keys = 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, + high_level_general_code_blocks, + high_level_general_block_codes) + if not blocks_compute_results: + return False, True, f"娌℃湁鎵惧埌鏉垮潡", [], keys, [] + codes_delegate = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache( + {trade_constant.TRADE_STATE_BUY_DELEGATED, trade_constant.TRADE_STATE_BUY_PLACE_ORDER})) + codes_success = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache( + {trade_constant.TRADE_STATE_BUY_SUCCESS})) codes = codes_delegate | codes_success - # 缁熻鎴愪氦浠g爜鐨勬澘鍧� 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_ + # 瀹炴椂娑ㄥ仠鍘熷洜 + 鎺ㄨ崘鍘熷洜 + if not k1_: + trade_codes_blocks_dict[c] = k4_ + else: + trade_codes_blocks_dict[c] = k1_ # 缁熻鏉垮潡涓殑浠g爜 trade_block_codes_dict = {} for c in trade_codes_blocks_dict: @@ -548,39 +1007,602 @@ 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------------- + # + can_buy_blocks = [] + can_buy_strong_blocks = [] + unique_count = 0 msg_list = [] - for key in [block]: - # 鏉垮潡涓凡缁忔湁鎴愪氦鐨勫氨涓嶄笅鍗曚簡 - if key in trade_success_blocks_count: - success_codes_count = len(trade_success_blocks_count[key]) - if success_codes_count >= 2: - msg_list.append(f"銆恵key}銆戜腑宸茬粡鏈墈success_codes_count}涓垚浜や唬鐮�") - log.logger_kpl_debug.debug(f"{code}锛氭澘鍧楋紙{key}锛夊凡缁忔湁鎴愪氦銆恵trade_success_blocks_count[key]}銆�") - continue - # 10:30浠ュ悗涔�1涓� - if int(tool.get_now_time_str().replace(":", "")) > int("103000") and 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, block_type - # 鏉垮潡鍙互涓嬪崟鏁伴噺 - # if trade_block_codes_dict.get(key) is None or len(trade_block_codes_dict.get(key)) < \ - # can_buy_codes_count_dict[key]: - # order_count = len(trade_block_codes_dict.get(key)) if key in trade_block_codes_dict else 0 - # logger_kpl_block_can_buy.info( - # f"code={code}锛氥�恵key}銆戝彲浠ヤ笅鍗曪紝鐜版湁鏁伴噺锛歿order_count} 鏈�澶ф暟閲忥細{can_buy_codes_count_dict[key]}") - # return True, f"鍙互涓嬪崟锛屾澘鍧�:銆恵key}銆�,鏉垮潡涓凡缁忎笅鍗曠殑鏁伴噺锛歿order_count}" - # else: - # order_count = len(trade_block_codes_dict.get(key)) - # msg_list.append(f"銆恵key}銆戜腑涓嬪崟浠g爜鏁伴噺{order_count}/鍏佽涓嬪崟鏁伴噺{can_buy_codes_count_dict[key]}") + active_buy_blocks = [] + for r in blocks_compute_results: + # r鐨勬暟鎹粨鏋�(鏉垮潡,鏄惁鍙互涔�,鏄惁鐙嫍,娑堟伅,鏄惁鏄己鍔挎澘鍧�, 绉瀬涔板叆淇℃伅) + if r[2]: + # 鐙嫍 + unique_count += 1 + if r[1]: + # 寮哄娍涓荤嚎鏈�澶氬悓鏃舵寕3鍙エ锛屾渶澶氭垚浜�2鍙エ + MAX_DELEGATE_COUNT = 3 if r[4] else 2 + MAX_DEAL_COUNT = 2 if r[4] else 1 + # if r[0] in trade_success_blocks_count and len(trade_success_blocks_count[r[0]]) >= MAX_DEAL_COUNT: + # msg_list.append(f"銆恵r[0]}銆戞湁鎴愪氦浠g爜锛歿trade_success_blocks_count[r[0]]}") + # continue + # if r[0] in trade_delegate_blocks_count and len(trade_delegate_blocks_count[r[0]]) >= MAX_DELEGATE_COUNT: + # msg_list.append(f"銆恵r[0]}銆戝凡鎸傚崟锛歿trade_delegate_blocks_count[r[0]]}") + # continue + if len(r) > 8: + can_buy_blocks.append((r[0], r[6], r[7], r[8])) + else: + # 锛堟澘鍧楀悕绉�,韬綅,鏉垮潡娑ㄥ仠鏁伴噺锛� + can_buy_blocks.append((r[0], 0, 1, 1)) + if r[4]: + can_buy_strong_blocks.append(r[0]) + if r[3]: + msg_list.append(r[3]) + if r[5]: + active_buy_blocks.append(r[0]) + msg_list.append(f"銆恵r[0]}銆戠Н鏋佷拱鍏�({r[5]})") + 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, active_buy_blocks + return can_buy_blocks, False, ",".join(msg_list), can_buy_strong_blocks, keys, active_buy_blocks - return False, ",".join(msg_list), block_type + # 鏇存柊浠g爜鏉垮潡鍒ゆ柇鏄惁鍙互涔扮殑缁撴灉 + # high_level_general_code_blocks 楂樹綅娉涘寲鏉垮潡 + @classmethod + def update_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas, + latest_current_limit_up_records, + before_blocks_dict, current_limit_up_block_codes_dict): + yesterday_current_limit_up_codes = set() + yesterday_current_limit_up_records_dict = {} + yesterday_current_limit_up_records = latest_current_limit_up_records[0][1] + if yesterday_current_limit_up_records: + for r in yesterday_current_limit_up_records: + yesterday_current_limit_up_codes.add(r[0]) + yesterday_current_limit_up_records_dict[r[0]] = r + high_level_general_code_blocks = {} + # 鏄惁鏄�3鏉垮強浠ヤ笂鐨勯珮浣嶆澘 + for r in current_limit_up_datas: + count = kpl_util.get_high_level_count(r[4]) + if count >= 3 and r[0] in yesterday_current_limit_up_codes: + latest_datas = latest_current_limit_up_records[:count - 1] + # 鏄珮浣嶆澘 + # 褰撴棩绮鹃�� + blocks = set(r[6].split("銆�")) + for d in latest_datas: + for dd in d[1]: + if dd[0] == r[0]: + blocks.add(dd[5]) + break + f_blocks = [] + for b in blocks: + if b: + f_blocks.append(b) + high_level_general_code_blocks[r[0]] = f_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, active_buy_blocks) + + +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): + """ + 璁$畻寮�1鐨勪唬鐮佷俊鎭紝涓嶅寘鍚�5鏉夸互涓婄殑 + @param current_limit_up_datas: + @return: + """ + timestamp_start, timestamp_end = kpl_block_util.open_limit_up_time_range + temp_dict = {} + for d in current_limit_up_datas: + code = d[0] + # d: (浠g爜, 鍚嶇О, 棣栨娑ㄥ仠鏃堕棿, 鏈�杩戞定鍋滄椂闂�, 鍑犳澘, 娑ㄥ仠鍘熷洜, 鏉垮潡, 瀹為檯娴侀��, 涓诲姏鍑�棰�,娑ㄥ仠鍘熷洜浠g爜,娑ㄥ仠鍘熷洜浠g爜鏁伴噺) + # 璁$畻鏄惁寮�1 + if int(d[2]) >= timestamp_end or int(d[2]) < timestamp_start: + continue + buy1_money = huaxin_l1_data_manager.get_buy1_money(code) + # 涔�1鏄惁澶т簬5000w + if not constant.TEST: + if not buy1_money or buy1_money < 5e7: + continue + if not tool.is_can_buy_code(code): + continue + blocks = {d[5]} + 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, exclude_codes=None, limit_up_time=None): + """ + 鑾峰彇褰撳墠娑ㄥ仠韬綅 + @param code: + @param block: + @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 + 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]) + 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 + # 浠g爜.娑ㄥ仠鏃堕棿 + 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, exclude_codes=None): + """ + 鑾峰彇鍘嗗彶娑ㄥ仠韬綅 + @param code: + @param block: + @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]) + 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 + # 鍓旈櫎鐐告澘浠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]) + 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 鏄ㄦ棩娑ㄥ仠浠g爜 + @return: + """ + # 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: + 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_dict: + open_limit_up_block_codes_dict[b] = set() + open_limit_up_block_codes_dict[b].add(c) + if block not in open_limit_up_block_codes_dict: + return False, "鏉垮潡鏈紑1" + open_limit_up_block_codes = list(open_limit_up_block_codes_dict.get(block)) + count = len(open_limit_up_block_codes) + # ----鑾峰彇鍘嗗彶韬綅---- + 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, + 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 + exclude_codes.clear() + else: + # 鑾峰彇鍖呭惈楂樹綅鏉跨殑韬綅 + # ----鑾峰彇鍘嗗彶韬綅---- + 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}锛屽巻鍙�-{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): + """ + 鏄惁婵�杩涗拱锛堟澘鍧楃獊鐒舵定璧锋潵锛� + 1.鑰佷簩鍜岃�佷笁鐨勬定鍋滄椂闂寸浉宸�5鍒嗛挓鍐� + 2.鑰佷笁涓嶈兘涔伴『浣嶅埌鑰佸洓锛堣�佸洓涓庤�佷笁鐩稿樊10鍒嗛挓鍐咃級 + 3.鍓�2涓エ涓嶈兘鐐告澘锛堝巻鍙茶韩浣嶄笌鐜板湪韬綅涓�鑷达級 + 4.闄ゅ紑鍓嶄袱涓唬鐮佸彲涔拌��1涓庤��2 + 5.涔拌��2鐨勬儏鍐碉細鑰�1涓嶆弧瓒充拱鍏ユ潯浠� + + @param code: + @param block: + @param yesterday_limit_up_codes: + @return: + """ + + # 鑾峰彇褰撳墠浠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): + """ + 鏄惁鏄縺杩涗拱 + @param code: + @return: {婵�杩涗拱鐨勬澘鍧梷, 鍘熷洜 + """ + # 璁$畻 + # 鑾峰彇寮�1鐨勬澘鍧� + open_limit_up_code_dict = kpl_data_constant.open_limit_up_code_dict_for_radical_buy + open_limit_up_blocks = set() + if open_limit_up_code_dict: + for c in open_limit_up_code_dict: + open_limit_up_blocks |= open_limit_up_code_dict[c][1] + # 鑾峰彇浠g爜鐨勬澘鍧� + keys_, info = cls.get_code_blocks(code) + if not keys_: + return set(), "娌¤幏鍙栧埌鏉垮潡浜ら泦" + + match_blocks = open_limit_up_blocks & keys_ + can_buy_blocks = set() + fmsges = [] + 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[1]}") + fmsges.append("寮�1鍒ゆ柇##" + ",".join(msges)) + if not can_buy_blocks: + msges.clear() + 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[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) if __name__ == "__main__": -- Gitblit v1.8.0