Administrator
2024-10-09 deb26c8b90f9d67c340b3a757740085ac8dd5743
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
现价处理器
"""
# 获取到现价
import decimal
import logging
 
from l2.huaxin import huaxin_target_codes_manager
from log_module import async_log_util
from log_module.log import logger_l2_codes_subscript
import constant
from code_attribute import gpcode_manager
from utils import tool, import_util
from ths.l2_code_operate import L2CodeOperate
from trade import trade_manager, l2_trade_util, trade_constant
from trade.trade_data_manager import CodeActualPriceProcessor
 
trade_gui = import_util.import_lib("trade.trade_gui")
 
__actualPriceProcessor = CodeActualPriceProcessor()
 
latest_add_codes = set()
 
 
def accept_prices(prices, request_id=None):
    print("总价格代码数量:", len(prices))
    now_str = tool.get_now_time_str()
    # 获取想买单
    want_codes = gpcode_manager.WantBuyCodesManager().list_code_cache()
    if True:
        _code_list = []
        _delete_list = []
        temp_prices = []
        for d in prices:
            code, price = d["code"], float(d["price"])
            temp_prices.append((code, price))
            # 获取收盘价
            pricePre = gpcode_manager.CodePrePriceManager.get_price_pre_cache(code)
            if pricePre is not None:
                # 是否是想买单
                is_want_buy = code in want_codes
                trade_state = trade_manager.CodesTradeStateManager().get_trade_state_cache(code)
                # 如果当前清单处于委托状态就不能移除
                if trade_state == trade_constant.TRADE_STATE_BUY_PLACE_ORDER or trade_state == trade_constant.TRADE_STATE_BUY_DELEGATED:
                    is_want_buy = True
                rate = round((price - pricePre) * 100 / pricePre, 2)
                if tool.is_ge_code(code):
                    # 创业板的涨幅需要打折
                    rate = rate / 2
                if rate >= 0:
                    # 暂存涨幅为正的代码
                    _code_list.append((rate, code, 1 if is_want_buy else 0))
                elif is_want_buy:
                    _code_list.append((rate, code, 1 if is_want_buy else 0))
                else:
                    # 暂存涨幅为负的代码
                    _delete_list.append((rate, code, 0))
                try:
                    __actualPriceProcessor.save_current_price(code, price,
                                                              gpcode_manager.get_limit_up_price_by_preprice(code,
                                                                                                            pricePre) == tool.to_price(
                                                                  decimal.Decimal(d["price"])))
                except Exception as e:
                    logging.exception(e)
                    logger_l2_codes_subscript.exception(e)
        gpcode_manager.set_prices(temp_prices)
        # -------------------------------处理交易位置分配---------------------------------
        # 排序
        new_code_list = sorted(_code_list, key=lambda e: (e.__getitem__(2), e.__getitem__(0)), reverse=True)
        # -------------------------------处理L2监听---------------------------------
        max_count = constant.HUAXIN_L2_MAX_CODES_COUNT
 
        _delete_list = []
        for item in new_code_list:
            if l2_trade_util.is_in_forbidden_trade_codes(
                    item[1]) or item[0] < 0:
                # 在(黑名单)/(涨幅小于)的数据
                if trade_manager.CodesTradeStateManager().get_trade_state_cache(
                        item[1]) != trade_constant.TRADE_STATE_BUY_SUCCESS:
                    # 没成交才会加入删除
                    _delete_list.append(item)
 
        for item in _delete_list:
            new_code_list.remove(item)
        # 截取前几个代码填充
        add_list = new_code_list[:max_count]
        async_log_util.info(logger_l2_codes_subscript,
                            f"({request_id})需要订阅的代码:{add_list}")
 
        # 后面的代码全部删除
        _delete_list.extend(new_code_list[max_count:])
 
        add_code_list = []
        del_code_list = []
        for d in add_list:
            add_code_list.append(d[1])
 
        for d in _delete_list:
            del_code_list.append(d[1])
 
        if constant.L2_SOURCE_TYPE == constant.L2_SOURCE_TYPE_HUAXIN:
            # 华鑫L2,获取加入代码的涨停价
 
            # 是否和上次一样
            try:
                add_code_set = set(add_code_list)
                # global latest_add_codes
                # if not latest_add_codes:
                #     latest_add_codes = set()
                # # 判断设置的代码是否相同
                # dif1 = latest_add_codes - add_code_set
                # dif2 = add_code_set - latest_add_codes
                # if dif1 or dif2:
                if True:
                    global latest_add_codes
                    async_log_util.info(logger_l2_codes_subscript,
                                        f"({request_id})预处理新增订阅代码:{add_code_set - latest_add_codes}")
                    latest_add_codes = add_code_set
                    add_datas = []
                    for d in add_code_list:
                        limit_up_price = gpcode_manager.get_limit_up_price(d)
                        limit_up_price = round(float(limit_up_price), 2)
                        min_volume = int(round(50 * 10000 / limit_up_price))
                        # 传递笼子价
                        add_datas.append(
                            # (代码, 最小量, 涨停价,影子订单价格,买量, 特殊价格)
                            (d, min_volume, limit_up_price, round(tool.get_shadow_price(limit_up_price), 2),
                             tool.get_buy_volume(limit_up_price), constant.AVAILABLE_BUY_MONEYS))
                    huaxin_target_codes_manager.HuaXinL2SubscriptCodesManager.push(add_datas, request_id)
            except Exception as e:
                logging.exception(e)
        else:
            # 后面的代码数量
            # 先删除应该删除的代码
            for code in del_code_list:
                if gpcode_manager.is_listen_old(code):
                    cid, pid = gpcode_manager.get_listen_code_pos(code)
                    # 强制移除
                    if cid and pid:
                        gpcode_manager.set_listen_code_by_pos(cid, pid, "")
                    # 判断是否在监听里面
                    L2CodeOperate.get_instance().add_operate(0, code, "现价变化")
            # 增加应该增加的代码
            for code in add_code_list:
                if not gpcode_manager.is_listen_old(code):
                    L2CodeOperate.get_instance().add_operate(1, code, "现价变化")
 
            # 获取卡位数量
            free_count = gpcode_manager.get_free_listen_pos_count()
            if free_count < 2:
                # 空闲位置不足
                listen_codes = gpcode_manager.get_listen_codes()
                for code in listen_codes:
                    if not gpcode_manager.is_in_gp_pool(code):
                        client_id, pos = gpcode_manager.get_listen_code_pos(code)
                        gpcode_manager.set_listen_code_by_pos(client_id, pos, "")
                        free_count += 1
                        if free_count > 2:
                            break
 
 
__trade_price_dict = {}
 
# 最近的非涨停价成交的信息,数据结构:{code:(价格,时间)}
__trade_price_not_limit_up_info_dict = {}
 
 
# 设置成交价
def set_trade_price(code, price):
    __trade_price_dict[code] = price
 
 
def set_latest_not_limit_up_time(code, time_str_with_ms):
    """
    记录最近的一次上板时间(最近的一笔主动买就是上板时间)
    @param code:
    @param time_str:
    @return:
    """
    __trade_price_not_limit_up_info_dict[code] = time_str_with_ms
 
 
 
# 获取成交价
def get_trade_price(code):
    return __trade_price_dict.get(code)
 
 
def get_trade_not_limit_up_time_with_ms(code):
    """
    获取最近的非板上成交的时间
    @param code:
    @return:(价格, 时间)
    """
    return __trade_price_not_limit_up_info_dict.get(code)