""" 股票代码管理器 """ from code_atrribute.history_k_data_util import HistoryKDatasUtils from db import redis_manager_delegate as redis_manager from db.redis_manager_delegate import RedisUtils from utils import tool import decimal __redisManager = redis_manager.RedisManager(0) __db = 0 class CodesNameManager: __code_name_dict = {} @classmethod def set_code_name(cls, code, name): cls.__code_name_dict[code] = name @classmethod def get_code_name(cls, code): return cls.__code_name_dict.get(code) @classmethod def request_code_name(cls, code): datas = HistoryKDatasUtils.get_gp_latest_info([code]) if datas: name = datas[0]['sec_name'] cls.set_code_name(code, name) # 获取二板代码 def get_second_gp_list(): codes = RedisUtils.smembers(__redisManager.getRedis(), "gp_list") return codes class CodePrePriceManager: __price_pre_cache = {} __redisManager = redis_manager.RedisManager(0) # 获取收盘价 @classmethod def get_price_pre(cls, code): result = RedisUtils.get(cls.__redisManager.getRedis(), "price-pre-{}".format(code)) if result is not None: return float(result) return None # 获取缓存 @classmethod def get_price_pre_cache(cls, code): if code in cls.__price_pre_cache: return float(cls.__price_pre_cache[code]) val = cls.get_price_pre(code) if val: cls.__price_pre_cache[code] = val return val # 设置收盘价 @classmethod def set_price_pre(cls, code, price, force=False): RedisUtils.setex(cls.__redisManager.getRedis(), "price-pre-{}".format(code), tool.get_expire(), str(price)) cls.__price_pre_cache[code] = float(price) __limit_up_price_dict = {} # 获取涨停价 def get_limit_up_price(code): # 读取内存中的值 if code in __limit_up_price_dict: return __limit_up_price_dict[code] price = CodePrePriceManager.get_price_pre_cache(code) if price is None: return None limit_up_price = tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal(f"{tool.get_limit_up_rate(code)}")) __limit_up_price_dict[code] = limit_up_price return limit_up_price def get_price_pre_cache(code): price = CodePrePriceManager.get_price_pre_cache(code) return price def set_price_pre(code, price): CodePrePriceManager.set_price_pre(code, price, force=True) def request_price_pre(codes): k_results = HistoryKDatasUtils.get_gp_latest_info(codes, "pre_close,sec_id") for kr in k_results: set_price_pre(kr['sec_id'], round(kr['pre_close'], 2)) def get_limit_up_price_cache(code): if code in __limit_up_price_dict: return __limit_up_price_dict[code] return None # 获取跌停价 def get_limit_down_price(code): price = CodePrePriceManager.get_price_pre_cache(code) if price is None: return None return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal(f"{tool.get_limit_down_rate(code)}"))