Administrator
2025-06-03 ea44e1fddfb7a663802ca6dcd33f2350c805ec61
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
"""
常用工具
"""
import ctypes
import decimal
import math
import random
import threading
import time
import time as t
import datetime
from threading import Thread
 
import constant
 
 
def async_call(fn):
    def wrapper(*args, **kwargs):
        Thread(target=fn, args=args, kwargs=kwargs).start()
 
    return wrapper
 
 
def singleton(cls):
    """
    单例装饰器
    @param cls:
    @return:
    """
    instances = {}
 
    def get_instance(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
 
    return get_instance
 
 
def get_expire():
    now = int(t.time())
    end = int(t.time()) + 60 * 60 * 24
    local_time = t.strftime("%Y-%m-%d", t.localtime(end))
    end = int(t.mktime(t.strptime(local_time, "%Y-%m-%d")))
    expire = end - now
    # 加随机数,防止一起销毁数据
    expire += random.randint(0, 60 * 30)
    return expire
 
 
def get_now_date():
    date = datetime.datetime.now()
    return date
 
 
def get_now_date_str(format="%Y-%m-%d"):
    date = datetime.datetime.now().strftime(format)
    return date
 
 
# 日期减去多少天
def date_sub(date_str, day, format="%Y-%m-%d"):
    t_ = time.mktime(time.strptime(date_str, format))
    t_ -= day * 24 * 60 * 60
    return time.strftime(format, t.localtime(t_))
 
 
def get_now_time_str():
    time_str = datetime.datetime.now().strftime("%H:%M:%S")
    return time_str
 
 
def get_now_time_as_int():
    time_str = datetime.datetime.now().strftime("%H:%M:%S")
    return int(time_str.replace(":", ""))
 
 
def get_now_time_with_ms_str():
    now = datetime.datetime.now()
    ms = int(now.microsecond / 1000)
    time_str = now.strftime(f"%H:%M:%S.{ms:03d}")
    return time_str
 
 
def get_now_datetime_str():
    time_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return time_str
 
 
def get_now_timestamp():
    return round(time.time() * 1000)
 
 
# 转为价格,四舍五入保留2位小数
def to_price(_decimal):
    return _decimal.quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP)
 
 
# 是否为交易时间
def is_trade_time():
    # 测试
    if constant.TEST:
        return True
 
    relative_timestamp = t.time() % (24 * 60 * 60) + 8 * 60 * 60
    start1 = 60 * 60 * 9 + 24 * 60
    end1 = 60 * 60 * 11 + 31 * 60
    start2 = 60 * 60 * 12 + 58 * 60
    end2 = 60 * 60 * 15 + 1 * 60
    if start1 < relative_timestamp < end1 or start2 < relative_timestamp < end2:
        return True
    else:
        return False
 
 
# 是否为报警时间
def is_alert_time():
    relative_timestamp = t.time() % (24 * 60 * 60) + 8 * 60 * 60
    start1 = 60 * 60 * 9 + 29 * 60
    end1 = 60 * 60 * 11 + 29 * 60
    start2 = 60 * 60 * 13
    end2 = 60 * 60 * 14 + 54 * 60
    if start1 < relative_timestamp < end1 or start2 < relative_timestamp < end2:
        return True
    else:
        return False
 
 
# 是否为修复时间
def is_repaire_time():
    relative_timestamp = t.time() % (24 * 60 * 60) + 8 * 60 * 60
    start1 = 60 * 60 * 9 + 29 * 60
    end1 = 60 * 60 * 11 + 29 * 60
    start2 = 60 * 60 * 13
    end2 = 60 * 60 * 15
    if start1 < relative_timestamp < end1 or start2 < relative_timestamp < end2:
        return True
    else:
        return False
 
 
# 是否为初始化时间
def is_init_time():
    relative_timestamp = t.time() % (24 * 60 * 60) + 8 * 60 * 60
    start1 = 60 * 60 * 9 + 30 * 60
    end1 = 60 * 60 * 15 + 1 * 60
    if start1 < relative_timestamp < end1:
        return False
    else:
        return True
 
 
