From 21d753614ea7bbe936b8560cbf466c4e438821b2 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期五, 23 九月 2022 16:08:16 +0800 Subject: [PATCH] l2数据计算优化 --- l2_data_manager.py | 213 +++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 151 insertions(+), 62 deletions(-) diff --git a/l2_data_manager.py b/l2_data_manager.py index f04ee18..25093bc 100644 --- a/l2_data_manager.py +++ b/l2_data_manager.py @@ -18,8 +18,9 @@ import redis_manager import tool import trade_manager -from log import logger_l2_trade, logger_l2_trade_cancel +from log import logger_l2_trade, logger_l2_trade_cancel, logger_l2_trade_buy from trade_data_manager import TradeBuyDataManager +import limit_up_time_manager _redisManager = redis_manager.RedisManager(1) # l2鏁版嵁绠$悊 @@ -94,38 +95,52 @@ # 鑾峰彇鎾や拱鍏ュ紑濮嬭绠楃殑淇℃伅 # 杩斿洖鏁版嵁鐨勫唴瀹逛负锛氭挙閿�鐐圭储寮� 鎾や拱绾拱棰� 璁$畻鐨勬暟鎹储寮� @staticmethod - def get_buy_cancel_compute_start_data(code): + def get_buy_cancel_single_pos(code): redis = TradePointManager.__get_redis() - info = redis.get("buy_cancel_compute_info-{}".format(code)) + info = redis.get("buy_cancel_single_pos-{}".format(code)) if info is None: - return None, None, None + return None else: - info = json.loads(info) - return info[0], info[1], info[2] + return int(info) # 璁剧疆涔版挙鐐逛俊鎭� # buy_num 绾拱棰� computed_index璁$畻鍒扮殑涓嬫爣 index鎾や拱淇″彿璧风偣 @classmethod - def set_buy_cancel_compute_start_data(cls, code, buy_num, computed_index, index): + def set_buy_cancel_single_pos(cls, code, index): redis = TradePointManager.__get_redis() expire = tool.get_expire() - redis.setex("buy_cancel_compute_info-{}".format(code), expire, json.dumps((index, buy_num, computed_index))) - - # 澧炲姞鎾や拱鐨勭函涔伴 - @classmethod - def add_buy_nums_for_cancel(cls, code, num_add, computed_index): - cancel_index, nums, c_index = cls.get_buy_cancel_compute_start_data(code) - if cancel_index is None: - raise Exception("鏃犳挙涔颁俊鍙疯褰�") - nums += num_add - cls.set_buy_cancel_compute_start_data(code, nums, computed_index) + redis.setex("buy_cancel_single_pos-{}".format(code), expire, index) # 鍒犻櫎涔版挙鐐规暟鎹� - @staticmethod - def delete_buy_cancel_point(code): + @classmethod + def delete_buy_cancel_point(cls, code): redis = TradePointManager.__get_redis() - redis.delete("buy_cancel_compute_info-{}".format(code)) + redis.delete("buy_cancel_single_pos-{}".format(code)) + + # 璁剧疆涔版挙绾拱棰� + @classmethod + def set_compute_info_for_cancel_buy(cls, code, index, nums): + redis = TradePointManager.__get_redis() + expire = tool.get_expire() + redis.setex("compute_info_for_cancel_buy-{}".format(code), expire, json.dumps((index, nums))) + logger_l2_trade_buy.info("{}淇濆瓨鎾ゅ崟绾拱棰濅俊鎭細{}锛寋}", code, index, nums) + + # 鑾峰彇涔版挙绾拱棰濊绠椾俊鎭� + @classmethod + def get_compute_info_for_cancel_buy(cls, code): + redis = TradePointManager.__get_redis() + info = redis.get("compute_info_for_cancel_buy-{}".format(code)) + if info is None: + return None, 0 + else: + info = json.loads(info) + return info[0], info[1] + + @classmethod + def delete_compute_info_for_cancel_buy(cls, code): + redis = TradePointManager.__get_redis() + redis.delete("compute_info_for_cancel_buy-{}".format(code)) def load_l2_data(code, force=False): @@ -398,6 +413,11 @@ ("thread-id={} code={} ".format(cls.random_key[code], code) + content).format(*args)) @classmethod + def buy_debug(cls, code, content, *args): + logger_l2_trade_buy.debug( + ("thread-id={} code={} ".format(cls.random_key[code], code) + content).format(*args)) + + @classmethod # 鏁版嵁澶勭悊鍏ュ彛 # datas: 鏈鎴浘鏁版嵁 # capture_timestamp:鎴浘鏃堕棿鎴� @@ -474,38 +494,45 @@ if start_index < 0: start_index = 0 # 鑾峰彇涔嬪墠鏄惁鏈夎褰曠殑鎾や拱淇″彿 - cancel_index, buy_num_for_cancel, computed_index = cls.__has_order_cancel_begin_pos(code) + cancel_index = TradePointManager.get_buy_cancel_single_pos(code) + + cancel_computed_index, cancel_buy_num = TradePointManager.get_compute_info_for_cancel_buy(code) + if cancel_computed_index is None: + logger_l2_trade.error("{} 鏈幏鍙栧埌涔版挙绾拱棰�,璧峰璁$畻浣嶏細{}", code, start_index) + buy_single_index, buy_exec_index, buy_compute_index, buy_num = cls.__get_order_begin_pos(code) if cancel_index is None: # 鏃犳挙鍗曚俊鍙疯捣濮嬬偣璁板綍 cancel_index = cls.__compute_order_cancel_begin_single(code, max(start_index - 3, 0), 3) - buy_num_for_cancel = buy_num - computed_index = buy_single_index if cancel_index is not None: - cls.debug(code, "鎵惧埌鎾ゅ崟淇″彿锛屾暟鎹鐞嗚捣濮嬬偣锛歿} 鏁版嵁锛歿}", start_index, local_today_datas[code][start_index]) - if cancel_index is not None: + cls.debug(code, "鎵惧埌鎾ゅ崟淇″彿-{}锛屼拱鍏ヤ俊鍙蜂负 锛屾暟鎹鐞嗚捣濮嬬偣锛歿}", cancel_index, start_index) + # 淇濆瓨鎾ゅ崟淇″彿 + TradePointManager.set_buy_cancel_single_pos(code, cancel_index) # 鑾峰彇闃堝�� 鏈変拱鎾や俊鍙凤紝缁熻鎾や拱绾拱棰� - threshold_money = cls.__get_threshmoney(code) - cls.__start_compute_cancel(code, cancel_index, max(computed_index, buy_exec_index + 1), buy_num_for_cancel, - threshold_money, - capture_time) - else: - # 鏃犱拱鎾や俊鍙�,鏄惁鏈夎櫄鎷熶笅鍗� - unreal_buy_info = cls.unreal_buy_dict.get(code) - if unreal_buy_info is not None: - cls.debug(code, "鏈夎櫄鎷熶笅鍗曪紝鏃犱拱鎾や俊鍙凤紝寮�濮嬫墽琛屼拱鍏�") - # unreal_buy_info 鐨勫唴瀹规牸寮忎负锛�(瑙︽硶涔版搷浣滀笅鏍�,鎴浘鏃堕棿) - # 鐪熷疄涓嬪崟 - cls.__buy(code, unreal_buy_info[1], local_today_datas[code][unreal_buy_info[0]], - unreal_buy_info[0]) - pass + threshold_money = cls.__get_threshmoney(code) + cls.__start_compute_cancel(code, cancel_index, cancel_computed_index + 1, + cancel_buy_num, + threshold_money, + capture_time) + + # 鏄惁鏈夎櫄鎷熶笅鍗� + unreal_buy_info = cls.unreal_buy_dict.get(code) + if unreal_buy_info is not None: + cls.debug(code, "鏈夎櫄鎷熶笅鍗曪紝鏃犱拱鎾や俊鍙凤紝寮�濮嬫墽琛屼拱鍏�,鎴浘鏃堕棿锛歿}", capture_time) + # unreal_buy_info 鐨勫唴瀹规牸寮忎负锛�(瑙︽硶涔版搷浣滀笅鏍�,鎴浘鏃堕棿) + # 鐪熷疄涓嬪崟 + cls.__buy(code, unreal_buy_info[1], local_today_datas[code][unreal_buy_info[0]], + unreal_buy_info[0]) # 寮�濮嬭绠楁挙鐨勪俊鍙� @classmethod def __start_compute_cancel(cls, code, cancel_index, compute_start_index, origin_num, threshold_money, capture_time): # sure_type 0-铏氭嫙鎸備拱浣� 1-鐪熷疄鎸備拱浣� + cancel_single = cancel_index is not None computed_index, buy_num_for_cancel, sure_type = cls.__sum_buy_num_for_cancel_order(code, compute_start_index, - origin_num, threshold_money) + origin_num, threshold_money, + cancel_single) + total_datas = local_today_datas[code] if computed_index is not None: cls.debug(code, "鑾峰彇鍒版挙鍗曟墽琛屼俊鍙凤紝淇″彿浣嶇疆锛歿}锛宮2:{} 鏁版嵁锛歿}", computed_index, threshold_money, @@ -528,10 +555,10 @@ cls.__start_compute_buy(code, computed_index + 1, threshold_money, capture_time) pass else: - cls.debug(code, "鏈幏鍙栧埌鎾ゅ崟鎵ц淇″彿锛岃绠楀紑濮嬩綅缃細{}锛� 绾拱棰濓細{}", compute_start_index, buy_num_for_cancel) - # 鏃犻渶鎾や拱锛岃褰曟挙涔颁俊鍙� - TradePointManager.set_buy_cancel_compute_start_data(code, buy_num_for_cancel, len(total_datas) - 1, - cancel_index) + cls.debug(code, "鎾や拱绾拱棰濊绠楋紝璁$畻浣嶇疆锛歿}-{}锛岀洰鍓嶄负姝㈢函涔版墜鏁帮細{}", compute_start_index, total_datas[-1]["index"], + buy_num_for_cancel) + # 鏃犻渶鎾や拱锛岃缃绠椾俊鎭� + TradePointManager.set_compute_info_for_cancel_buy(code, int(total_datas[-1]["index"]), buy_num_for_cancel) # 鍒ゆ柇鏄惁鏈夎櫄鎷熶笅鍗� unreal_buy_info = cls.unreal_buy_dict.get(code) if unreal_buy_info is not None: @@ -547,6 +574,9 @@ @classmethod def __buy(cls, code, capture_timestamp, last_data, last_data_index): + # 鍒犻櫎铏氭嫙涓嬪崟 + if code in cls.unreal_buy_dict: + cls.unreal_buy_dict.pop(code) cls.debug(code, "寮�濮嬫墽琛屼拱鍏�") try: trade_manager.start_buy(code, capture_timestamp, last_data, @@ -565,6 +595,7 @@ # 鍙栨秷涔板叆鏍囪瘑 TradePointManager.delete_buy_point(code) TradePointManager.delete_buy_cancel_point(code) + TradePointManager.delete_compute_info_for_cancel_buy(code) cls.debug(code, "鎵ц鎾ゅ崟鎴愬姛") except Exception as e: cls.debug(code, "鎵ц鎾ゅ崟寮傚父锛歿}", str(e)) @@ -585,22 +616,29 @@ num = 0 new_get_pos = True cls.debug(code, "鑾峰彇鍒颁拱鍏ヤ俊鍙疯捣濮嬬偣锛歿} 鏁版嵁锛歿}", buy_single_index, total_datas[buy_single_index]) + limit_up_time_manager.save_limit_up_time(code, total_datas[buy_single_index]["val"]["time"]) if buy_single_index is None: # 鏈幏鍙栧埌涔板叆淇″彿锛岀粓姝㈢▼搴� return None # 涔板叆绾拱棰濈粺璁� - compute_index, buy_nums = cls.__sum_buy_num_for_order(code, max(buy_single_index, compute_start_index), num, - threshold_money) + compute_index, buy_nums = cls.__sum_buy_num_for_order_2(code, max(buy_single_index, compute_start_index), num,threshold_money,buy_single_index) + + #cls.__sum_buy_num_for_order(code, max(buy_single_index, compute_start_index), num,threshold_money) if compute_index is not None: - cls.debug(code, "鑾峰彇鍒颁拱鍏ユ墽琛屼綅缃細{} m鍊硷細{} 鏁版嵁锛歿}", compute_index, threshold_money, total_datas[compute_index]) + cls.debug(code, "鑾峰彇鍒颁拱鍏ユ墽琛屼綅缃細{} m鍊硷細{} 绾拱鎵嬫暟锛歿} 鏁版嵁锛歿}", compute_index, threshold_money, buy_nums, + total_datas[compute_index]) # 璁板綍涔板叆淇″彿浣嶇疆 cls.__save_order_begin_data(code, buy_single_index, compute_index, compute_index, buy_nums) # 铏氭嫙涓嬪崟 cls.unreal_buy_dict[code] = (compute_index, capture_time) # 鍒犻櫎涔嬪墠鐨勬墍鏈夋挙鍗曚俊鍙� TradePointManager.delete_buy_cancel_point(code) + TradePointManager.delete_compute_info_for_cancel_buy(code) + # 涓轰拱鎾や繚瀛樺熀纭�绾拱棰� + TradePointManager.set_compute_info_for_cancel_buy(code, compute_index, buy_nums) + # 鏁版嵁鏄惁澶勭悊瀹屾瘯 if L2DataUtil.is_index_end(code, compute_index): cls.debug(code, "鏁版嵁澶勭悊瀹屾瘯锛屼笅鍗�, 鏁版嵁鎴浘鏃堕棿-{}", capture_time) @@ -626,15 +664,6 @@ @classmethod def __save_order_begin_data(self, code, buy_single_index, buy_exec_index, compute_index, num): TradePointManager.set_buy_compute_start_data(code, buy_single_index, buy_exec_index, compute_index, num) - - # 鑾峰彇鎾ゅ崟璧峰浣嶇疆 - @classmethod - def __has_order_cancel_begin_pos(cls, code): - # cancel_index:鎾ゅ崟淇″彿璧风偣 - # buy_num_for_cancel锛氫粠鎸傚叆鐐硅绠楃殑绾拱棰� - # computed_index 璁$畻鐨勬渶鍚庝綅缃� - cancel_index, buy_num_for_cancel, computed_index = TradePointManager.get_buy_cancel_compute_start_data(code) - return cancel_index, buy_num_for_cancel, computed_index # 璁$畻涓嬪崟璧峰淇″彿 # compute_data_count 鐢ㄤ簬璁$畻鐨刲2鏁版嵁鏁伴噺 @@ -776,11 +805,66 @@ # 娑ㄥ仠涔� buy_nums += int(_val["num"]) * int(total_datas[i]["re"]) if buy_nums >= threshold_num: + logger_l2_trade_buy.info("{}鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛歿} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿}", code, i, buy_nums, threshold_num) return i, buy_nums elif L2DataUtil.is_limit_up_price_buy_cancel(_val): # 娑ㄥ仠涔版挙 buy_nums -= int(_val["num"]) * int(total_datas[i]["re"]) + logger_l2_trade_buy.info("{}灏氭湭鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛岃捣濮嬭绠椾綅缃細{} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿}", code, compute_start_index, buy_nums, + threshold_num) return None, buy_nums + + # 缁熻涔板叆鍑�涔伴噺锛屼笉璁$畻鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓嶇殑涔版挙鍗� + @classmethod + def __sum_buy_num_for_order_2(cls, code, compute_start_index, origin_num, threshold_money, buy_single_index): + total_datas = local_today_datas[code] + buy_nums = origin_num + limit_up_price = gpcode_manager.get_limit_up_price(code) + if limit_up_price is None: + raise Exception("娑ㄥ仠浠锋棤娉曡幏鍙�") + threshold_num = threshold_money / (limit_up_price * 100) + property_buy_num_count = 0 + same_time_property = cls.__get_same_time_property(code) + for i in range(compute_start_index, len(total_datas)): + data = total_datas[i] + _val = total_datas[i]["val"] + # 鏈夎繛缁�4涓定鍋滀拱灏辨爣璁拌绠楄捣濮嬬偣 + if L2DataUtil.is_limit_up_price_buy(_val): + # 娑ㄥ仠涔� + buy_nums += int(_val["num"]) * int(total_datas[i]["re"]) + if buy_nums >= threshold_num: + logger_l2_trade_buy.info("{}鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛歿} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿}", code, i, buy_nums, threshold_num) + elif L2DataUtil.is_limit_up_price_buy_cancel(_val): + # 娑ㄥ仠涔版挙 + # 鍒ゆ柇涔板叆浣嶇疆鏄惁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓� + buy_index, buy_data = l2_data_util.get_buy_data_with_cancel_data(total_datas[i], + local_today_num_operate_map.get(code)) + if buy_index is not None: + # 鎵惧埌涔版挙鏁版嵁鐨勪拱鍏ョ偣 + if buy_index >= buy_single_index: + buy_nums -= int(_val["num"]) * int(data["re"]) + cls.buy_debug(code, "{}鏁版嵁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍚� 鎾や拱绾拱鎵嬫暟锛歿} 鐩爣鎵嬫暟锛歿}", i, buy_nums, threshold_num) + else: + cls.buy_debug(code, "{}鏁版嵁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓嶏紝涔板叆浣嶏細{}", i, buy_index) + if total_datas[buy_single_index]["val"]["time"] == buy_data["val"]["time"]: + # 鍚屼竴绉�,鑰屼笖杩樺湪棰勪及涔板叆浣嶄箣鍚庢寜姒傜巼璁$畻 + property_buy_num_count -= int(_val["num"]) * int(data["re"]) + cls.buy_debug(code, "{}鏁版嵁涔板叆浣嶄笌棰勪及涔板叆浣嶅湪鍚屼竴绉�", i) + else: + # 鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐� + cls.cancel_debug(code, "鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�: 浣嶇疆-{} 鏁版嵁-{}", i, data) + buy_nums -= int(_val["num"]) * int(total_datas[i]["re"]) + property_buy_num = round(property_buy_num_count * same_time_property) + cls.buy_debug(code, "涔板叆淇″彿鐐逛箣鍓嶅悓涓�绉掍拱鍏ユ墜鏁�-{}锛屼綅缃�-{}锛屾�绘墜鏁帮細{}锛岀洰鏍囨墜鏁帮細{}", property_buy_num, i, + buy_nums + property_buy_num, threshold_num) + # 鏈夋挙鍗曚俊鍙凤紝涓斿皬浜庨槇鍊� + if buy_nums + property_buy_num >= threshold_num: + return i, buy_nums + property_buy_num + + cls.buy_debug(code, "灏氭湭鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛岃捣濮嬭绠椾綅缃細{} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿}", compute_start_index, + buy_nums + property_buy_num, + threshold_num) + return None, buy_nums + property_buy_num # 鍚屼竴鏃堕棿涔板叆鐨勬鐜囪绠� @classmethod @@ -795,7 +879,7 @@ # 缁熻涔版挙鍑�涔伴噺 @classmethod - def __sum_buy_num_for_cancel_order(cls, code, start_index, origin_num, threshold_money): + def __sum_buy_num_for_cancel_order(cls, code, start_index, origin_num, threshold_money, cancel_single=True): buy_nums = origin_num total_datas = local_today_datas[code] limit_up_price = gpcode_manager.get_limit_up_price(code) @@ -807,7 +891,8 @@ same_time_property = cls.__get_same_time_property(code) # 鍚屼竴绉掞紝鍦ㄩ浼颁拱鍏ヤ綅涔嬪悗鐨勬暟鎹箣鍜� property_buy_num_count = 0 - cls.cancel_debug(code, "鎾ゅ崟绾拱棰濊绠椾綅缃細{}-{} 棰勪及鎸備拱浣嶏細{}", start_index, len(total_datas) - 1, sure_pos) + cls.cancel_debug(code, "鎾ゅ崟绾拱棰濊绠椾綅缃細{}-{} 棰勪及鎸備拱浣嶏細{} 鏄惁鏈夋挙鍗曚俊鍙凤細{}", start_index, len(total_datas) - 1, sure_pos, + cancel_single) for i in range(start_index, len(total_datas)): data = total_datas[i] _val = data["val"] @@ -828,7 +913,7 @@ # 鎵惧埌涔版挙鏁版嵁鐨勪拱鍏ョ偣 if buy_index < sure_pos: buy_nums -= int(_val["num"]) * int(data["re"]) - cls.cancel_debug(code, "{}鏁版嵁鍦ㄩ浼颁拱鍏ヤ綅涔嬪墠 鎾や拱绾拱棰濓細{}", i, buy_nums * limit_up_price) + cls.cancel_debug(code, "{}鏁版嵁鍦ㄩ浼颁拱鍏ヤ綅涔嬪墠 鎾や拱绾拱鎵嬫暟锛歿} 鐩爣鎵嬫暟锛歿}", i, buy_nums, threshold_num) else: cls.cancel_debug(code, "{}鏁版嵁鍦ㄩ浼颁拱鍏ヤ綅涔嬪悗锛屼拱鍏ヤ綅锛歿}", i, buy_index) if sure_data["val"]["time"] == buy_data["val"]["time"]: @@ -840,10 +925,14 @@ cls.cancel_debug(code, "鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�: 浣嶇疆-{} 鏁版嵁-{}", i, data) property_buy_num = round(property_buy_num_count * same_time_property) - cls.cancel_debug(code, "棰勪及涔板叆鐐逛箣鍚庡悓涓�绉掍拱鍏ユ墜鏁�-{}锛屼綅缃�-{}锛屾�绘墜鏁帮細{}", property_buy_num, i, buy_nums + property_buy_num) - if buy_nums + property_buy_num <= threshold_num: + cls.cancel_debug(code, "棰勪及涔板叆鐐逛箣鍚庡悓涓�绉掍拱鍏ユ墜鏁�-{}锛屼綅缃�-{}锛屾�绘墜鏁帮細{}锛岀洰鏍囨墜鏁帮細{}", property_buy_num, i, + buy_nums + property_buy_num, threshold_num) + # 鏈夋挙鍗曚俊鍙凤紝涓斿皬浜庨槇鍊� + if buy_nums + property_buy_num <= threshold_num and cancel_single: return i, buy_nums + property_buy_num, sure_type - return None, buy_nums + round(property_buy_num_count * same_time_property), sure_type + buy_num_news = buy_nums + round(property_buy_num_count * same_time_property) + cls.cancel_debug(code, "澶勭悊璧峰浣嶇疆锛歿} 鏈�缁堢函涔伴锛歿}", start_index, buy_num_news) + return None, buy_num_news, sure_type @classmethod def test(cls): -- Gitblit v1.8.0