Administrator
2024-03-29 a3d453b329d234d569a85f9e81bcccddcd16b9a4
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
"""
股票代码管理器
"""
import json
 
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("1.1"))
    __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_up_price_by_preprice(price):
    if price is None:
        return None
    return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("1.1"))
 
 
def get_limit_down_price_by_preprice(price):
    if price is None:
        return None
    return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("0.9"))
 
 
# 获取跌停价
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("0.9"))