Administrator
2023-08-03 500e2f3db6ce9ab2f6f06e7b3b23ce15f71db59d
code_attribute/gpcode_manager.py
@@ -129,40 +129,63 @@
# 首板代码管理
class FirstCodeManager:
    redisManager = redis_manager.RedisManager(0)
    __instance = None
    @classmethod
    def __get_redis(cls):
        return cls.redisManager.getRedis()
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(FirstCodeManager, cls).__new__(cls, *args, **kwargs)
            # 初始化设置
            # 获取交易窗口的锁
            cls.__instance.redisManager = redis_manager.RedisManager(0)
            cls.__instance.__first_code_record_cache = RedisUtils.smembers(cls.__instance.__get_redis(),
                                                                           "first_code_record")
            cls.__instance.__first_code_limited_up_record_cache = RedisUtils.smembers(cls.__instance.__get_redis(),
                                                                                      "first_code_limited_up_record")
        return cls.__instance
    def __get_redis(self):
        return self.redisManager.getRedis()
    # 加入首板历史记录
    @classmethod
    def add_record(cls, codes):
    def add_record(self, codes):
        hasChanged = False
        for code in codes:
            RedisUtils.sadd(cls.__get_redis(), "first_code_record", code)
        RedisUtils.expire(cls.__get_redis(), "first_code_record", tool.get_expire())
            if code not in self.__first_code_record_cache:
                RedisUtils.sadd(self.__get_redis(), "first_code_record", code)
                hasChanged = True
            self.__first_code_record_cache.add(code)
        if hasChanged:
            RedisUtils.expire(self.__get_redis(), "first_code_record", tool.get_expire())
    @classmethod
    def is_in_first_record(cls, code):
        if RedisUtils.sismember(cls.__get_redis(), "first_code_record", code):
    def is_in_first_record(self, code):
        if RedisUtils.sismember(self.__get_redis(), "first_code_record", code):
            return True
        else:
            return False
    def is_in_first_record_cache(self, code):
        return code in self.__first_code_record_cache
    # 加入首板涨停过代码集合
    @classmethod
    def add_limited_up_record(cls, codes):
    def add_limited_up_record(self, codes):
        hasChanged = False
        for code in codes:
            RedisUtils.sadd(cls.__get_redis(), "first_code_limited_up_record", code)
        RedisUtils.expire(cls.__get_redis(), "first_code_limited_up_record", tool.get_expire())
            if code not in  self.__first_code_limited_up_record_cache:
                RedisUtils.sadd(self.__get_redis(), "first_code_limited_up_record", code)
                hasChanged = True
            self.__first_code_limited_up_record_cache.add(code)
        if hasChanged:
            RedisUtils.expire(self.__get_redis(), "first_code_limited_up_record", tool.get_expire())
    # 是否涨停过
    @classmethod
    def is_limited_up(cls, code):
        if RedisUtils.sismember(cls.__get_redis(), "first_code_limited_up_record", code):
    def is_limited_up(self, code):
        if RedisUtils.sismember(self.__get_redis(), "first_code_limited_up_record", code):
            return True
        else:
            return False
    def is_limited_up_cache(self, code):
        return code in self.__first_code_limited_up_record_cache
# 想要买的代码
@@ -432,35 +455,38 @@
    return list
# 获取收盘价
def get_price_pre(code):
    redis_instance = __redisManager.getRedis()
    result = RedisUtils.get(redis_instance, "price-pre-{}".format(code))
    if result is not None:
        return float(result)
    return None
class CodePrePriceManager:
    __price_pre_cache = {}
    __redisManager = redis_manager.RedisManager(0)
    # 获取收盘价
    @classmethod
    def get_price_pre(cls, code):
        redis_instance = cls.__redisManager.getRedis()
        result = RedisUtils.get(redis_instance, "price-pre-{}".format(code))
        if result is not None:
            return float(result)
        return None
__price_pre_cache = {}
    # 获取缓存
    @classmethod
    def get_price_pre_cache(cls, code):
        if code in cls.__price_pre_cache:
            return cls.__price_pre_cache[code]
        val = cls.get_price_pre(code)
        if val:
            cls.__price_pre_cache[code] = val
        return val
# 获取缓存
def get_price_pre_cache(code):
    if code in __price_pre_cache:
        return __price_pre_cache[code]
    val = get_price_pre(code)
    if val:
        __price_pre_cache[code] = val
    return val
# 设置收盘价
def set_price_pre(code, price, force=False):
    codes = get_gp_list()
    if code not in codes and not FirstCodeManager.is_in_first_record(code) and not force:
        return
    redis_instance = __redisManager.getRedis()
    RedisUtils.setex(redis_instance, "price-pre-{}".format(code), tool.get_expire(), str(price))
    # 设置收盘价
    @classmethod
    def set_price_pre(cls, code, price, force=False):
        codes = get_gp_list()
        if code not in codes and not FirstCodeManager().is_in_first_record_cache(code) and not force:
            return
        redis_instance = cls.__redisManager.getRedis()
        RedisUtils.setex(redis_instance, "price-pre-{}".format(code), tool.get_expire(), str(price))
        cls.__price_pre_cache[code] = price
__limit_up_price_dict = {}
@@ -471,7 +497,7 @@
    # 读取内存中的值
    if code in __limit_up_price_dict:
        return __limit_up_price_dict[code]
    price = get_price_pre_cache(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("1.1"))
@@ -487,7 +513,7 @@
# 获取跌停价
def get_limit_down_price(code):
    price = get_price_pre_cache(code)
    price = CodePrePriceManager.get_price_pre_cache(code)
    if price is None:
        return None
    return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("0.9"))