def is_set_code_time():
    # 测试
    if constant.TEST:
        return True
 
    relative_timestamp = t.time() % (24 * 60 * 60) + 8 * 60 * 60
    start1 = 60 * 60 * 9 + 14 * 60
    end1 = 60 * 60 * 11 + 35 * 60
    start2 = 60 * 60 * 12 + 50 * 60
    end2 = 60 * 60 * 15 + 5 * 60
    if start1 < relative_timestamp < end1 or start2 < relative_timestamp < end2:
        return True
    else:
        return False
 
 
def run_time():
    def decorator(func):
        def infunc(*args, **kwargs):
            start = round(time.time() * 1000)
            result = func(args, **kwargs)
            print("执行时间", round(time.time() * 1000) - start)
            return result
 
        return infunc
 
    return decorator
 
 
def get_time_as_second(time_str):
    ts = time_str.split(":")
    return int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
 
 
def get_time_as_millionsecond(time_str):
    s_str, ms_str = time_str.split(".")
    ts = s_str.split(":")
    return (int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])) * 1000 + int(ms_str)
 
 
# 将秒数格式化为时间
def time_seconds_format(seconds):
    h = seconds // 3600
    m = seconds % 3600 // 60
    s = seconds % 60
    return "{0:0>2}:{1:0>2}:{2:0>2}".format(h, m, s)
 
 
def time_millionseconds_format(millionseconds):
    ms = millionseconds % 1000
    seconds = millionseconds // 1000
    h = seconds // 3600
    m = seconds % 3600 // 60
    s = seconds % 60
    return "{0:0>2}:{1:0>2}:{2:0>2}.{3:0>3}".format(h, m, s, ms)
 
 
def timestamp_format(timestamp, format):
    """
    时间戳格式化
    @param timestamp:
    @param format:
    @return:
    """
    return datetime.datetime.fromtimestamp(timestamp).strftime(format)
 
 
# 交易時間的差值
# 如11:29:59 与 13:00:00只相差1s
def trade_time_sub(time_str_1, time_str_2):
    split_time = get_time_as_second("11:30:00")
    time_1 = get_time_as_second(time_str_1)
    time_2 = get_time_as_second(time_str_2)
    if time_1 < split_time < time_2:
        time_2 = time_2 - 90 * 60
    elif time_2 < split_time < time_1:
        time_2 = time_2 + 90 * 60
 
    return time_1 - time_2
 
 
def trade_time_sub_with_ms(time_str_1, time_str_2):
    split_time = get_time_as_second("11:30:00") * 1000
    time_1 = get_time_as_millionsecond(time_str_1)
    time_2 = get_time_as_millionsecond(time_str_2)
    if time_1 < split_time < time_2:
        time_2 = time_2 - 90 * 60 * 1000
    elif time_2 < split_time < time_1:
        time_2 = time_2 + 90 * 60 * 1000
 
    return time_1 - time_2
 
 
def compare_time(time_1: str, time_2: str):
    return int(time_1.replace(":", "")) - int(time_2.replace(":", ""))
 
 
def compare_time_with_ms(time_1: str, time_2: str):
    return int(time_1.replace(":", "").replace(".", "")) - int(time_2.replace(":", "").replace(".", ""))
 
 
# 交易时间加几s
def trade_time_add_second(time_str, second):
    ts = time_str.split(":")
    s_ = int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
    s = s_ + second
    # 是否在11:30:00
    if s >= 11 * 3600 + 30 * 60 > s_:
        s += 90 * 60
    return time_seconds_format(s)
 
 
def trade_time_add_millionsecond(time_str, millionsecond):
    s_str, ms_str = time_str.split(".")
    ts = s_str.split(":")
    s_ = int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
    ms_ = s_ * 1000
    ms_ += int(ms_str)
    ms = ms_ + millionsecond
    # 是否在11:30:00
    if ms >= 11 * 3600 * 1000 + 30 * 60 * 1000 > ms_:
        ms += 90 * 60 * 1000
    return time_millionseconds_format(ms)
 
 
def compute_buy1_real_time(time_):
    ts = time_.split(":")
    s = int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
    cha = (s - 2) % 3
    return time_seconds_format(s - 2 - cha)
 
 
def to_time_with_ms(time_s_str, time_ms):
    """
    将时间+毫秒数 转为字符串
    @param time_s_str: 时间如:10:00:00
    @param time_ms: 毫秒如: 12
    @return: 如:10:00:00.001
    """
    return f"{time_s_str}." + "{0:0>3}".format(time_ms)
 
 
