Administrator
2023-08-03 35cf19418535fb67cd51919a744177249d052ba1
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
掘金
"""
 
from __future__ import print_function, absolute_import
 
import schedule
 
import gm.api as gmapi
 
from code_attribute import big_money_num_manager, global_data_loader, gpcode_manager, gpcode_first_screen_manager
from db.redis_manager import RedisUtils
from ths import client_manager
import constant
from utils import global_util, tool
import threading
 
import server
 
from db import redis_manager
from user import authority
import decimal
 
from third_data.history_k_data_util import HistoryKDatasUtils
from trade import l2_trade_util, trade_manager
from l2.cancel_buy_strategy import L2LimitUpSellStatisticUtil, LCancelBigNumComputer, DCancelBigNumComputer
from log_module.log import logger_juejin_tick, logger_system
from trade.trade_data_manager import CodeActualPriceProcessor
from trade.trade_queue_manager import JueJinBuy1VolumnManager
 
redisManager = redis_manager.RedisManager(0)
__jueJinBuy1VolumnManager = JueJinBuy1VolumnManager()
__actualPriceProcessor = CodeActualPriceProcessor()
 
 
# 设置账户信息
def setAccountInfo(accountId, strategyId, token):
    redis = redisManager.getRedis()
    RedisUtils.set(redis, "juejin-account-id", accountId)
    RedisUtils.set(redis, "juejin-strategy-id", strategyId)
    RedisUtils.set(redis, "juejin-token", token)
 
 
def getAccountInfo():
    redis = redisManager.getRedis()
    account_id = RedisUtils.get(redis, "juejin-account-id")
    strategy_id = RedisUtils.get(redis, "juejin-strategy-id")
    token = RedisUtils.get(redis, "juejin-token")
    return account_id, strategy_id, token
 
 
def init_data():
    # 删除所有的涨停卖数据
    L2LimitUpSellStatisticUtil.clear()
    # 重置所有的大单数据
    big_money_num_manager.reset_all()
    # 清除水下捞数据
    __actualPriceProcessor.clear_under_water_data()
    # 载入行业股票代码
    global_data_loader.load_industry()
    # 载入代码自由流通市值
    global_data_loader.load_zyltgb()
    # 载入量
    global_data_loader.load_volumn()
 
    # 9点25之前删除所有代码
    if tool.trade_time_sub(tool.get_now_time_str(), "09:25:00") <= 0 or True:
        # 删除L2监听代码
        gpcode_manager.clear_listen_codes()
        # 删除首板代码
        gpcode_manager.clear_first_codes()
        # 删除首板未筛选代码
        gpcode_first_screen_manager.clear_first_no_screen_codes()
        # 删除禁止代码
        l2_trade_util.init_forbidden_trade_codes()
        # 清空白名单
        l2_trade_util.WhiteListCodeManager.clear()
        # 清空想要买
        gpcode_manager.WantBuyCodesManager().clear()
        # 清空分数禁止代码
        trade_manager.ForbiddenBuyCodeByScoreManager().clear()
        # 清空暂停交易代码
        gpcode_manager.PauseBuyCodesManager.clear()
        # 清除L撤数据
        LCancelBigNumComputer.clear()
        # 清除D撤数据
        DCancelBigNumComputer.clear()
 
 
# 每日初始化
def everyday_init():
    # 交易時間不能做初始化
    # if not tool.is_init_time() or True:
    #     raise Exception("交易时间不能初始化")
    init_data()
    codes = gpcode_manager.get_gp_list()
    logger_system.info("每日初始化")
 
    # 今日实时涨停
    global_data_loader.add_limit_up_codes([], True)
    # 主要获取收盘价
    __get_latest_info(None)
    # 获取60天最大量与昨日量
    global_util.today_volumn.clear()
    global_util.max60_volumn.clear()
    global_util.yesterday_volumn.clear()
    # 清除大单数据
    global_util.big_money_num.clear()
    # 初始化大单数据
    for code in codes:
        big_money_num_manager.add_num(code, 0)
        big_money_num_manager.expire(code)
    # 清除涨停时间
    global_util.limit_up_time.clear()
 
    # 初始化同花顺主站
    if constant.L2_SOURCE_TYPE == constant.L2_SOURCE_TYPE_THS:
        l2_clients = client_manager.getValidL2Clients()
        for client in l2_clients:
            try:
                server.repair_ths_main_site(client)
            except Exception as e:
                pass
 
 
def __run_schedule():
    while True:
        schedule.run_pending()
 
 
def init(context):
    # gmapi.subscribe(symbols="SZSE.002529", frequency="1d", count=30)
    # 订阅浦发银行, bar频率为一天和一分钟
    # 订阅订阅多个频率的数据,可多次调用subscribe
    # 获取需要监听的股票
    init_data()
    logger_system.info("掘金初始化")
    schedule.every().day.at("09:15:00").do(everyday_init)
    t1 = threading.Thread(target=lambda: __run_schedule())
    # 后台运行
    t1.setDaemon(True)
    t1.start()
 
    # 多个时间点获取收盘价
    gmapi.schedule(schedule_func=__get_latest_info, date_rule='1d', time_rule='09:28:00')
 
    # 初始化内容
    clients = authority.get_l2_clients()
    for client in clients:
        for i in range(0, constant.L2_CODE_COUNT_PER_DEVICE):
            gpcode_manager.init_listen_code_by_pos(client, i)
 
 
def __get_latest_info(context):
    # 初始化内容
    clients = authority.get_l2_clients()
    for c in clients:
        for i in range(0, constant.L2_CODE_COUNT_PER_DEVICE):
            gpcode_manager.init_listen_code_by_pos(int(c), i)
    codes = gpcode_manager.get_gp_list()
    result = HistoryKDatasUtils.get_gp_latest_info(codes)
    for item in result:
        sec_level = item['sec_level']
        symbol = item['symbol']
        symbol = symbol.split(".")[1]
        pre_close = tool.to_price(decimal.Decimal(str(item['pre_close'])))
        if sec_level == 1:
            if symbol in codes:
                gpcode_manager.CodePrePriceManager.set_price_pre(symbol, pre_close)
        else:
            gpcode_manager.rm_gp(symbol)
 
 
# 获取最新的信息
def __get_current_info():
    data = gpcode_manager.get_gp_list()
    results = HistoryKDatasUtils.get_gp_current_info(data)
    logger_juejin_tick.debug("定时获取:{}", results)
    for result in results:
        price = result["price"]
        symbol = result['symbol']
        # 保存最新价
        symbol = symbol.split(".")[1]
 
 
# 设置收盘价
def re_set_price_pre(code):
    codes = [code]
    re_set_price_pres(codes)
 
 
def re_set_price_pres(codes, force=False):
    result = HistoryKDatasUtils.get_gp_latest_info(codes)
    for item in result:
        symbol = item['symbol']
        symbol = symbol.split(".")[1]
        pre_close = tool.to_price(decimal.Decimal(str(item['pre_close'])))
        gpcode_manager.CodePrePriceManager.set_price_pre(symbol, pre_close, force)
 
 
__prices_now = {}
 
 
# 获取近90天的最大量与最近的量
# 获取最近一次涨停/涨停下一个交易日的最大值
def get_volumns_by_code(code, count=60) -> object:
    datas = HistoryKDatasUtils.get_history_tick_n(code, count, "open,high,low,close,volume,pre_close,bob")
    # 计算
    datas.sort(key=lambda x: x["bob"], reverse=True)
    return datas
 
 
# 解析最大量
def parse_max_volume(datas, is_new_top=False):
    max_volume = 0
 
    max_volume_date = None
    if is_new_top:
        # 如果是突破前高就取最大量
        for item in datas:
            if max_volume < item["volume"]:
                max_volume = item["volume"]
                max_volume_date = item["bob"]
        return max_volume, max_volume, max_volume_date.strftime("%Y-%m-%d")
    else:
 
        date = None
        target_volume = None
        for i in range(len(datas)):
            # 查询涨停
            item = datas[i]
            volume = item["volume"]
            if max_volume < volume:
                max_volume = volume
                max_volume_date = item['bob']
            # 是否有涨停
            limit_up_price = float(gpcode_manager.get_limit_up_price_by_preprice(item["pre_close"]))
            if abs(limit_up_price - item["high"]) < 0.01:
                # 涨停
                next_volume = 0
                if i > 0:
                    next_volume = datas[i - 1]["volume"]
                date = datas[i]["bob"]
                if volume < next_volume:
                    volume = next_volume
                    date = datas[i - 1]["bob"]
                target_volume = (volume, date)
                break
        if not target_volume:
            target_volume = (max_volume, max_volume_date)
 
        # --判断近60天无涨停的最大量
        max_60_volume_info = [0, None]
        # 60天内是否有涨停
        has_60_limit_up = False
        for i in range(60):
            if i >= len(datas):
                break
            item = datas[i]
            volume = item["volume"]
            if max_60_volume_info[0] < volume:
                max_60_volume_info = [volume, item["bob"]]
            limit_up_price = float(gpcode_manager.get_limit_up_price_by_preprice(item["pre_close"]))
            if abs(limit_up_price - item["high"]) < 0.01:
                has_60_limit_up = True
                break
 
        if not has_60_limit_up and target_volume[0] > max_60_volume_info[0] * 3:
            # 60天内无涨停,且60天内最大量小于最大量的1/3,判断为地量,返回近60个交易日的最大量
            return max_60_volume_info[0], max_60_volume_info[0], max_60_volume_info[1].strftime("%Y-%m-%d")
        else:
            return target_volume[0], target_volume[0], target_volume[1].strftime("%Y-%m-%d")
 
 
if __name__ == '__main__':
    # init_data()+
    init_data()