# 最近涨停最大买1金额管理 """ 今日最高价管理 """ import json from db import redis_manager_delegate as redis_manager from db.redis_manager_delegate import RedisUtils from utils import tool class LatestLimitUpMaxBuy1MoneyInfoManager: # {代码:[(时间, (买1价, 买1量))]} __latest_max_buy1_money_list_cache = {} __db = 4 _redisManager = redis_manager.RedisManager(5) __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = super(LatestLimitUpMaxBuy1MoneyInfoManager, cls).__new__(cls, *args, **kwargs) cls.__load_data() return cls.__instance @classmethod def __load_data(cls): redis = cls._redisManager.getRedis() keys = RedisUtils.keys(redis, "latest_max_buy1_list-*", auto_free=False) for key in keys: code = key.split("-")[-1] val = RedisUtils.get(redis, key, auto_free=False) if val: val = json.loads(val) cls.__latest_max_buy1_money_list_cache[code] = val def __sync_data(self, code): """ 同步数据 @param code: @return: """ if code not in self.__latest_max_buy1_money_list_cache: return RedisUtils.setex_async(self.__db, f"latest_max_buy1_list-{code}", tool.get_expire(), json.dumps(self.__latest_max_buy1_money_list_cache[code])) def set_price_info(self, code, time_str, buy1_info, sell1_info): """ 设置价格信息 @param sell1_info: (卖1价, 卖1量) @param code: @param time_str: @param buy1_info: 卖1信息:(买1价, 买1量) @return: """ # 09:30:00之前不需要处理 if time_str < '09:30:00': return if code not in self.__latest_max_buy1_money_list_cache: self.__latest_max_buy1_money_list_cache[code] = [] # 是否是涨停 if sell1_info[1] < 1: # 涨停价 # 如果原来没有值需要赋当前值 if not self.__latest_max_buy1_money_list_cache[code]: self.__latest_max_buy1_money_list_cache[code].append((time_str, buy1_info)) self.__sync_data(code) else: # 原来有值, 看是否为空 if not self.__latest_max_buy1_money_list_cache[code][-1][1] or self.__latest_max_buy1_money_list_cache[code][-1][1][1] < buy1_info[1]: # 空值 self.__latest_max_buy1_money_list_cache[code][-1] = (time_str, buy1_info) self.__sync_data(code) else: # 非涨停价 if not self.__latest_max_buy1_money_list_cache[code]: self.__latest_max_buy1_money_list_cache[code].append((time_str, None)) if self.__latest_max_buy1_money_list_cache[code][-1][1]: # 原来没有空值 self.__latest_max_buy1_money_list_cache[code].append((time_str, None)) # 删除之前N-2条数据 if len(self.__latest_max_buy1_money_list_cache[code]) > 2: self.__latest_max_buy1_money_list_cache[code] = self.__latest_max_buy1_money_list_cache[code][-2:] self.__sync_data(code) # async_log_util.info(logger_limit_up_record, f"{code}-{limit_up_info}") def get_latest_info_list(self, code): """ 获取最近的信息列表 @param code: @return: """ return self.__latest_max_buy1_money_list_cache.get(code) def get_latest_info(self, code): """ @param code: @return: """ mlist = self.__latest_max_buy1_money_list_cache.get(code) if not mlist: return None for i in range(-1, -3, -1): if mlist[i][1]: return mlist[i] return None if __name__ == "__main__": code = "000333" LatestLimitUpMaxBuy1MoneyInfoManager().set_price_info(code, '09:49:00', (54.50, 100000), (54.50, 100)) LatestLimitUpMaxBuy1MoneyInfoManager().set_price_info(code, '09:50:00', (54.54, 100000), (54.50, 0)) LatestLimitUpMaxBuy1MoneyInfoManager().set_price_info(code, '09:51:00', (54.54, 200000), (54.50, 0)) LatestLimitUpMaxBuy1MoneyInfoManager().set_price_info(code, '09:52:00', (54.54, 100000), (54.50, 0)) LatestLimitUpMaxBuy1MoneyInfoManager().set_price_info(code, '09:53:00', (54.52, 100000), (54.50, 2)) print(LatestLimitUpMaxBuy1MoneyInfoManager().get_latest_info(code)) LatestLimitUpMaxBuy1MoneyInfoManager().set_price_info(code, '09:54:00', (54.54, 1000), (54.50, 0)) print(LatestLimitUpMaxBuy1MoneyInfoManager().get_latest_info(code)) print(LatestLimitUpMaxBuy1MoneyInfoManager().get_latest_info_list(code))