From fb47d36048e94b9a506d5c153e3dd19a01e37df1 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期一, 30 十月 2023 16:30:27 +0800 Subject: [PATCH] bug修复 --- third_data/code_plate_key_manager.py | 633 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 534 insertions(+), 99 deletions(-) diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py index 4a13787..1db7f34 100644 --- a/third_data/code_plate_key_manager.py +++ b/third_data/code_plate_key_manager.py @@ -6,64 +6,134 @@ import json import constant -import global_data_loader -import global_util -import log -import tool -from db import redis_manager +from db.redis_manager_delegate import RedisUtils +from third_data import kpl_block_util, kpl_api +from utils import global_util, tool +from log_module import log, async_log_util +from db import redis_manager_delegate as redis_manager -from log import logger_kpl_limit_up -from trade import trade_manager +from log_module.log import logger_kpl_limit_up, logger_kpl_block_can_buy +from third_data.kpl_util import KPLPlatManager +from trade import trade_manager, l2_trade_util -# 瀹炴椂寮�鐩樺暒甯傚満鏁版嵁 -class RealTimeKplMarketData: - # 绮鹃�夊墠5 - top_5_reason_set = set() - # 琛屼笟鍓�5 - top_5_industry_set = set() +# 浠g爜绮鹃�夋澘鍧楃鐞� +class KPLCodeJXBlockManager: + __db = 3 + __redisManager = redis_manager.RedisManager(3) + __code_blocks = {} + # 澶囩敤 + __code_by_blocks = {} - @classmethod - def set_top_5_reasons(cls, datas): - temp_set = set() - base_count = 5 - for i in range(0, len(datas)): - if datas[i][1] in constant.KPL_INVALID_BLOCKS: - base_count += 1 - if i >= base_count: - break - if datas[i][3] > 5000 * 10000: - temp_set.add(datas[i][1]) - cls.top_5_reason_set = temp_set + __instance = None - @classmethod - def set_top_5_industry(cls, datas): - temp_set = set() - base_count = 5 - for i in range(0, len(datas)): - if datas[i][1] in constant.KPL_INVALID_BLOCKS: - base_count += 1 - if i >= base_count: - break + def __new__(cls, *args, **kwargs): + if not cls.__instance: + cls.__instance = super(KPLCodeJXBlockManager, cls).__new__(cls, *args, **kwargs) + return cls.__instance - if datas[i][2] > 5000 * 10000: - temp_set.add(datas[i][1]) - cls.top_5_reason_set = temp_set + def __get_redis(self): + return self.__redisManager.getRedis() - # 鑾峰彇鑳藉涔扮殑琛屼笟鍏抽敭瀛梥et - @classmethod - def get_can_buy_key_set(cls): - temp_set = cls.top_5_reason_set | cls.top_5_industry_set - return temp_set + def save_jx_blocks(self, code, blocks, by=False): + if blocks is None: + return + if len(blocks) > 2: + blocks = blocks[:2] - @classmethod - def is_in_top(cls, keys): - reasons = cls.get_can_buy_key_set() - temp_set = keys & reasons - if temp_set: - return True, temp_set + # 淇濆瓨鍓�2鏉℃暟鎹� + if by: + RedisUtils.setex_async(self.__db, f"kpl_jx_blocks_by-{code}", tool.get_expire(), json.dumps(blocks)) + self.__code_by_blocks[code] = blocks else: - return False, None + RedisUtils.setex_async(self.__db, f"kpl_jx_blocks-{code}", tool.get_expire(), json.dumps(blocks)) + self.__code_blocks[code] = blocks + + # 鑾峰彇绮鹃�夋澘鍧� + 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 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): + if limit_up_price and buy_1_price: + # 澶勭悊涔�1,鍗�1淇℃伅 + pre_close_price = round(float(limit_up_price) / 1.1, 2) + # 濡傛灉娑ㄥ箙澶т簬7%灏辫鍙栨澘鍧� + price_rate = (buy_1_price - pre_close_price) / pre_close_price + if price_rate > 0.07: + if not self.get_jx_blocks_cache(code): + blocks = kpl_api.getCodeJingXuanBlocks(code) + self.save_jx_blocks(code, blocks) + async_log_util.info(logger_kpl_block_can_buy,f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�-{blocks}") + elif price_rate > 0.03: + # 娣诲姞澶囩敤鏉垮潡 + if not self.get_jx_blocks_cache(code, by=True): + blocks = kpl_api.getCodeJingXuanBlocks(code) + self.save_jx_blocks(code, blocks, by=True) + async_log_util.info(logger_kpl_block_can_buy, f"{code}:鑾峰彇鍒扮簿閫夋澘鍧�(澶囩敤)-{blocks}") + + + +# 寮�鐩樺暒绂佹浜ゆ槗鏉垮潡绠$悊 +class KPLPlateForbiddenManager: + __redisManager = redis_manager.RedisManager(3) + __kpl_forbidden_plates_cache = set() + + __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.__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 RedisUtils.smembers(self.__get_redis(), "kpl_forbidden_plates") + + def list_all_cache(self): + return self.__kpl_forbidden_plates_cache class LimitUpCodesPlateKeyManager: @@ -97,16 +167,18 @@ # 浠婃棩娑ㄥ仠鍘熷洜鍙樺寲 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()) + 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() - 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: @@ -114,6 +186,8 @@ self.total_key_codes_dict[k].add(code) logger_kpl_limit_up.info("{}鏉垮潡鍏抽敭璇�:{}", code, keys) + + # 鏍规嵁浼犲叆鐨勫叧閿瘝涓庢定鍋滀唬鐮佷俊鎭尮閰嶈韩浣� def get_codes_by_key_without_mine(self, key, code): # 鍙瘮杈冧粖鏃ユ定鍋滃師鍥� @@ -123,14 +197,134 @@ codes_set.discard(code) return codes_set + # 娑ㄥ仠鍘熷洜鍖归厤鍏抽敭瀛�(鍜屾定鍋滃垪琛ㄤ腑鐨勬定鍋滃師鍥犲仛瀵规瘮),杩斿洖:{鍏抽敭璇�:浠g爜闆嗗悎} + def match_limit_up_reason_keys(self, code, keys): + fresult = {} + for k in keys: + if k in self.total_key_codes_dict: + codes = set(self.total_key_codes_dict[k]) + codes.discard(code) + if codes: + fresult[k] = codes + return fresult -# 鐩爣浠g爜鍏抽敭璇嶇鐞� -class TargetCodePlateKeyManager: + +# 瀹炴椂寮�鐩樺暒甯傚満鏁版嵁 +class RealTimeKplMarketData: + # 绮鹃�夊墠5 + top_5_reason_list = [] + # 琛屼笟鍓�5 + top_5_industry_list = [] + # + top_5_key_dict = {} + total_reason_dict = {} + total_industry_dict = {} + __KPLPlateForbiddenManager = KPLPlateForbiddenManager() + __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager() + __KPLPlatManager = KPLPlatManager() + + @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 + + 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_top_5_industry(cls, datas): + for d in datas: + cls.total_industry_dict[d[1]] = d + temp_list = [] + for i in range(0, len(datas)): + if datas[i][1] in constant.KPL_INVALID_BLOCKS: + continue + temp_list.append((datas[i][1], datas[i][2], len(temp_list))) + if len(temp_list) > 10: + break + if datas[i][2] < 3 * 10000 * 10000: + break + cls.top_5_industry_list = temp_list + cls.__reset_top_5_dict() + + @classmethod + def __reset_top_5_dict(cls): + temp_dict = {} + for t in cls.top_5_industry_list: + temp_dict[t[0]] = t + for t in cls.top_5_reason_list: + temp_dict[t[0]] = t + cls.top_5_key_dict = temp_dict + + # 鑾峰彇鑳藉涔扮殑琛屼笟鍏抽敭瀛梥et + @classmethod + def get_can_buy_key_set(cls): + 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() + forbidden_plates = cls.__KPLPlateForbiddenManager.list_all_cache() + reasons = reasons - forbidden_plates + temp_set = keys & reasons + if temp_set: + return True, temp_set + else: + return False, None + + +# 浠g爜鍘嗗彶娑ㄥ仠鍘熷洜涓庢澘鍧楃鐞� +class CodesHisReasonAndBlocksManager: __redisManager = redis_manager.RedisManager(1) # 鍘嗗彶娑ㄥ仠鍘熷洜 __history_limit_up_reason_dict = {} - # 浜岀骇琛屼笟 - __second_industry_dict = {} # 鏉垮潡 __blocks_dict = {} @@ -139,56 +333,90 @@ 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))) + 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 - return self.__history_limit_up_reason_dict.get(code) + if code in self.__history_limit_up_reason_dict: + return self.__history_limit_up_reason_dict.get(code) + else: + return None 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 - return self.__blocks_dict.get(code) + if code in self.__blocks_dict: + return self.__blocks_dict.get(code) + else: + return None else: return reasons - # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,鏉垮潡 + def get_total_keys(self, code): + reasons = self.get_history_limit_up_reason(code) + if reasons is None: + reasons = set() + blocks = self.get_blocks(code) + if blocks is None: + blocks = set() + return reasons | blocks + + +# 鐩爣浠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): 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 = set() - if code in self.__history_limit_up_reason_dict: - k2 = self.__history_limit_up_reason_dict[code] + # 鍔犺浇浠婃棩鍘嗗彶鍘熷洜,鏆傛椂涓嶉渶瑕佸巻鍙插師鍥犱簡 + 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 = set() - if code in self.__blocks_dict: - k4 = self.__blocks_dict[code] + jingxuan_blocks = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code) + if not jingxuan_blocks: + jingxuan_blocks = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code, by=True) + if jingxuan_blocks: + jingxuan_blocks = jingxuan_blocks[:2] + k4 |= set([x[1] for x in jingxuan_blocks]) for k in [k1, k11, k2, k3, k4]: keys |= k @@ -199,42 +427,249 @@ class CodePlateKeyBuyManager: + # 鏃犳澘鍧� + BLOCK_TYPE_NONE = -1 + # 涓�鑸澘鍧� + BLOCK_TYPE_COMMON = 0 + # 寮哄娍鏉垮潡 + BLOCK_TYPE_STRONG = 1 + # 鐚涙媺鏉垮潡 + BLOCK_TYPE_SOON_LIMIT_UP = 2 + # 娼滀紡鏉垮潡 + BLOCK_TYPE_START_UP = 3 + __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_manager.TRADE_STATE_NOT_TRADE: + # 鍙涓嬭繃鍗曠殑灏变笉绉婚櫎 + return + l2_trade_util.forbidden_trade(code, msg=msg) + logger_kpl_block_can_buy.info(msg) + + @classmethod + def __is_block_can_buy(cls, code, block, current_limit_up_datas, code_limit_up_reason_dict, + yesterday_current_limit_up_codes, limit_up_record_datas): + # log.logger_kpl_debug.info(f"鍒ゆ柇鏉垮潡鏄惁鍙拱锛歿block}") + # is_top_8_record, top_8_record = kpl_block_util.is_record_top_block(code, block, limit_up_record_datas, + # yesterday_current_limit_up_codes, 50) + # is_top_4_current, top_4_current = kpl_block_util.is_current_top_block(code, block, current_limit_up_datas, + # yesterday_current_limit_up_codes, 50) + # is_top_4 = is_top_8_record and is_top_4_current + # msg_list.append(f"\n瀹炴椂top10(娑ㄥ仠鏁伴噺锛歿len(current_limit_up_datas)})") + # msg_list.append(f"鍘嗗彶top20(娑ㄥ仠鏁伴噺锛歿len(top_8_record)})") + + # 鑾峰彇涓绘澘瀹炴椂韬綅,鍓旈櫎楂樹綅鏉� + current_shsz_rank, front_current_shsz_rank_codes = kpl_block_util.get_code_current_rank(code, block, + current_limit_up_datas, + code_limit_up_reason_dict, + yesterday_current_limit_up_codes, + shsz=True) + record_shsz_rank, record_shsz_rank_codes = kpl_block_util.get_code_record_rank(code, block, + limit_up_record_datas, + code_limit_up_reason_dict, + yesterday_current_limit_up_codes, + shsz=True) + # 鑾峰彇涓绘澘鍘嗗彶韬綅 + + open_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas, + code_limit_up_reason_dict) + 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}/{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}锛�" + + # 鑾峰彇鍙互涔扮殑鏉垮潡 + # 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, limit_up_record_datas, yesterday_current_limit_up_codes, + before_blocks_dict): + # 鍔犺浇娑ㄥ仠浠g爜鐨勭洰鏍囨澘鍧� + def load_code_block(): + for d in limit_up_record_datas: + if d[2] in constant.KPL_INVALID_BLOCKS and d[3] in before_blocks_dict: + code_limit_up_reason_dict[d[3]] = list(before_blocks_dict.get(d[3]))[0] + else: + code_limit_up_reason_dict[d[3]] = d[2] + return code_limit_up_reason_dict + + if current_limit_up_datas is None: + current_limit_up_datas = [] + + # 鑾峰彇鐩爣浠g爜鏉垮潡 + 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 k1: + for k in k1: + if k not in constant.KPL_INVALID_BLOCKS: + keys.add(k) + # 濮嬬粓鑾峰彇绮鹃�夋澘鍧� + if True: + # 鑾峰彇 + if k4: + keys |= k4 + keys = keys - constant.KPL_INVALID_BLOCKS + + # log.logger_kpl_debug.info("{}鏈�缁堝叧閿瘝锛歿}", code, keys) + + # 娑ㄥ仠鍒楄〃涓尮閰嶅叧閿瘝锛岃繑鍥烇紙鏉垮潡:浠g爜闆嗗悎锛夛紝浠g爜闆嗗悎涓凡缁忔帓闄よ嚜韬� + if not keys: + return None, "灏氭湭鎵惧埌娑ㄥ仠鍘熷洜" + code_limit_up_reason_dict = {} + load_code_block() + msg_list = [] + + can_buy_blocks = [] + for block in keys: + + can_buy, msg = cls.__is_block_can_buy(code, block, current_limit_up_datas, code_limit_up_reason_dict, + yesterday_current_limit_up_codes, limit_up_record_datas) + if can_buy: + can_buy_blocks.append((block, msg)) + else: + msg_list.append(msg) + if len(can_buy_blocks) == len(keys): + blocks = [x[0] for x in can_buy_blocks] + blocks_msg = "\n".join([x[1] for x in can_buy_blocks]) + return blocks, blocks_msg + + return None, "\n".join(msg_list) # 鏄惁鍙互涓嬪崟 + # 杩斿洖锛氭槸鍚﹀彲浠ヤ笅鍗�,娑堟伅,鏉垮潡绫诲瀷 @classmethod def can_buy(cls, code): - keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code) - # 鏉垮潡Key鏄惁鍦ㄥ競鍦哄墠5key涓� - is_in, valid_keys = RealTimeKplMarketData.is_in_top(keys) - if not valid_keys: - return False, "鏉垮潡鏈湪甯傚満娴佸叆鍓�5" - # 鐩稿悓鏉垮潡涓槸鍚﹀凡缁忔湁鍒殑绁ㄦ定鍋� - is_back = False, '' - for key in valid_keys: - codes = cls.__LimitUpCodesPlateKeyManager.get_codes_by_key_without_mine(key, code) - if codes and len(codes) > 0: - is_back = True, key - break - if not is_back[0]: - return False, f"鏉垮潡涓涓定鍋滐細{valid_keys}" - # 鐪嬫澘鍧椾腑鏄惁宸茬粡鏈夊凡缁忎笅鍗曠殑鎴栬�呮垚浜ょ殑浠g爜 - codes = trade_manager.get_codes_by_trade_states( - {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER, - trade_manager.TRADE_STATE_BUY_SUCCESS}) - # 閬嶅巻宸茬粡鎴愪氦/涓嬪崟鐨勪唬鐮侊紝鑾峰彇鍏舵定鍋滃師鍥狅紝鐒跺悗鍜屽綋鍓嶄唬鐮佹定鍋滃師鍥犲仛姣旇緝锛屾湁鐩稿悓浠g爜鐨勪笉鑳戒拱 + if constant.TEST: + return True, cls.BLOCK_TYPE_NONE + # if True: + # # 娴嬭瘯 + # return True, "涓嶅垽鏂澘鍧楄韩浣�" + return cls.__can_buy_compute_result_dict.get(code) + + @classmethod + def __compute_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas, + yesterday_current_limit_up_codes, before_blocks_dict): + + blocks, block_msg = cls.get_can_buy_block(code, current_limit_up_datas, + limit_up_record_datas, yesterday_current_limit_up_codes, + before_blocks_dict) + if not blocks: + return False, block_msg + codes_delegate = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache( + {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER})) + codes_success = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache( + {trade_manager.TRADE_STATE_BUY_SUCCESS})) + + codes = codes_delegate | codes_success + + # 缁熻鎴愪氦浠g爜鐨勬澘鍧� + trade_codes_blocks_dict = {} + # 宸茬粡鎴愪氦鐨勬澘鍧� + trade_success_blocks_count = {} for c in codes: keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(c) # 瀹炴椂娑ㄥ仠鍘熷洜 - for k_ in k1_: - # 褰撳墠浠g爜宸茬粡鏈夋寕鐨勬垨鑰呮垚浜ょ殑 - if k_ in valid_keys: - return False, f"{k_}鏉垮潡涓殑{c}宸茬粡涓嬪崟/涔板叆鎴愬姛锛屽悓涓�鏉垮潡涓彧鑳戒拱1涓エ" - return True, f"娑ㄥ仠鍘熷洜锛歿is_back[1]}" + trade_codes_blocks_dict[c] = k1_ | k4_ + # 缁熻鏉垮潡涓殑浠g爜 + trade_block_codes_dict = {} + for c in trade_codes_blocks_dict: + for b in trade_codes_blocks_dict[c]: + if c in codes_success: + if b not in trade_success_blocks_count: + trade_success_blocks_count[b] = set() + trade_success_blocks_count[b].add(c) + if b not in trade_block_codes_dict: + trade_block_codes_dict[b] = set() + trade_block_codes_dict[b].add(c) + + # ---------------------------------鍔犺浇宸茬粡涓嬪崟/鎴愪氦鐨勪唬鐮佷俊鎭�------------end------------- + msg_list = [] + for key in blocks: + # 鏉垮潡涓凡缁忔湁鎴愪氦鐨勫氨涓嶄笅鍗曚簡 + if key in trade_success_blocks_count: + success_codes_count = len(trade_success_blocks_count[key]) + if success_codes_count >= 1: + msg_list.append(f"銆恵key}銆戜腑宸茬粡鏈墈success_codes_count}涓垚浜や唬鐮�") + log.logger_kpl_debug.debug(f"{code}锛氭澘鍧楋紙{key}锛夊凡缁忔湁鎴愪氦銆恵trade_success_blocks_count[key]}銆�") + continue + return True, block_msg + + return False, ",".join(msg_list) + + # 鏇存柊浠g爜鏉垮潡鍒ゆ柇鏄惁鍙互涔扮殑缁撴灉 + @classmethod + def update_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas, + yesterday_current_limit_up_codes, + before_blocks_dict): + can_buy, msg = cls.__compute_can_buy_blocks(code, current_limit_up_datas, limit_up_record_datas, + yesterday_current_limit_up_codes, + before_blocks_dict) + # 淇濆瓨鏉垮潡璁$畻缁撴灉 + cls.__can_buy_compute_result_dict[code] = (can_buy, msg) + + # 鍒ゆ柇鏄惁涓虹湡鑰佸ぇ + @classmethod + def __is_real_first_limit_up(cls, code, block, current_limit_up_datas, limit_up_record_datas, + yesterday_current_limit_up_codes, + before_blocks_dict): + # 鍔犺浇娑ㄥ仠浠g爜鐨勭洰鏍囨澘鍧� + def load_code_block(): + if limit_up_record_datas: + for d in limit_up_record_datas: + if d[2] in constant.KPL_INVALID_BLOCKS and d[3] in before_blocks_dict: + code_limit_up_reason_dict[d[3]] = list(before_blocks_dict.get(d[3]))[0] + else: + code_limit_up_reason_dict[d[3]] = d[2] + return code_limit_up_reason_dict + + if current_limit_up_datas is None: + current_limit_up_datas = [] + if limit_up_record_datas is None: + limit_up_record_datas = [] + code_limit_up_reason_dict = {} + load_code_block() + can_buy, msg = cls.__is_block_can_buy(code, block, current_limit_up_datas, code_limit_up_reason_dict, + yesterday_current_limit_up_codes, limit_up_record_datas) + return can_buy, msg + + @classmethod + def is_need_cancel(cls, code, limit_up_reason, current_limit_up_datas, limit_up_record_datas, + yesterday_current_limit_up_codes, + before_blocks_dict): + can_buy, msg = cls.__is_real_first_limit_up(code, limit_up_reason, current_limit_up_datas, + limit_up_record_datas, + yesterday_current_limit_up_codes, + before_blocks_dict) + if not can_buy: + logger_kpl_block_can_buy.warning(f"{code} 鏍规嵁娑ㄥ仠鍘熷洜锛坽limit_up_reason}锛夊尮閰嶄笉鑳戒拱") + return not can_buy if __name__ == "__main__": - datas = log.load_kpl_reason_changes() - for k in datas: - LimitUpCodesPlateKeyManager().set_today_limit_up_reason_change(k[0], k[1], k[2]) + pass -- Gitblit v1.8.0