# 全角转半角
def strQ2B(ustring):
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 12288:  # 全角空格直接转换
            inside_code = 32
        elif 65281 <= inside_code <= 65374:  # 全角字符(除空格)根据关系转化
            inside_code -= 65248
        rstring += chr(inside_code)
    return rstring
 
 
# 将时间戳s格式化
def to_time_str(timestamp_second, format_="%H:%M:%S"):
    return datetime.datetime.fromtimestamp(timestamp_second).strftime(format_)
 
 
# 获取买入价格笼子的最低价
def get_buy_min_price(price):
    price1 = price * (1 - 0.02)
    price1 = math.ceil(price1 * 100) / 100
    price2 = price - 0.1
    return max(price1, price2)
 
 
# 获取买入价格笼子的最低价
def get_shadow_price(price):
    # fprice = round((100 - random.randint(2, 10)) * price / 100, 2)
    # if price - 0.1 < fprice:
    #     fprice = price - 0.1
    # return round(fprice, 2)
    if price < 20:
        return round(get_buy_min_price(price) - 0.03, 2)
    else:
        # 大股价直接向下取2%
        return round(price * (1 - 0.02), 2)
 
 
if __name__ == "__main__":
    print(trade_time_sub("13:00:01",
                         "11:29:55"))
 
 
class CodeDataCacheUtil:
    # --缓存操作--
    @classmethod
    def clear_cache(cls, cache_dict, code):
        if code in cache_dict:
            cache_dict.pop(code)
 
    @classmethod
    def set_cache(cls, cache_dict, code, data):
        cache_dict[code] = data
 
    @classmethod
    def get_cache(cls, cache_dict, code):
        if code in cache_dict:
            return True, cache_dict.get(code)
        return False, None
 
 
def is_can_buy_code(code):
    if code.find("00") == 0 or code.find("60") == 0 or code.find("30") == 0:
        return True
    return False
 
 
def is_target_code(code):
    """
    是否是目标代码
    @param code:
    @return:
    """
    prefixes = ["00", "60", "30"]
    for prefix in prefixes:
        if code.find(prefix) == 0:
            return True
    return False
 
 
def get_limit_up_rate(code):
    # 获取涨停倍数
    if code.find("00") == 0 or code.find("60") == 0:
        return 1.1
    else:
        return 1.2
 
 
def get_limit_down_rate(code):
    # 获取涨停倍数
    if code.find("00") == 0 or code.find("60") == 0:
        return 0.9
    else:
        return 0.8
 
 
def get_thread_id():
    try:
        if constant.is_windows():
            return threading.current_thread().ident
        else:
            thread_id = ctypes.CDLL('libc.so.6').syscall(186)  # Linux下的系统调用号
            return thread_id
    except:
        pass
    return None
 
 
def get_buy_volume(limit_up_price):
    return get_buy_volume_by_money(limit_up_price, constant.BUY_MONEY_PER_CODE)
 
 
def get_buy_volume_by_money(limit_up_price, money):
    count = (money // int(round(float(limit_up_price) * 100))) * 100
    if count < 100:
        count = 100
    return count
 
 
# 深证
MARKET_TYPE_SZSE = 1
# 上证
MARKET_TYPE_SSE = 0
# 未知
MARKET_TYPE_UNKNOWN = -1
 
 
def get_market_type(code):
    """
    根据股票代码
    :param code:
    :return:
    """
    if code.find("00") == 0 or code.find("30") == 0 or code.find("12") == 0:
        return MARKET_TYPE_SZSE
    elif code.find("60") == 0 or code.find("68") == 0 or code.find("11") == 0:
        return MARKET_TYPE_SSE
    else:
        return MARKET_TYPE_UNKNOWN
 
 
def is_sh_code(code):
    """
    是否是上证
    @param code:
    @return:
    """
    return get_market_type(code) == MARKET_TYPE_SSE
 
 
def is_sz_code(code):
    """
    是否是深证
    @param code:
    @return:
    """
    return get_market_type(code) == MARKET_TYPE_SZSE
 
 
def is_ge_code(code):
    """
    是否是创业板
    @param code:
    @return:
    """
    return code.find("30") == 0
 
 
if __name__ == "__main__":
    print(timestamp_format(1726034271, "%H%M%S"))