Administrator
22 小时以前 eb6e8940bb0e014fc0799f5904f52fb5c6f84f20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 最近涨停最大买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))