From 21c96ed504f93f16ce6f8a3ccf164a87c9edd9c0 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期二, 30 一月 2024 15:45:23 +0800 Subject: [PATCH] 增加调试日志 --- l2/l2_data_util.py | 108 +++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 75 insertions(+), 33 deletions(-) diff --git a/l2/l2_data_util.py b/l2/l2_data_util.py index 081a585..9abfa2b 100644 --- a/l2/l2_data_util.py +++ b/l2/l2_data_util.py @@ -13,12 +13,13 @@ import constant from code_attribute import gpcode_manager -from db.redis_manager import RedisUtils +from db.redis_manager_delegate import RedisUtils from l2 import l2_data_log, l2_data_source_util -from log_module import log -from db import redis_manager +from log_module import log, log_export, async_log_util +from db import redis_manager_delegate as redis_manager from utils import tool +__db = 1 _redisManager = redis_manager.RedisManager(1) # l2鏁版嵁绠$悊 # 鏈湴鏈�鏂颁竴娆′笂浼犵殑鏁版嵁 @@ -32,14 +33,16 @@ # 涔板叆璁㈠崟鍙锋槧灏�,鍙湁鍘熺敓鐨凩2鏁版嵁鎵嶆湁 local_today_buyno_map = {} +# 宸茬粡鎾ゅ崟鐨勮鍗曞彿 +local_today_canceled_buyno_map = {} + def load_l2_data(code, load_latest=True, force=False): - redis = _redisManager.getRedis() # 鍔犺浇鏈�杩戠殑l2鏁版嵁 - if load_latest : + if load_latest: if local_latest_datas.get(code) is None or force: # 鑾峰彇鏈�杩戠殑鏁版嵁 - _data = RedisUtils.get(redis,"l2-data-latest-{}".format(code)) + _data = RedisUtils.get(_redisManager.getRedis(), "l2-data-latest-{}".format(code)) if _data is not None: if code in local_latest_datas: local_latest_datas[code] = json.loads(_data) @@ -48,7 +51,7 @@ # 鑾峰彇浠婃棩鐨勬暟鎹� if local_today_datas.get(code) is None or force: - datas = log.load_l2_from_log() + datas = log_export.load_l2_from_log() datas = datas.get(code) if datas is None: datas = [] @@ -71,6 +74,7 @@ # 鏍规嵁浠婃棩鏁版嵁鍔犺浇 load_num_operate_map(local_today_num_operate_map, code, local_today_datas.get(code), force) load_buy_no_map(local_today_buyno_map, code, local_today_datas.get(code), force) + load_canceled_buy_no_map(local_today_canceled_buyno_map, code, local_today_datas.get(code), force) return data_normal return True @@ -108,6 +112,26 @@ local_today_buyno_map[code].setdefault(key, data) +# 灏嗘暟鎹牴鎹畂rderNo鍒嗙被宸叉挙璁㈠崟,鍘熺敓鏁版嵁鎵嶆湁 +def load_canceled_buy_no_map(local_today_canceled_buyno_map, code, source_datas, clear=False): + # 鍙湁鍘熺敓L2鏁版嵁鎵嶄細鏈夋鎿嶄綔 + if constant.L2_SOURCE_TYPE != constant.L2_SOURCE_TYPE_HUAXIN: + return + if local_today_canceled_buyno_map.get(code) is None: + local_today_canceled_buyno_map[code] = {} + if clear: + local_today_canceled_buyno_map[code] = {} + + for data in source_datas: + # 鍙暀涓嬩拱鎾� + if data["val"]["operateType"] != 1: + continue + # 鍙~鍏呬拱鍏ユ暟鎹� + key = "{}".format(data["val"]["orderNo"]) + if local_today_canceled_buyno_map[code].get(key) is None: + local_today_canceled_buyno_map[code].setdefault(key, data) + + @tool.async_call def saveL2Data(code, datas, msg=""): start_time = round(time.time() * 1000) @@ -118,29 +142,29 @@ redis_instance = _redisManager.getRedis() try: - if redis_instance.setnx("l2-save-{}".format(code), "1") > 0: - + if RedisUtils.setnx(redis_instance, "l2-save-{}".format(code), "1", auto_free=False) > 0: # 璁$畻淇濈暀鐨勬椂闂� expire = tool.get_expire() i = 0 for _data in datas: i += 1 key = "l2-" + _data["key"] - value = RedisUtils.get(redis_instance, key) + value = RedisUtils.get(redis_instance, key, auto_free=False) if value is None: # 鏂板 try: value = {"index": _data["index"], "re": _data["re"]} - RedisUtils.setex(redis_instance,key, expire, json.dumps(value)) + RedisUtils.setex(redis_instance, key, expire, json.dumps(value), auto_free=False) except: logging.error("鏇存L2鏁版嵁鍑洪敊锛歿} key:{}".format(code, key)) else: json_value = json.loads(value) if json_value["re"] != _data["re"]: json_value["re"] = _data["re"] - RedisUtils.setex(redis_instance,key, expire, json.dumps(json_value)) + RedisUtils.setex(redis_instance, key, expire, json.dumps(json_value), auto_free=False) finally: - RedisUtils.delete(redis_instance, "l2-save-{}".format(code)) + RedisUtils.delete(redis_instance, "l2-save-{}".format(code), auto_free=False) + RedisUtils.realse(redis_instance) print("淇濆瓨鏂版暟鎹敤鏃讹細", msg, "鑰楁椂锛歿}".format(round(time.time() * 1000) - start_time)) return datas @@ -148,30 +172,27 @@ # 淇濆瓨l2鏁版嵁 def save_l2_data(code, datas, add_datas): - redis = _redisManager.getRedis() # 鍙湁鏈夋柊鏇炬暟鎹墠闇�瑕佷繚瀛� - if len(add_datas) > 0: + if add_datas: # 淇濆瓨鏈�杩戠殑鏁版嵁 __start_time = round(time.time() * 1000) if datas: - RedisUtils.setex(redis,"l2-data-latest-{}".format(code), tool.get_expire(), json.dumps(datas)) - l2_data_log.l2_time(code, round(time.time() * 1000) - __start_time, "淇濆瓨鏈�杩憀2鏁版嵁鐢ㄦ椂") + RedisUtils.setex_async(__db, "l2-data-latest-{}".format(code), tool.get_expire(), + json.dumps(datas)) + # l2_data_log.l2_time(code, round(time.time() * 1000) - __start_time, "淇濆瓨鏈�杩憀2鏁版嵁鐢ㄦ椂") # 璁剧疆杩涘唴瀛� local_latest_datas[code] = datas set_l2_data_latest_count(code, len(datas)) try: - log.logger_l2_data.info("{}-{}", code, add_datas) + async_log_util.l2_data_log.info(log.logger_l2_data, f"{code}-{add_datas}") except Exception as e: logging.exception(e) - # 鏆傛椂涓嶅皢鏁版嵁淇濆瓨鍒皉edis - # saveL2Data(code, add_datas) # 璁剧疆鏈�鏂扮殑l2鏁版嵁閲囬泦鐨勬暟閲� def set_l2_data_latest_count(code, count): - redis = _redisManager.getRedis() key = "latest-l2-count-{}".format(code) - RedisUtils.setex( redis,key, 2, count) + RedisUtils.setex(_redisManager.getRedis(), key, 2, count) pass @@ -179,10 +200,9 @@ def get_l2_data_latest_count(code): if code is None or len(code) < 1: return 0 - redis = _redisManager.getRedis() key = "latest-l2-count-{}".format(code) - result = RedisUtils.get(redis, key) + result = RedisUtils.get(_redisManager.getRedis(), key) if result is None: return 0 else: @@ -224,7 +244,7 @@ price = float(val["price"]) money = price * int(val["num"]) if price > 3.0: - if money >= 30000: + if money >= 29900: return True else: return False @@ -351,7 +371,7 @@ # 鏁版嵁閲嶅娆℃暟榛樿涓�1 datas.append({"key": key, "val": item, "re": 1}) dataIndexs.setdefault(key, len(datas) - 1) - # TODO 娴嬭瘯鐨勬椂鍊欏紑鍚紝鏂逛究璁板綍澶у崟鏁版嵁 + # 娴嬭瘯鐨勬椂鍊欏紑鍚紝鏂逛究璁板綍澶у崟鏁版嵁 # l2_data_util.save_big_data(code, same_time_num, data) return datas @@ -388,11 +408,16 @@ if int(val["operateType"]) != 2: return False + return True - price = float(val["price"]) - num = int(val["num"]) - # if price * num * 100 < 50 * 10000: - # return False + # 娑ㄥ仠鍗栨挙 + @classmethod + def is_limit_up_price_sell_cancel(cls, val): + if int(val["limitPrice"]) != 1: + return False + + if int(val["operateType"]) != 3: + return False return True # 鏄惁娑ㄥ仠涔版挙 @@ -424,6 +449,22 @@ return True return False + # 鏄惁涓轰拱 + @classmethod + def is_buy(cls, val): + if int(val["operateType"]) == 0: + return True + return False + + # l2鏃堕棿宸�� + @classmethod + def time_sub_as_ms(cls, val1, val2): + # 璁$畻鏃堕棿宸�� + sub_s = tool.trade_time_sub(val1["time"], val2["time"]) + sub_ms = int(val1["tms"]) - int(val2["tms"]) + fs = sub_s * 1000 + sub_ms + return fs + class L2TradeQueueUtils(object): # 涔板叆鏁版嵁鏄惁宸叉挙 @@ -435,8 +476,9 @@ # 鏄惁鏈変拱鎾ゆ暟鎹� if cancel_datas: for cancel_data in cancel_datas: - buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, cancel_data, - local_today_num_operate_map) + buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(cancel_data, + local_today_buyno_map.get( + code)) if buy_index == data["index"]: return True return False @@ -537,4 +579,4 @@ if __name__ == "__main__": - print(load_l2_data("002235")) + print(L2DataUtil.time_sub_as_ms({"time": "13:00:00", "time_ms": "980"}, {"time": "11:29:59", "time_ms": "590"})) -- Gitblit v1.8.0