Administrator
4 天以前 48fb7a00951f91bdc707e5dd2d196e5bccb752c3
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
"""
成交大单管理
"""
import json
 
from db.redis_manager_delegate import RedisUtils
from utils import tool
from db import redis_manager_delegate as redis_manager
from l2 import l2_data_util, l2_data_source_util
 
 
# 成交进度计算
class DealOrderNoManager:
    __db = 2
    __redisManager = redis_manager.RedisManager(2)
    __deal_orderno_cache = {}
    __last_progress = {}
    __instance = None
 
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(DealOrderNoManager, cls).__new__(cls, *args, **kwargs)
            cls.__load_datas()
        return cls.__instance
 
    @classmethod
    def __get_redis(cls):
        return cls.__redisManager.getRedis()
 
    @classmethod
    def __load_datas(cls):
        __redis = cls.__get_redis()
        try:
            keys = RedisUtils.keys(__redis, "deal_orderno-*")
            for k in keys:
                code = k.split("-")[-1]
                val = RedisUtils.smembers(__redis, k)
                if val is None:
                    val = set()
                tool.CodeDataCacheUtil.set_cache(cls.__deal_orderno_cache, code, val)
        finally:
            RedisUtils.realse(__redis)
 
    # 添加订单号
    def __add_orderno(self, code, orderno):
        RedisUtils.sadd_async(self.__db, f"deal_orderno-{code}", orderno)
        RedisUtils.expire_async(self.__db, f"deal_orderno-{code}", tool.get_expire())
 
    # 移除订单号
    def __remove_orderno(self, code, orderno):
        RedisUtils.srem_async(self.__db, f"deal_orderno-{code}", orderno)
        RedisUtils.expire_async(self.__db, f"deal_orderno-{code}", tool.get_expire())
 
    # 清除数据
    def clear(self):
        self.__deal_orderno_cache.clear()
        keys = RedisUtils.keys(self.__get_redis(), "deal_orderno-*")
        for k in keys:
            RedisUtils.delete_async(self.__db, k)
 
    def remove_orderno(self, code, orderno):
        if code in self.__deal_orderno_cache:
            if orderno in self.__deal_orderno_cache[code]:
                self.__deal_orderno_cache[code].discard(orderno)
                self.__remove_orderno(code, orderno)
 
    def add_orderno(self, code, orderno):
        if code not in self.__deal_orderno_cache:
            self.__deal_orderno_cache[code] = set()
        self.__deal_orderno_cache[code].add(orderno)
        self.__add_orderno(code, orderno)
 
    # 设置成交进度
    def get_deal_nums(self, code, orderno_map: dict):
        if code not in self.__deal_orderno_cache:
            return 0
        total_num = 0
        for orderno in self.__deal_orderno_cache[code]:
            if orderno_map:
                if str(orderno) in orderno_map:
                    data = orderno_map[str(orderno)]
                    total_num += data["val"]["num"] * data["re"]
        return total_num
 
    def get_deal_ordernos(self, code):
        return self.__deal_orderno_cache.get(code)
 
 
# 统计大单总共成交手数
def get_deal_big_money_num(code):
    val = DealOrderNoManager().get_deal_nums(code, l2_data_util.local_today_buyno_map.get(code))
    return val
 
 
# 统计大单成交笔数
def get_deal_big_money_count(code):
    ordernos = DealOrderNoManager().get_deal_ordernos(code)
    if ordernos:
        return len(ordernos)
    return 0
 
 
if __name__ == "__main__":
    pass