Administrator
2023-07-10 a3efed8a184c3c4b979bea1eb1ba578152642974
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
"""
代码价格管理
"""
import json
 
from utils import tool
from db import redis_manager
from log_module.log import logger_trade_queue_price_info
 
 
class Buy1PriceManager:
    __redisManager = redis_manager.RedisManager(1)
    __latest_data = {}
    __current_buy_1_price = {}
 
    @classmethod
    def __get_redis(cls):
        return cls.__redisManager.getRedis()
 
    # 保存买1价格信息
    @classmethod
    def __save_buy1_price_info(cls, code, limit_up_time, open_limit_up_time):
        cls.__get_redis().setex(f"buy1_price_limit_up_info-{code}", tool.get_expire(),
                                json.dumps((limit_up_time, open_limit_up_time)))
 
    @classmethod
    def __get_buy1_price_info(cls, code):
        data = cls.__get_redis().get(f"buy1_price_limit_up_info-{code}")
        if not data:
            return None, None
        data = json.loads(data)
        return data[0], data[1]
 
    @classmethod
    def __save_buy1_price(cls, code, buy_1_price):
        cls.__current_buy_1_price[code] = buy_1_price
        cls.__get_redis().setex(f"buy1_price-{code}", tool.get_expire(), buy_1_price)
 
    @classmethod
    def __get_buy1_price(cls, code):
        return cls.__get_redis().get(f"buy1_price-{code}")
 
    @classmethod
    def get_buy1_price(cls, code):
        if code in cls.__current_buy_1_price:
            return cls.__current_buy_1_price.get(code)
        return cls.__get_buy1_price(code)
 
    # 处理
    @classmethod
    def process(cls, code, buy_1_price, time_str, limit_up_price, sell_1_price, sell_1_volumn):
        data_str = f"{buy_1_price},{time_str},{limit_up_price},{sell_1_price},{sell_1_volumn}"
        if cls.__latest_data.get(code) == data_str:
            return
        cls.__latest_data[code] = data_str
        # 保存买1价格
        cls.__save_buy1_price(code, buy_1_price)
 
        # 记录日志
        logger_trade_queue_price_info.info(
            f"code={code} data: time_str-{time_str}, buy_1_price-{buy_1_price},limit_up_price-{limit_up_price},sell_1_price-{sell_1_price},sell_1_volumn-{sell_1_volumn}")
        # 买1价格不能小于1块
        if float(buy_1_price) < 1.0:
            return
 
        is_limit_up = abs(float(limit_up_price) - float(buy_1_price)) < 0.01
        old_limit_up_time, old_open_limit_up_time = cls.__get_buy1_price_info(code)
        if old_limit_up_time and old_open_limit_up_time:
            return
        if is_limit_up and old_limit_up_time is None and float(sell_1_price) < 0.1 and int(sell_1_volumn) <= 0:
            # 卖1消失,买1为涨停价则表示涨停
            cls.__save_buy1_price_info(code, time_str, None)
        elif old_limit_up_time and not is_limit_up and old_open_limit_up_time is None:
            # 有涨停时间,当前没有涨停,之前没有打开涨停
            cls.__save_buy1_price_info(code, old_limit_up_time, time_str)
 
    # 是否可以下单
    @classmethod
    def is_can_buy(cls, code):
        old_limit_up_time, old_open_limit_up_time = cls.__get_buy1_price_info(code)
        if old_limit_up_time and old_open_limit_up_time:
            return True
        return False
 
    # 获取涨停信息
    # 返回涨停时间与炸板时间
    @classmethod
    def get_limit_up_info(cls, code):
        old_limit_up_time, old_open_limit_up_time = cls.__get_buy1_price_info(code)
        return old_limit_up_time, old_open_limit_up_time
 
    # 设置涨停时间
    @classmethod
    def set_limit_up_time(cls, code, time_str):
        limit_up_time, open_limit_up_time = cls.get_limit_up_info(code)
        if limit_up_time is None:
            cls.__save_buy1_price_info(code, time_str, None)
 
 
if __name__ == "__main__":
    print(Buy1PriceManager.get_limit_up_info("002777"))