Administrator
2025-03-12 b678592566d1477db1ba004ed0befc98ef57ab71
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
涨停卖数据管理
"""
from code_attribute import gpcode_manager
from l2 import l2_log
from l2.l2_data_util import L2DataUtil
from log_module import async_log_util, log_export
from log_module.log import hx_logger_l2_sell_delegate, hx_logger_l2_sell_deal
from utils import tool
 
 
class L2LimitUpSellDataManager:
    """
    涨停卖数据管理:
    用来扫代码使用,当涨停卖有成交的时候就代表在吃最后一档
    此时统计涨停卖的金额作为快照
    当板上成交额大于涨停卖数据一定的比值就下单
    """
    # 卖单集合
    __order_no_set_dict = {}
 
    # 订单号与数据的对照表:{code:{orderno:[order_no,price,volume]}}
    __order_no_data_map_dict = {}
    # 挂起的卖手数
    __delegating_sell_num_dict = {}
 
    @classmethod
    def add_l2_data(cls, code, add_datas):
        """
        添加L2委托原始数据
        @param code: 代码
        @param add_datas: 新增的格式化数据
        @return:
        """
        try:
            if code not in cls.__order_no_set_dict:
                cls.__order_no_set_dict[code] = set()
            if code not in cls.__order_no_data_map_dict:
                cls.__order_no_data_map_dict[code] = {}
            if code not in cls.__delegating_sell_num_dict:
                cls.__delegating_sell_num_dict[code] = 0
 
            limit_up_price = gpcode_manager.get_limit_up_price_as_num(code)
            for d in add_datas:
                val = d["val"]
                if L2DataUtil.is_limit_up_price_sell(val):
                    # 卖
                    order_no, price, volume = val["orderNo"], val["price"], val["num"] * 100
                    cls.__order_no_data_map_dict[code][order_no] = (order_no, price, volume)
                    cls.__order_no_set_dict[code].add(order_no)
                    cls.__delegating_sell_num_dict[code] += volume
                    async_log_util.l2_data_log.info(hx_logger_l2_sell_delegate,
                                                    f"{code}-卖-{(order_no, price, volume)}-{cls.__delegating_sell_num_dict[code]}")
                elif L2DataUtil.is_limit_up_price_sell_cancel(val):
                    # 卖撤
                    order_no, price, volume = val["orderNo"], val["price"], val["num"] * 100
                    cls.__order_no_set_dict[code].discard(order_no)
                    cls.__delegating_sell_num_dict[code] -= volume
                    async_log_util.l2_data_log.info(hx_logger_l2_sell_delegate,
                                                    f"{code}-卖撤-{(order_no, price, volume)}-{cls.__delegating_sell_num_dict[code]}")
        except:
            pass
 
    @classmethod
    def get_delegating_sell_num(cls, code):
        """
        获取处于委托状态的卖单总手数
        @param code:
        @return:
        """
        return cls.__delegating_sell_num_dict.get(code)
 
    @classmethod
    def set_deal_datas(cls, code, datas):
        """
        设置成交的卖单
        @param code:
        @param datas:  q.append((data['SecurityID'], data['TradePrice'], data['TradeVolume'],data['OrderTime'], data['MainSeq'], data['SubSeq'], data['BuyNo'],data['SellNo'], data['ExecType']))
        @return: 是否触发计算
        """
        try:
            limit_up_price = gpcode_manager.get_limit_up_price_as_num(code)
            order_no_set = cls.__order_no_set_dict.get(code)
            if order_no_set is None:
                order_no_set = set()
            limit_up_active_buy_datas = []
            for d in datas:
                # 是否有涨停主动买成交
                if d[6] < d[7]:
                    continue
                if abs(d[1] - limit_up_price) > 0.001:
                    continue
                limit_up_active_buy_datas.append(d)
            total_deal_volume = 0
            if code in cls.__delegating_sell_num_dict:
                for d in datas:
                    # 减去
                    if d[7] in order_no_set:
                        total_deal_volume += d[2]
                cls.__delegating_sell_num_dict[code] -= total_deal_volume
 
            if len(limit_up_active_buy_datas):
                l2_log.info(code, hx_logger_l2_sell_deal,
                            f"涨停主动买成交:{limit_up_active_buy_datas}")
                # 打印日志
                l2_log.info(code, hx_logger_l2_sell_deal,
                            f"有涨停主动卖:{code}-{datas[-1][3]}-{cls.__delegating_sell_num_dict.get(code)}, 成交量-{total_deal_volume}")
        except:
            pass
 
    @classmethod
    def clear_data(cls, code):
        """
        清除数据:当出现主动卖的时候就可以清除数据
        @param code:
        @return:
        """
        if cls.__order_no_set_dict.get(code):
            cls.__order_no_set_dict[code].clear()
 
        if cls.__order_no_data_map_dict.get(code):
            cls.__order_no_data_map_dict[code].clear()
 
        if cls.__delegating_sell_num_dict.get(code):
            cls.__delegating_sell_num_dict[code] = 0
            async_log_util.l2_data_log.info(hx_logger_l2_sell_delegate,
                                            f"出现主动卖清除数据:{code}")
 
 
if __name__ == "__main__":
    # 测试涨停总卖吃掉的情况
    code = "002681"
    datas = log_export.load_huaxin_l2_sell_deal_list(code).get(code)
    print(datas)
    deal_datas = []
    deal_order_nos = set()
    for i in range(len(datas)):
        if 100100 <= int(datas[i][0].replace(":", "")) <= 100230:
            deal_datas.append(datas[i])
    for x in deal_datas:
        for xx in x[1]:
            deal_order_nos.add(xx[7])
 
    total_deal_volume = sum([sum([xx[2] for xx in x[1]]) for x in deal_datas])
    print("涨停卖成交量", total_deal_volume)
    datas = log_export.load_huaxin_l2_sell_delegate(code).get(code)
    fdatas = []
    for x in datas:
        # if int("101821") < int(x[0].replace(":", "")) < int("102841"):
        if 14578165 >= x[2][0] >= 14565438:
            fdatas.append(x)
    delegate_order_nos = set()
    total_delegate = 0
    for x in fdatas:
        if x[1] == '卖':
            total_delegate += x[2][2]
            delegate_order_nos.add(x[2][0])
        elif x[1] == '卖撤':
            total_delegate -= x[2][2]
            delegate_order_nos.discard(x[2][0])
    print(fdatas)
    for d in fdatas:
        print(d)
    print("总成交", total_deal_volume, total_deal_volume * 17.28)
    print("总委托", total_delegate, total_delegate * 17.28)
    print("委托剩余", total_delegate - total_deal_volume, (total_delegate - total_deal_volume) * 17.28)
 
    not_deal_order_nos = delegate_order_nos - deal_order_nos
    for x in not_deal_order_nos:
        print("未成交:", x)