From ed9e2367eea9baa6c8bea82e0f81c209ffb2a56f Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期一, 02 一月 2023 17:55:50 +0800
Subject: [PATCH] 撤单策略再次修改

---
 l2_data_manager_new.py |  771 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 598 insertions(+), 173 deletions(-)

diff --git a/l2_data_manager_new.py b/l2_data_manager_new.py
index 387f80d..5740086 100644
--- a/l2_data_manager_new.py
+++ b/l2_data_manager_new.py
@@ -6,6 +6,7 @@
 
 import big_money_num_manager
 import code_data_util
+import constant
 import global_util
 import gpcode_manager
 import industry_codes_sort
@@ -14,12 +15,14 @@
 import l2_data_util
 import l2_trade_factor
 import l2_trade_test
+import l2_trade_util
 import limit_up_time_manager
 import redis_manager
 import ths_industry_util
 import tool
 import trade_manager
 import trade_queue_manager
+import trade_data_manager
 from l2_data_manager import L2DataException, TradePointManager, local_today_datas, L2DataUtil, load_l2_data, \
     local_today_num_operate_map
 from log import logger_l2_trade, logger_l2_trade_cancel, logger_l2_trade_buy, logger_l2_process, logger_buy_1_volumn, \
@@ -177,7 +180,6 @@
     # capture_timestamp:鎴浘鏃堕棿鎴�
     def process(cls, code, datas, capture_timestamp):
         cls.random_key[code] = random.randint(0, 100000)
-        now_time_str = datetime.datetime.now().strftime("%H:%M:%S")
         __start_time = round(t.time() * 1000)
         try:
             if len(datas) > 0:
@@ -194,64 +196,86 @@
                         local_today_datas[code]) > 0:
                     _start_index = local_today_datas[code][-1]["index"] + 1
                 add_datas = l2_data_manager.L2DataUtil.get_add_data(code, datas, _start_index)
-                # ---------- 鍒ゆ柇鏄惁闇�瑕佽绠楀ぇ鍗� -----------
-                try:
-                    average_need, buy_single_index, buy_exec_index = AverageBigNumComputer.is_need_compute_average(code)
-                    # 璁$畻骞冲潎澶у崟
-                    if average_need:
-                        end_index = local_today_datas[code][-1]["index"]
-                        if len(add_datas) > 0:
-                            end_index = add_datas[-1]["index"]
-                        AverageBigNumComputer.compute_average_big_num(code, buy_exec_index, buy_single_index, end_index)
-                except Exception as e:
-                    logging.exception(e)
                 # -------------鏁版嵁澧為噺澶勭悊------------
-                if len(add_datas) > 0:
-                    # 鎷兼帴鏁版嵁
-                    local_today_datas[code].extend(add_datas)
-                    l2_data_util.load_num_operate_map(l2_data_manager.local_today_num_operate_map, code, add_datas)
-                    # 绗�1鏉℃暟鎹槸鍚︿负09:30:00
-                    if add_datas[0]["val"]["time"] == "09:30:00":
-                        if global_util.cuurent_prices.get(code):
-                            price_data = global_util.cuurent_prices.get(code)
-                            if price_data[1]:
-                                # 褰撳墠娑ㄥ仠浠凤紝璁剧疆娑ㄥ仠鏃堕棿
-                                logger_l2_process.info("寮�鐩樻定鍋滐細{}", code)
-                                # 淇濆瓨娑ㄥ仠鏃堕棿
-                                limit_up_time_manager.save_limit_up_time(code, "09:30:00")
-
-                total_datas = local_today_datas[code]
-                __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, "l2鏁版嵁棰勫鐞嗘椂闂�")
                 try:
-                    if len(add_datas) > 0:
-                        latest_time = add_datas[len(add_datas) - 1]["val"]["time"]
-                        # 鏃堕棿宸笉鑳藉お澶ф墠鑳藉鐞�
-                        # TODO 鏆傛椂鍏抽棴澶勭悊
-                        if l2_data_manager.L2DataUtil.is_same_time(now_time_str, latest_time):
-                            # 鍒ゆ柇鏄惁宸茬粡鎸傚崟
-                            state = trade_manager.get_trade_state(code)
-                            start_index = len(total_datas) - len(add_datas)
-                            end_index = len(total_datas) - 1
-                            if state == trade_manager.TRADE_STATE_BUY_DELEGATED or state == trade_manager.TRADE_STATE_BUY_PLACE_ORDER or state == trade_manager.TRADE_STATE_BUY_SUCCESS:
-                                # 宸叉寕鍗�
-                                cls.__process_order(code, start_index, end_index, capture_timestamp)
-                            else:
-                                # 鏈寕鍗�
-                                cls.__process_not_order(code, start_index, end_index, capture_timestamp)
-
-                        logger_l2_process.info("code:{} 澶勭悊鏁版嵁鑼冨洿: {}-{} 澶勭悊鏃堕棿:{} 鎴浘鏃堕棿鎴筹細{}", code, add_datas[0]["index"],
-                                               add_datas[-1]["index"], round(t.time() * 1000) - __start_time,
-                                               capture_timestamp)
-                        __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, "l2鏁版嵁澶勭悊鏃堕棿")
+                    cls.process_add_datas(code, add_datas, capture_timestamp, __start_time)
                 finally:
                     # 淇濆瓨鏁版嵁
                     l2_data_manager.save_l2_data(code, datas, add_datas)
                     __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time,
                                                        "淇濆瓨鏁版嵁鏃堕棿锛坽}锛�".format(len(add_datas)))
-
         finally:
             if code in cls.unreal_buy_dict:
                 cls.unreal_buy_dict.pop(code)
+
+    @classmethod
+    def process_add_datas(cls, code, add_datas, capture_timestamp, __start_time):
+        if len(add_datas) > 0:
+            now_time_str = datetime.datetime.now().strftime("%H:%M:%S")
+            # 鎷兼帴鏁版嵁
+            local_today_datas[code].extend(add_datas)
+            l2_data_util.load_num_operate_map(l2_data_manager.local_today_num_operate_map, code, add_datas)
+            # ---------- 鍒ゆ柇鏄惁闇�瑕佽绠楀ぇ鍗� -----------
+            try:
+                average_need, buy_single_index, buy_exec_index = AverageBigNumComputer.is_need_compute_average(
+                    code, local_today_datas[code][-1])
+                # 璁$畻骞冲潎澶у崟
+                if average_need:
+                    end_index = local_today_datas[code][-1]["index"]
+                    if len(add_datas) > 0:
+                        end_index = add_datas[-1]["index"]
+                    AverageBigNumComputer.compute_average_big_num(code, buy_single_index, buy_single_index,
+                                                                  end_index)
+            except Exception as e:
+                logging.exception(e)
+
+            try:
+                average_need, buy_single_index, buy_exec_index = SecondAverageBigNumComputer.is_need_compute_average(
+                    code, local_today_datas[code][-1])
+                # 璁$畻骞冲潎澶у崟
+                if average_need:
+                    end_index = local_today_datas[code][-1]["index"]
+                    if len(add_datas) > 0:
+                        end_index = add_datas[-1]["index"]
+                    SecondAverageBigNumComputer.compute_average_big_num(code, buy_single_index, buy_single_index,
+                                                                        end_index)
+            except Exception as e:
+                logging.exception(e)
+
+            # 绗�1鏉℃暟鎹槸鍚︿负09:30:00
+            if add_datas[0]["val"]["time"] == "09:30:00":
+                if global_util.cuurent_prices.get(code):
+                    price_data = global_util.cuurent_prices.get(code)
+                    if price_data[1]:
+                        # 褰撳墠娑ㄥ仠浠凤紝璁剧疆娑ㄥ仠鏃堕棿
+                        logger_l2_process.info("寮�鐩樻定鍋滐細{}", code)
+                        # 淇濆瓨娑ㄥ仠鏃堕棿
+                        limit_up_time_manager.save_limit_up_time(code, "09:30:00")
+
+        total_datas = local_today_datas[code]
+        __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, "l2鏁版嵁棰勫鐞嗘椂闂�")
+
+        if len(add_datas) > 0:
+            latest_time = add_datas[len(add_datas) - 1]["val"]["time"]
+            # 鏃堕棿宸笉鑳藉お澶ф墠鑳藉鐞�
+            if l2_data_manager.L2DataUtil.is_same_time(now_time_str,
+                                                       latest_time) and not l2_trade_util.is_in_forbidden_trade_codes(
+                code):
+                # 鍒ゆ柇鏄惁宸茬粡鎸傚崟
+                state = trade_manager.get_trade_state(code)
+                start_index = len(total_datas) - len(add_datas)
+                end_index = len(total_datas) - 1
+                if state == trade_manager.TRADE_STATE_BUY_DELEGATED or state == trade_manager.TRADE_STATE_BUY_PLACE_ORDER or state == trade_manager.TRADE_STATE_BUY_SUCCESS:
+                    # 宸叉寕鍗�
+                    cls.__process_order(code, start_index, end_index, capture_timestamp)
+                else:
+                    # 鏈寕鍗�
+                    cls.__process_not_order(code, start_index, end_index, capture_timestamp)
+
+            logger_l2_process.info("code:{} 澶勭悊鏁版嵁鑼冨洿: {}-{} 澶勭悊鏃堕棿:{} 鎴浘鏃堕棿鎴筹細{}", code, add_datas[0]["index"],
+                                   add_datas[-1]["index"], round(t.time() * 1000) - __start_time,
+                                   capture_timestamp)
+            __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, "l2鏁版嵁澶勭悊鏃堕棿")
 
     # 澶勭悊鏈寕鍗�
     @classmethod
@@ -260,8 +284,7 @@
         # 鑾峰彇闃堝��
         threshold_money, msg = cls.__get_threshmoney(code)
         if round(t.time() * 1000) - __start_time > 10:
-            __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time,
-                                               "鑾峰彇m鍊兼暟鎹�楁椂")
+            __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, "鑾峰彇m鍊兼暟鎹�楁椂")
         cls.__start_compute_buy(code, start_index, end_index, threshold_money, capture_time)
 
     # 娴嬭瘯涓撶敤
@@ -279,9 +302,31 @@
             return
         # 鑾峰彇涔板叆淇″彿璧峰鐐�
         buy_single_index, buy_exec_index, buy_compute_index, num, count = cls.__get_order_begin_pos(code)
+
         # 鎾ゅ崟璁$畻,鍙湅涔�1
         cancel_data, cancel_msg = L2LimitUpMoneyStatisticUtil.process_data(code, start_index, end_index,
                                                                            buy_single_index, buy_exec_index)
+
+        # 鎾ゅ崟璁$畻锛岀湅绉掔骇澶у崟鎾ゅ崟
+        try:
+            b_need_cancel, b_cancel_data = SecondAverageBigNumComputer.need_cancel(code, buy_single_index,
+                                                                                   buy_exec_index, start_index,
+                                                                                   end_index)
+            if b_need_cancel and not cancel_data:
+                cancel_data = b_cancel_data
+                cancel_msg = "鐢虫姤鏃堕棿鎴嚦澶у崟鎾ら攢姣斾緥瑙﹀彂闃堝��"
+        except Exception as e:
+            logging.exception(e)
+
+        # 鎾ゅ崟璁$畻锛岀湅鍒嗛挓绾уぇ鍗曟挙鍗�
+        try:
+            b_need_cancel, b_cancel_data = AverageBigNumComputer.need_cancel(code, buy_single_index, buy_exec_index,
+                                                                             start_index, end_index)
+            if b_need_cancel and not cancel_data:
+                cancel_data = b_cancel_data
+                cancel_msg = "1鍒嗛挓鍐呭ぇ鍗曟挙閿�姣斾緥瑙﹀彂闃堝��"
+        except Exception as e:
+            logging.exception(e)
 
         if not cancel_data:
             # 缁熻鏉夸笂鍗�
@@ -294,13 +339,18 @@
         # 璁$畻m鍊煎ぇ鍗�
         cls.l2BigNumForMProcessor.process(code, max(buy_single_index, start_index), end_index,
                                           gpcode_manager.get_limit_up_price(code))
-
         if cancel_data:
+            if cancel_data["index"] == 175:
+                print("杩涘叆璋冭瘯")
             cls.debug(code, "瑙﹀彂鎾ゅ崟锛屾挙鍗曚綅缃細{} 锛屾挙鍗曞師鍥狅細{}", cancel_data["index"], cancel_msg)
             # 鎾ゅ崟
-            cls.cancel_buy(code, cancel_msg)
-            # 缁х画璁$畻涓嬪崟
-            cls.__process_not_order(code, cancel_data["index"] + 1, end_index, capture_time)
+            if cls.cancel_buy(code, cancel_msg):
+                # 鎾ゅ崟鎴愬姛锛岀户缁绠椾笅鍗�
+                cls.__process_not_order(code, cancel_data["index"] + 1, end_index, capture_time)
+            else:
+                # 鎾ゅ崟灏氭湭鎴愬姛
+                pass
+
         else:
             # 濡傛灉鏈夎櫄鎷熶笅鍗曢渶瑕佺湡瀹炰笅鍗�
             unreal_buy_info = cls.unreal_buy_dict.get(code)
@@ -336,9 +386,11 @@
                 cls.debug(code, "寮�濮嬫墽琛屼拱鍏�")
                 trade_manager.start_buy(code, capture_timestamp, last_data,
                                         last_data_index)
+                trade_data_manager.placeordercountmanager.place_order(code)
                 # 鑾峰彇涔板叆浣嶇疆淇℃伅
                 try:
                     buy_single_index, buy_exec_index, buy_compute_index, num, count = cls.__get_order_begin_pos(code)
+                    SecondAverageBigNumComputer.place_order_success(code, buy_single_index, buy_exec_index)
                     AverageBigNumComputer.place_order_success(code, buy_single_index, buy_exec_index)
                     LongAverageBigNumComputer.place_order_success(code, buy_single_index, buy_exec_index)
                 except Exception as e:
@@ -355,28 +407,31 @@
     # 鏄惁鍙互鍙栨秷
     @classmethod
     def __can_cancel(cls, code):
+        if constant.TEST:
+            return True, ""
+        # 鏆傛椂娉ㄩ噴鎺�
         # 14鐐瑰悗濡傛灉鏄澘鍧楄�佸ぇ灏变笉闇�瑕佸彇娑堜簡
-        now_time_str = tool.get_now_time_str()
-        if int(now_time_str.replace(":", "")) >= 140000:
-            industry, codes = ths_industry_util.get_same_industry_codes(code, gpcode_manager.get_gp_list())
-            if industry is None:
-                return True, "娌℃湁鑾峰彇鍒拌涓�"
-            codes_index = industry_codes_sort.sort_codes(codes, code)
-            if codes_index is not None and codes_index.get(code) is not None:
-                # 鍚屼竴鏉垮潡涓�佷簩鍚庨潰鐨勪笉鑳戒拱
-                if codes_index.get(code) == 0:
-                    return False, "14:00鍚庤�佸ぇ涓嶈兘鎾ゅ崟"
-                elif codes_index.get(code) == 1:
-                    # 鍒ゆ柇鑰佸ぇ鏄惁閮芥槸09锛�30锛�00娑ㄥ仠鐨�
-                    # 鍚�1鏉垮潡鑰佸ぇ鏄�09:30:00娑ㄥ仠锛岃�佷簩14锛�00鐮稿紑鐨勪笉鎾�
-                    first_count = 0
-                    for key in codes_index:
-                        if codes_index[key] == 0:
-                            first_count += 1
-                            if limit_up_time_manager.get_limit_up_time(key) == "09:30:00":
-                                first_count -= 1
-                    if first_count == 0:
-                        return False, "14:00鍚庤�佸ぇ閮藉紑鐩樻定鍋滐紝鑰佷簩涓嶈兘鎾ゅ崟"
+        # now_time_str = tool.get_now_time_str()
+        # if int(now_time_str.replace(":", "")) >= 140000:
+        #     industry, codes = ths_industry_util.get_same_industry_codes(code, gpcode_manager.get_gp_list())
+        #     if industry is None:
+        #         return True, "娌℃湁鑾峰彇鍒拌涓�"
+        #     codes_index = industry_codes_sort.sort_codes(codes, code)
+        #     if codes_index is not None and codes_index.get(code) is not None:
+        #         # 鍚屼竴鏉垮潡涓�佷簩鍚庨潰鐨勪笉鑳戒拱
+        #         if codes_index.get(code) == 0:
+        #             return False, "14:00鍚庤�佸ぇ涓嶈兘鎾ゅ崟"
+        #         elif codes_index.get(code) == 1:
+        #             # 鍒ゆ柇鑰佸ぇ鏄惁閮芥槸09锛�30锛�00娑ㄥ仠鐨�
+        #             # 鍚�1鏉垮潡鑰佸ぇ鏄�09:30:00娑ㄥ仠锛岃�佷簩14锛�00鐮稿紑鐨勪笉鎾�
+        #             first_count = 0
+        #             for key in codes_index:
+        #                 if codes_index[key] == 0:
+        #                     first_count += 1
+        #                     if limit_up_time_manager.get_limit_up_time(key) == "09:30:00":
+        #                         first_count -= 1
+        #             if first_count == 0:
+        #                 return False, "14:00鍚庤�佸ぇ閮藉紑鐩樻定鍋滐紝鑰佷簩涓嶈兘鎾ゅ崟"
 
         return True, ""
 
@@ -414,11 +469,10 @@
         except Exception as e:
             logging.exception(e)
 
-
         # 閲忔瘮瓒呰繃1.1鐨勪笉鑳戒拱
         volumn_rate = l2_trade_factor.L2TradeFactorUtil.get_volumn_rate_by_code(code)
-        if volumn_rate >= 1.1:
-            return False, "鏈�澶ч噺姣旇秴杩�1.1涓嶈兘涔�"
+        if volumn_rate >= 1.3:
+            return False, "鏈�澶ч噺姣旇秴杩�1.3涓嶈兘涔�"
 
         limit_up_time = limit_up_time_manager.get_limit_up_time(code)
         if limit_up_time is not None and l2_data_manager.L2DataUtil.get_time_as_second(
@@ -496,15 +550,20 @@
             l2_data_manager.L2BetchCancelBigNumProcessor.del_recod(code)
             cls.debug(code, "鎵ц鎾ゅ崟鎴愬姛")
         except Exception as e:
+            logging.exception(e)
             cls.debug(code, "鎵ц鎾ゅ崟寮傚父锛歿}", str(e))
 
     @classmethod
-    def cancel_buy(cls, code, msg=None):
-        can_cancel, reason = cls.__can_cancel(code)
-        if not can_cancel:
-            # 涓嶈兘鍙栨秷
-            cls.cancel_debug(code, "鎾ゅ崟涓柇锛屽師鍥狅細{}", reason)
-            return
+    def cancel_buy(cls, code, msg=None, source="l2"):
+        # 鏄惁鏄氦鏄撻槦鍒楄Е鍙�
+        if source == "trade_queue":
+            # 浜ゆ槗闃熷垪瑙﹀彂鐨勯渶瑕佷笅鍗曞悗5s
+            buy_single_index, buy_exec_index, buy_compute_index, num, count = cls.__get_order_begin_pos(code)
+            total_datas = local_today_datas[code]
+            if buy_exec_index is not None and buy_exec_index > 0:
+                now_time_str = datetime.datetime.now().strftime("%H:%M:%S")
+                if tool.trade_time_sub(now_time_str, total_datas[buy_exec_index]["val"]["time"]) < 5:
+                    return False
 
         l2_data_manager.L2ContinueLimitUpCountManager.del_data(code)
         if code in cls.unreal_buy_dict:
@@ -515,16 +574,26 @@
             l2_data_manager.TradePointManager.delete_compute_info_for_cancel_buy(code)
             l2_data_manager.TradePointManager.delete_count_info_for_cancel_buy(code)
         else:
+            can_cancel, reason = cls.__can_cancel(code)
+            if not can_cancel:
+                # 涓嶈兘鍙栨秷
+                cls.cancel_debug(code, "鎾ゅ崟涓柇锛屽師鍥狅細{}", reason)
+                cls.debug(code, "鎾ゅ崟涓柇锛屽師鍥狅細{}", reason)
+                return False
             cls.__cancel_buy(code)
 
         l2_data_manager.L2BigNumProcessor.del_big_num_pos(code)
         cls.debug(code, "鎵ц鎾ゅ崟鎴愬姛锛屽師鍥狅細{}", msg)
+        return True
 
     # 铏氭嫙涓嬪崟
     @classmethod
     def __virtual_buy(cls, code, buy_single_index, buy_exec_index, capture_time):
         cls.unreal_buy_dict[code] = (buy_exec_index, capture_time)
+        SecondAverageBigNumComputer.place_order_success(code, buy_single_index, buy_exec_index)
         AverageBigNumComputer.place_order_success(code, buy_single_index, buy_exec_index)
+        # 鍒犻櫎涔嬪墠鐨勬澘涓婂崠淇℃伅
+        L2LimitUpSellStatisticUtil.delete(code)
 
     @classmethod
     def __start_compute_buy(cls, code, compute_start_index, compute_end_index, threshold_money, capture_time,
@@ -600,10 +669,27 @@
 
             # 鏁版嵁鏄惁澶勭悊瀹屾瘯
             if compute_index >= compute_end_index:
+                need_cancel, cancel_data = SecondAverageBigNumComputer.need_cancel(code, buy_single_index,
+                                                                                   compute_index,
+                                                                                   buy_single_index, compute_index,
+                                                                                   True)
+                # 鍒嗛挓绾уぇ鍗曡绠�
+                # need_cancel, cancel_data = AverageBigNumComputer.need_cancel(code, buy_single_index, compute_index,
+                #                                                              buy_single_index, compute_index, True)
                 cls.debug(code, "鏁版嵁澶勭悊瀹屾瘯锛屼笅鍗�, 鏁版嵁鎴浘鏃堕棿-{}", capture_time)
                 # 鏁版嵁宸茬粡澶勭悊瀹屾瘯锛屽鏋滆繕娌℃挙鍗曞氨瀹為檯涓嬪崟
-                cls.__buy(code, capture_time, total_datas[compute_index], compute_index)
+                if need_cancel:
+                    if cls.cancel_buy(code, "鍒嗛挓绾уぇ鍗曟挙閿�"):
+                        # 鎵ц鎾ゅ崟鎴愬姛
+                        pass
+                else:
+                    cls.__buy(code, capture_time, total_datas[compute_index], compute_index)
             else:
+                # AverageBigNumComputer.need_cancel(code, buy_single_index, compute_index,
+                #                                   buy_single_index, compute_index, False)
+                SecondAverageBigNumComputer.need_cancel(code, buy_single_index, compute_index,
+                                                        buy_single_index, compute_index, False)
+
                 # 鏁版嵁灏氭湭澶勭悊瀹屾瘯锛岃繘琛屼笅涓�姝ュ鐞�
                 cls.debug(code, "鏁版嵁灏氭湭澶勭悊瀹屾瘯锛岃繘琛屼笅涓�姝ュ鐞嗭紝澶勭悊杩涘害锛歿}", compute_index)
                 # 澶勭悊鎾ゅ崟姝ラ
@@ -743,11 +829,23 @@
 
         buy_single_time_seconds = L2DataUtil.get_time_as_second(total_datas[buy_single_index]["val"]["time"])
 
+        # 鍙互瑙﹀彂涔�
+        trigger_buy = True
+        place_order_count = trade_data_manager.placeordercountmanager.get_place_order_count(code)
+        max_space_time = 2
+        if place_order_count <= 0:
+            max_space_time = 2
+        elif place_order_count <= 1:
+            max_space_time = 6 - 1
+        else:
+            max_space_time = 9 - 1
+
         for i in range(compute_start_index, compute_end_index + 1):
             data = total_datas[i]
             _val = total_datas[i]["val"]
+            trigger_buy = False
             # 蹇呴』涓鸿繛缁�3绉掑唴鐨勬暟鎹�
-            if L2DataUtil.get_time_as_second(_val["time"]) - buy_single_time_seconds > 2:
+            if L2DataUtil.get_time_as_second(_val["time"]) - buy_single_time_seconds > max_space_time:
                 TradePointManager.delete_buy_point(code)
                 if i == compute_end_index:
                     # 鏁版嵁澶勭悊瀹屾瘯
@@ -761,42 +859,48 @@
             if L2DataUtil.is_limit_up_price_buy(_val):
                 if cls.__is_big_money(limit_up_price, _val):
                     sub_threshold_count += int(total_datas[i]["re"])
-                # 娑ㄥ仠涔�
-                buy_nums += int(_val["num"]) * int(total_datas[i]["re"])
-                buy_count += int(total_datas[i]["re"])
-                if buy_nums >= threshold_num and buy_count >= get_threshold_count():
-                    logger_l2_trade_buy.info("{}鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛歿} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿} 缁熻绾拱鍗曟暟锛歿} 鐩爣绾拱鍗曟暟锛歿}, 澶у崟鏁伴噺锛歿}", code, i,
-                                             buy_nums,
-                                             threshold_num, buy_count, get_threshold_count(), sub_threshold_count)
+                if round(int(_val["num"]) * float(_val["price"])) >= 5900:
+                    trigger_buy = True
+                    # 鍙粺璁�59涓囦互涓婄殑閲戦
+                    buy_nums += int(_val["num"]) * int(total_datas[i]["re"])
+                    buy_count += int(total_datas[i]["re"])
+                    if buy_nums >= threshold_num and buy_count >= get_threshold_count():
+                        logger_l2_trade_buy.info("{}鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛歿} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿} 缁熻绾拱鍗曟暟锛歿} 鐩爣绾拱鍗曟暟锛歿}, 澶у崟鏁伴噺锛歿}", code,
+                                                 i,
+                                                 buy_nums,
+                                                 threshold_num, buy_count, get_threshold_count(), sub_threshold_count)
             elif L2DataUtil.is_limit_up_price_buy_cancel(_val):
                 if cls.__is_big_money(limit_up_price, _val):
                     sub_threshold_count -= int(total_datas[i]["re"])
-                # 娑ㄥ仠涔版挙
-                # 鍒ゆ柇涔板叆浣嶇疆鏄惁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓�
-                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"])
-                        buy_count -= 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"]:
-                            # 鍚屼竴绉�,褰撲綔涔板叆淇″彿涔嬪悗澶勭悊
+                if round(int(_val["num"]) * float(_val["price"])) >= 5900:
+                    # 鍙粺璁�59涓囦互涓婄殑閲戦
+                    # 娑ㄥ仠涔版挙
+                    # 鍒ゆ柇涔板叆浣嶇疆鏄惁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓�
+                    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"])
                             buy_count -= int(data["re"])
-                            cls.buy_debug(code, "{}鏁版嵁涔板叆浣嶄笌棰勪及涔板叆浣嶅湪鍚屼竴绉�", i)
-                else:
-                    # 鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�
-                    cls.buy_debug(code, "鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�: 浣嶇疆-{} 鏁版嵁-{}", i, data)
-                    buy_nums -= int(_val["num"]) * int(total_datas[i]["re"])
-                    buy_count -= int(total_datas[i]["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"]:
+                                # 鍚屼竴绉�,褰撲綔涔板叆淇″彿涔嬪悗澶勭悊
+                                buy_nums -= int(_val["num"]) * int(data["re"])
+                                buy_count -= int(data["re"])
+                                cls.buy_debug(code, "{}鏁版嵁涔板叆浣嶄笌棰勪及涔板叆浣嶅湪鍚屼竴绉�", i)
+                    else:
+                        # 鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�
+                        cls.buy_debug(code, "鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�: 浣嶇疆-{} 鏁版嵁-{}", i, data)
+                        buy_nums -= int(_val["num"]) * int(total_datas[i]["re"])
+                        buy_count -= int(total_datas[i]["re"])
             cls.buy_debug(code, "浣嶇疆-{}锛屾�绘墜鏁帮細{}锛岀洰鏍囨墜鏁帮細{}", i,
                           buy_nums, threshold_num)
             # 鏈夋挙鍗曚俊鍙凤紝涓斿皬浜庨槇鍊�
-            if buy_nums >= threshold_num and buy_count >= get_threshold_count():
+            if buy_nums >= threshold_num and buy_count >= get_threshold_count() and trigger_buy:
                 return i, buy_nums, buy_count, None
 
         cls.buy_debug(code, "灏氭湭鑾峰彇鍒颁拱鍏ユ墽琛岀偣锛岃捣濮嬭绠椾綅缃細{} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿}  缁熻绾拱鍗曟暟锛歿} 鐩爣绾拱鍗曟暟锛歿} 澶у崟鏁伴噺锛歿}",
@@ -1194,7 +1298,7 @@
                         # 涓庢墽琛屼綅鐩搁殧>=5s鏃惰鍒欑敓鏁�
                         if exec_time_offset >= 5:
                             cancel_index = i
-                            cancel_msg = "灏佸崟閲戦灏忎簬1000涓�"
+                            cancel_msg = "灏佸崟閲戦灏忎簬1000涓�,涓簕}".format(total_num)
                             break
                     # 鐩搁偦2s鍐呯殑鏁版嵁鍑忓皬50%
                     # 涓�1s鐨勬�绘暟
@@ -1227,14 +1331,15 @@
                 # ------澶у崟鎾ゅ鐞�-------
                 # if total_num < min_volumn_big:
                 if exec_time_offset < 1800:
-                    try:
-                        b_need_cancel, b_cancel_index = AverageBigNumComputer.need_cancel(code, buy_exec_index, i, i)
-                        if b_need_cancel:
-                            cancel_index = b_cancel_index
-                            cancel_msg = "1鍒嗛挓鍐呭ぇ鍗曟挙閿�姣斾緥瑙﹀彂闃堝��"
-                            break
-                    except Exception as e:
-                        logging.exception(e)
+                    pass
+                    # try:
+                    #     b_need_cancel, b_cancel_index = AverageBigNumComputer.need_cancel(code, i, i)
+                    #     if b_need_cancel:
+                    #         cancel_index = b_cancel_index
+                    #         cancel_msg = "1鍒嗛挓鍐呭ぇ鍗曟挙閿�姣斾緥瑙﹀彂闃堝��"
+                    #         break
+                    # except Exception as e:
+                    #     logging.exception(e)
                 # 30鍒嗛挓澶栨墠鎵ц
                 elif 1800 <= exec_time_offset <= 5400:
                     try:
@@ -1322,7 +1427,8 @@
         # 鑾峰彇娑ㄥ仠鍗栫殑闃堝��
         limit_up_price = gpcode_manager.get_limit_up_price(code)
         zyltgb = l2_trade_factor.L2TradeFactorUtil.get_zyltgb(code)
-        threshold_num = int(zyltgb * 0.015) // (limit_up_price * 100)
+        # 澶т簬鑷敱娴侀�氬競鍊肩殑4.8%
+        threshold_num = int(zyltgb * 0.048) // (limit_up_price * 100)
         total_num = cls.__get_sell_data(code)
         cancel_index = None
         process_index = cls.__get_process_index(code)
@@ -1332,7 +1438,7 @@
                 continue
             if i <= process_index:
                 continue
-            if L2DataUtil.is_limit_up_price_sell(total_datas[i]["val"]):
+            if L2DataUtil.is_limit_up_price_sell(total_datas[i]["val"]) or L2DataUtil.is_sell(total_datas[i]["val"]):
                 num = int(total_datas[i]["val"]["num"])
                 cls.__incre_sell_data(code, num)
                 total_num += num
@@ -1343,7 +1449,9 @@
             process_index = cancel_index
         else:
             process_index = end_index
-        # 淇濆瓨澶勭悊鐨勪綅缃�
+        L2TradeDataProcessor.cancel_debug(code, "鏉夸笂鍗栦俊鎭細璁$畻浣嶇疆锛歿}-{} 鏉夸笂鍗栨暟鎹畕}/{}", start_index, end_index, total_num,
+                                          threshold_num)
+
         cls.__save_process_index(code, process_index)
         if cancel_index is not None:
             return total_datas[cancel_index], "鏉夸笂鍗栫殑鎵嬫暟{} 瓒呰繃{}".format(total_num, threshold_num)
@@ -1355,6 +1463,259 @@
         load_l2_data(code)
         L2TradeDataProcessor.random_key[code] = 123123
         cls.process(code, 126, 171, 126)
+
+
+# s绾у钩鍧囧ぇ鍗曡绠�
+# 璁$畻鑼冨洿鍒扮敵鎶ユ椂闂寸殑閭d竴绉�
+class SecondAverageBigNumComputer:
+    __redis_manager = redis_manager.RedisManager(0)
+    __place_order_time_dict = {}
+
+    @classmethod
+    def __getRedis(cls):
+        return cls.__redis_manager.getRedis()
+
+    @classmethod
+    def __save_average_data(cls, code, average_num, average_up_count, start_index, end_index):
+        key = "s_average_big_num-{}".format(code)
+        cls.__getRedis().setex(key, 2000, json.dumps((average_num, average_up_count, start_index, end_index)))
+        L2TradeDataProcessor.cancel_debug(code, "淇濆瓨绉掔骇澶у崟浣嶇疆淇℃伅锛氬钩鍧囨墜鏁�-{} 澶у崟鏁伴噺-{} 璁$畻寮�濮嬭寖鍥�-{}:{}".format(average_num,
+                                                                                                 average_up_count,
+                                                                                                 start_index,
+                                                                                                 end_index))
+
+    @classmethod
+    def __get_average_data(cls, code):
+        key = "s_average_big_num-{}".format(code)
+        val = cls.__getRedis().get(key)
+        if val is None:
+            return None, None, None, None
+        val = json.loads(val)
+        return val[0], val[1], val[2], val[3]
+
+    # 淇濆瓨涔版挙鏁版嵁
+    @classmethod
+    def __save_cancel_data(cls, code, cancel_index):
+        key = "s_average_big_num_comput_info-{}".format(code)
+        cls.__getRedis().sadd(key, cancel_index)
+
+    # 鑾峰彇涔版挙鐨勬暟鎹�
+    @classmethod
+    def __get_cancel_datas(cls, code):
+        key = "s_average_big_num_comput_info-{}".format(code)
+        val = cls.__getRedis().smembers(key)
+        return val
+
+    # 淇濆瓨涔版挙鏁版嵁
+    @classmethod
+    def __save_apply_time(cls, code, time_str):
+        key = "s_average_big_num_apply_time-{}".format(code)
+        cls.__getRedis().setex(key, tool.get_expire(), time_str)
+
+    # 鑾峰彇涔版挙鐨勬暟鎹�
+    @classmethod
+    def __get_apply_time(cls, code):
+        key = "s_average_big_num_apply_time-{}".format(code)
+        val = cls.__getRedis().get(key)
+        return val
+
+    @classmethod
+    def __clear_data(cls, code):
+        key = "s_average_big_num_comput_info-{}".format(code)
+        cls.__getRedis().delete(key)
+        key = "s_average_big_num-{}".format(code)
+        cls.__getRedis().delete(key)
+
+    @classmethod
+    def clear_data(cls):
+        key = "s_average_big_num_comput_info-*"
+        keys = cls.__getRedis().keys(key)
+        for k in keys:
+            cls.__getRedis().delete(k)
+        key = "s_average_big_num-*"
+        keys = cls.__getRedis().keys(key)
+        for k in keys:
+            cls.__getRedis().delete(k)
+
+    # 璁$畻骞冲潎鎵嬫暟
+    # 璁$畻鑼冨洿锛氫拱鍏ヤ俊鍙疯捣濮嬬偣鍒颁拱鍏ユ墽琛屼綅鐨勪笅涓�寮犲浘缁撴潫鐐规暟鎹负姝�
+    @classmethod
+    def compute_average_big_num(cls, code, buy_single_index, start_index, end_index):
+        print("compute_average_big_num", code, buy_single_index, start_index, end_index)
+        L2TradeDataProcessor.cancel_debug(code, "寮�濮嬭绠楃煭澶у崟浣嶇疆")
+        total_data = local_today_datas[code]
+        num = 0
+        count = 0
+        apply_time_second = int(cls.get_apply_time(code).replace(":", ""))
+        for i in range(start_index, end_index + 1):
+            data = total_data[i]
+            val = data["val"]
+            if int(val["time"].replace(":", "")) > apply_time_second:
+                # 閲嶆柊璁剧疆璁$畻缁撴潫浣嶇疆
+                end_index = i - 1
+                break
+
+            if L2DataUtil.is_limit_up_price_buy(val):  # and float(val["price"]) * int(val["num"]) > 7500:
+                # 75涓囦互涓婄殑鎵嶅弬涓庤绠楀钩鍧囧ぇ鍗�
+                count += data["re"]
+                num += int(val["num"])
+        # 濡傛灉娌℃湁鎵惧埌75涓囦互涓婄殑鍗曞氨涓嶆坊鍔�75w鐨勭瓫閫夋潯浠�
+        if count == 0:
+            for i in range(start_index, end_index + 1):
+                data = total_data[i]
+                val = data["val"]
+                if L2DataUtil.is_limit_up_price_buy(val):
+                    if int(val["time"].replace(":", "")) > apply_time_second:
+                        break
+                    # 75涓囦互涓婄殑鎵嶅弬涓庤绠楀钩鍧囧ぇ鍗�
+                    count += data["re"]
+                    num += int(val["num"])
+
+        average_num = num // count
+        average_num = round(5900/ gpcode_manager.get_limit_up_price(code))
+        average_up_count = 0
+        for i in range(start_index, end_index + 1):
+            data = total_data[i]
+            val = data["val"]
+            if L2DataUtil.is_limit_up_price_buy(val):
+                if int(val["time"].replace(":", "")) > apply_time_second:
+                    break
+                if int(val["num"]) >= average_num:
+                    average_up_count += data["re"]
+        print("骞冲潎鎵嬫暟锛�", average_num, "澶у崟鎬绘暟锛�", average_up_count)
+        # 淇濆瓨鏁版嵁
+        cls.__save_average_data(code, average_num, average_up_count, start_index, end_index)
+
+    # 鏄惁闇�瑕佹挙鍗�
+    @classmethod
+    def need_cancel(cls, code, buy_single_index, buy_exec_index, start_index, end_index, need_cancel=True):
+        average_num, average_up_count, a_start_index, a_end_index = cls.__get_average_data(code)
+        if average_num is None:
+            return False, None
+        total_data = local_today_datas[code]
+        # 濡傛灉start_index涓巄uy_single_index鐩稿悓锛屽嵆鏄笅鍗曞悗鐨勭涓�娆¤绠�
+        # 闇�瑕佹煡璇拱鍏ヤ俊鍙蜂箣鍓嶇殑鍚�1s鏄惁鏈夋定鍋滄挙鐨勬暟鎹�
+        if buy_single_index == start_index:
+            for i in range(buy_single_index - 1, 0, -1):
+                data = total_data[i]
+                val = data["val"]
+                if val["time"] != total_data[buy_single_index]["val"]["time"]:
+                    break
+                if L2DataUtil.is_limit_up_price_buy_cancel(val) and int(val["cancelTime"]) == 0:
+                    # 娑ㄥ仠涔版挙閿�涓旀挙閿�鐨勯棿闅旀椂闂翠负0
+                    # 鏌ヨ涔板叆淇″彿锛屽鏋滄棤娉曟煡璇㈠埌鎴栬�呮槸涔板叆浣嶇疆姣斾拱鍏ヤ俊鍙峰皬灏变笉绠�
+                    buy_index, buy_data = l2_data_util.get_buy_data_with_cancel_data(data,
+                                                                                     local_today_num_operate_map.get(
+                                                                                         code))
+                    if buy_index is not None and a_start_index <= buy_index <= a_end_index:
+                        # 鍦ㄤ拱鍏ヤ俊鍙蜂箣鍚�
+                        cls.__save_cancel_data(code, i)
+
+        for i in range(start_index, end_index + 1):
+            data = total_data[i]
+            val = data["val"]
+            # print("澶勭悊杩涘害", i)
+            if L2DataUtil.is_limit_up_price_buy_cancel(val):
+
+                # 鏌ヨ涔板叆浣嶇疆
+                buy_index, buy_data = l2_data_util.get_buy_data_with_cancel_data(data,
+                                                                                 local_today_num_operate_map.get(
+                                                                                     code))
+                if buy_index is not None and a_start_index <= buy_index <= a_end_index:
+                    cls.__save_cancel_data(code, i)
+        if need_cancel:
+            # 璁$畻涔版挙澶у崟鏆傛瘮
+            cancel_datas = cls.__get_cancel_datas(code)
+
+            if cancel_datas is not None and len(cancel_datas) > 0:
+                cancel_rate_threshold = 0.49
+                place_order_count = trade_data_manager.placeordercountmanager.get_place_order_count(code)
+                if place_order_count <= 1:
+                    cancel_rate_threshold = 0.49
+                elif place_order_count <= 2:
+                    ancel_rate_threshold = 0.549
+                else:
+                    ancel_rate_threshold = 0.59
+                cancel_indexs = []
+                for index in cancel_datas:
+                    cancel_indexs.append(int(index))
+                cancel_indexs.sort()
+                # print("鍙栨秷鐨勬暟鎹�", cancel_indexs)
+                cancel_count = 0
+                for index in cancel_indexs:
+                    data = total_data[index]
+                    if int(data["val"]["num"]) >= average_num:
+                        cancel_count += data["re"]
+                        if cancel_count / average_up_count > cancel_rate_threshold:
+                            return True, total_data[index]
+
+        return False, None
+
+    # 鏄惁闇�瑕佽绠�
+    @classmethod
+    def is_need_compute_average(cls, code, latest_data):
+        total_datas = local_today_datas[code]
+        data = cls.__place_order_time_dict.get(code)
+        if data is None:
+            return False, None, None
+        elif tool.trade_time_sub(latest_data["val"]["time"], cls.get_apply_time(code)) < 5:
+            # 鏈�5s鏃堕棿涓婁紶鐢虫姤鏃堕棿
+            return True, data[1], data[2]
+        else:
+            cls.__place_order_time_dict.pop(code)
+        return False, None, None
+
+    # 璁剧疆鐢虫姤鏃堕棿
+    @classmethod
+    def set_apply_time(cls, code, time_str, force=False):
+        old_time_str = cls.get_apply_time(code)
+        if not force:
+            if old_time_str is not None:
+                sub_time = tool.trade_time_sub(time_str, old_time_str)
+                if sub_time <= 0 or sub_time > 4:
+                    # 鐢虫姤鏃堕棿涓庝笅鍗曟椂闂翠笉鑳芥搷杩�4s
+                    return
+        cls.__save_apply_time(code, time_str)
+
+    @classmethod
+    def get_apply_time(cls, code):
+        return cls.__get_apply_time(code)
+
+    # 涓嬪崟鎴愬姛
+    @classmethod
+    def place_order_success(cls, code, buy_single_index, buy_exec_index):
+        cls.__clear_data(code)
+        cls.__place_order_time_dict[code] = (t.time(), buy_single_index, buy_exec_index)
+        # 浠ラ槻涓囦竴锛屽厛淇濆瓨涓嬪崟淇℃伅
+        total_data = local_today_datas[code]
+        cls.set_apply_time(code, total_data[buy_exec_index]["val"]["time"], True)
+        cls.compute_average_big_num(code, buy_single_index, buy_single_index, total_data[-1]["index"])
+
+    @classmethod
+    def __test(cls, datas):
+        code = datas[0]
+        load_l2_data(code)
+        L2TradeDataProcessor.random_key[code] = 123123
+        # 鍏堟墽琛屼笅鍗�
+        buy_single_index = datas[1]
+        buy_exec_index = datas[2]
+        local_today_datas[code] = local_today_datas[code][0:datas[4]]
+        cls.place_order_success(code, buy_single_index, buy_exec_index)
+        # 鎵ц鏄惁闇�瑕佽绠梐verage
+        cls.compute_average_big_num(code, buy_single_index, buy_single_index, datas[3])
+        for i in range(buy_single_index, datas[4]):
+            cancel, cancel_data = cls.need_cancel(code, i, i)
+            if cancel:
+                print("闇�瑕佹挙鍗�", cancel, cancel_data["index"])
+                break
+
+    @classmethod
+    def test(cls):
+        cls.__test(("000716", 410, 420, 461, 536))
+        # 浠g爜 涔板叆淇″彿璧峰鐐�  涔板叆淇℃伅鎵ц浣嶇疆  璁$畻鏈綅 鏈�杩滆绠椾綅缃�
+        # cls.__test(("002793", 292, 308, 314, 410))
+
+        # 鎵ц鏄惁闇�瑕佹挙閿�
 
 
 # 骞冲潎澶у崟璁$畻
@@ -1384,19 +1745,18 @@
         val = json.loads(val)
         return val[0], val[1], val[2], val[3]
 
+    # 淇濆瓨涔版挙鏁版嵁
     @classmethod
-    def __save_compute_info(cls, code, cancel_count, process_index):
+    def __save_cancel_data(cls, code, cancel_index):
         key = "average_big_num_comput_info-{}".format(code)
-        cls.__getRedis().setex(key, 2000, json.dumps((cancel_count, process_index)))
+        cls.__getRedis().sadd(key, cancel_index)
 
+    # 鑾峰彇涔版挙鐨勬暟鎹�
     @classmethod
-    def __get_compute_info(cls, code):
+    def __get_cancel_datas(cls, code):
         key = "average_big_num_comput_info-{}".format(code)
-        val = cls.__getRedis().get(key)
-        if val is None:
-            return None, None
-        val = json.loads(val)
-        return val[0], val[1]
+        val = cls.__getRedis().smembers(key)
+        return val
 
     @classmethod
     def __clear_data(cls, code):
@@ -1405,10 +1765,22 @@
         key = "average_big_num-{}".format(code)
         cls.__getRedis().delete(key)
 
+    @classmethod
+    def clear_data(cls):
+        key = "average_big_num_comput_info-*"
+        keys = cls.__getRedis().keys(key)
+        for k in keys:
+            cls.__getRedis().delete(k)
+        key = "average_big_num-*"
+        keys = cls.__getRedis().keys(key)
+        for k in keys:
+            cls.__getRedis().delete(k)
+
     # 璁$畻骞冲潎鎵嬫暟
     # 璁$畻鑼冨洿锛氫拱鍏ヤ俊鍙疯捣濮嬬偣鍒颁拱鍏ユ墽琛屼綅鐨勪笅涓�寮犲浘缁撴潫鐐规暟鎹负姝�
     @classmethod
-    def compute_average_big_num(cls, code, buy_exec_index, start_index, end_index):
+    def compute_average_big_num(cls, code, buy_single_index, start_index, end_index):
+        print("compute_average_big_num", code, buy_single_index, start_index, end_index)
         L2TradeDataProcessor.cancel_debug(code, "寮�濮嬭绠楃煭澶у崟浣嶇疆")
         total_data = local_today_datas[code]
         num = 0
@@ -1416,11 +1788,23 @@
         for i in range(start_index, end_index + 1):
             data = total_data[i]
             val = data["val"]
-            if L2DataUtil.is_limit_up_price_buy(val):
+            if L2DataUtil.is_limit_up_price_buy(val) and float(val["price"]) * int(val["num"]) >= 5000:
+                # 75涓囦互涓婄殑鎵嶅弬涓庤绠楀钩鍧囧ぇ鍗�
                 count += data["re"]
                 num += int(val["num"])
-        average_num = num // count
+        # 濡傛灉娌℃湁鎵惧埌75涓囦互涓婄殑鍗曞氨涓嶆坊鍔�75w鐨勭瓫閫夋潯浠�
+        if count == 0:
+            for i in range(start_index, end_index + 1):
+                data = total_data[i]
+                val = data["val"]
+                if L2DataUtil.is_limit_up_price_buy(val):
+                    # 75涓囦互涓婄殑鎵嶅弬涓庤绠楀钩鍧囧ぇ鍗�
+                    count += data["re"]
+                    num += int(val["num"])
 
+        average_num = num // count
+        #average_num = 0
+        average_num = round(5900 / gpcode_manager.get_limit_up_price(code))
         average_up_count = 0
         for i in range(start_index, end_index + 1):
             data = total_data[i]
@@ -1428,53 +1812,87 @@
             if L2DataUtil.is_limit_up_price_buy(val):
                 if int(val["num"]) >= average_num:
                     average_up_count += data["re"]
-        print("骞冲潎澶у崟锛�", average_num, average_up_count)
+        print("骞冲潎鎵嬫暟锛�", average_num, "澶у崟鎬绘暟锛�", average_up_count)
         # 淇濆瓨鏁版嵁
         cls.__save_average_data(code, average_num, average_up_count, start_index, end_index)
-        cls.__save_compute_info(code, 0, buy_exec_index)
 
     # 鏄惁闇�瑕佹挙鍗�
     @classmethod
-    def need_cancel(cls, code, buy_exec_index, start_index, end_index):
+    def need_cancel(cls, code, buy_single_index, buy_exec_index, start_index, end_index, need_cancel=True):
         average_num, average_up_count, a_start_index, a_end_index = cls.__get_average_data(code)
         if average_num is None:
             return False, None
-        cancel_count, process_index = cls.__get_compute_info(code)
         total_data = local_today_datas[code]
-        try:
-            for i in range(start_index, end_index + 1):
-                if i <= buy_exec_index:
-                    continue
-                if process_index >= i:
-                    continue
+        # 濡傛灉start_index涓巄uy_single_index鐩稿悓锛屽嵆鏄笅鍗曞悗鐨勭涓�娆¤绠�
+        # 闇�瑕佹煡璇拱鍏ヤ俊鍙蜂箣鍓嶇殑鍚�1s鏄惁鏈夋定鍋滄挙鐨勬暟鎹�
+        if buy_single_index == start_index:
+            for i in range(buy_single_index - 1, 0, -1):
                 data = total_data[i]
                 val = data["val"]
-                if L2DataUtil.is_limit_up_price_buy_cancel(val) and int(val["num"]) >= average_num:
-                    # 鏌ヨ涔板叆浣嶇疆
+                if val["time"] != total_data[buy_single_index]["val"]["time"]:
+                    break
+                if L2DataUtil.is_limit_up_price_buy_cancel(val) and int(val["cancelTime"]) == 0:
+                    # 娑ㄥ仠涔版挙閿�涓旀挙閿�鐨勯棿闅旀椂闂翠负0
+                    # 鏌ヨ涔板叆淇″彿锛屽鏋滄棤娉曟煡璇㈠埌鎴栬�呮槸涔板叆浣嶇疆姣斾拱鍏ヤ俊鍙峰皬灏变笉绠�
                     buy_index, buy_data = l2_data_util.get_buy_data_with_cancel_data(data,
                                                                                      local_today_num_operate_map.get(
                                                                                          code))
                     if buy_index is not None and a_start_index <= buy_index <= a_end_index:
-                        # 涔板叆浣嶇疆瑕佸湪骞冲潎鍊艰绠楄寖鍥村唴
+                        # 鍦ㄤ拱鍏ヤ俊鍙蜂箣鍚�
+                        cls.__save_cancel_data(code, i)
+
+        for i in range(start_index, end_index + 1):
+            data = total_data[i]
+            val = data["val"]
+            # print("澶勭悊杩涘害", i)
+            if L2DataUtil.is_limit_up_price_buy_cancel(val):
+
+                # 鏌ヨ涔板叆浣嶇疆
+                buy_index, buy_data = l2_data_util.get_buy_data_with_cancel_data(data,
+                                                                                 local_today_num_operate_map.get(
+                                                                                     code))
+                if buy_index is not None and a_start_index <= buy_index <= a_end_index:
+                    cls.__save_cancel_data(code, i)
+        if need_cancel:
+            # 璁$畻涔版挙澶у崟鏆傛瘮
+            cancel_datas = cls.__get_cancel_datas(code)
+
+            if cancel_datas is not None and len(cancel_datas) > 0:
+                cancel_rate_threshold = 0.49
+                place_order_count = trade_data_manager.placeordercountmanager.get_place_order_count(code)
+                if place_order_count <=1:
+                    cancel_rate_threshold=0.49
+                elif place_order_count <=2:
+                    ancel_rate_threshold = 0.549
+                else:
+                    ancel_rate_threshold = 0.59
+                cancel_indexs = []
+                for index in cancel_datas:
+                    cancel_indexs.append(int(index))
+                cancel_indexs.sort()
+                # print("鍙栨秷鐨勬暟鎹�", cancel_indexs)
+                cancel_count = 0
+                for index in cancel_indexs:
+                    data = total_data[index]
+                    if int(data["val"]["num"]) >= average_num:
                         cancel_count += data["re"]
-                        process_index = i
-                        print("鎾ら攢澶у崟", cancel_count)
-                        if cancel_count / average_up_count >= 0.49:
-                            return True, i
-        finally:
-            cls.__save_compute_info(code, cancel_count, process_index)
+                        if cancel_count / average_up_count > cancel_rate_threshold:
+                            return True, total_data[index]
+
         return False, None
 
     # 鏄惁闇�瑕佽绠�
     @classmethod
-    def is_need_compute_average(cls, code):
+    def is_need_compute_average(cls, code, latest_data):
+        total_datas = local_today_datas[code]
         data = cls.__place_order_time_dict.get(code)
         if data is None:
             return False, None, None
-        elif t.time() - data[0] < 0.5:
-            # 500ms鍐呯殑鏁版嵁鎵嶉渶瑕佽绠梐verage
-            cls.__place_order_time_dict.pop(code)
+        elif tool.trade_time_sub(latest_data["val"]["time"], total_datas[data[2]]["val"]["time"]) < 3:
+            # 3s鍐呯殑鏁版嵁鎵嶉渶瑕佽绠梐verage
             return True, data[1], data[2]
+        else:
+            cls.__place_order_time_dict.pop(code)
         return False, None, None
 
     # 涓嬪崟鎴愬姛
@@ -1484,7 +1902,7 @@
         cls.__place_order_time_dict[code] = (t.time(), buy_single_index, buy_exec_index)
         # 浠ラ槻涓囦竴锛屽厛淇濆瓨涓嬪崟淇℃伅
         total_data = local_today_datas[code]
-        cls.compute_average_big_num(code, buy_exec_index, buy_single_index, total_data[-1]["index"])
+        cls.compute_average_big_num(code, buy_single_index, buy_single_index, total_data[-1]["index"])
 
     @classmethod
     def __test(cls, datas):
@@ -1497,18 +1915,18 @@
         local_today_datas[code] = local_today_datas[code][0:datas[4]]
         cls.place_order_success(code, buy_single_index, buy_exec_index)
         # 鎵ц鏄惁闇�瑕佽绠梐verage
-        if cls.is_need_compute_average(code):
-            cls.compute_average_big_num(code, buy_exec_index, buy_single_index, datas[3])
-        for i in range(buy_exec_index, datas[4]):
-            cancel, index = cls.need_cancel(code, buy_exec_index, i, i)
+        cls.compute_average_big_num(code, buy_single_index, buy_single_index, datas[3])
+        for i in range(buy_single_index, datas[4]):
+            cancel, cancel_data = cls.need_cancel(code, i, i)
             if cancel:
-                print("闇�瑕佹挙鍗�", cancel, index)
+                print("闇�瑕佹挙鍗�", cancel, cancel_data["index"])
                 break
 
     @classmethod
     def test(cls):
-        # cls.__test(("601579", 311, 319, 347, 404))
-        cls.__test(("601579", 311, 319, 327, 404))
+        cls.__test(("000716", 410, 420, 461, 536))
+        # 浠g爜 涔板叆淇″彿璧峰鐐�  涔板叆淇℃伅鎵ц浣嶇疆  璁$畻鏈綅 鏈�杩滆绠椾綅缃�
+        # cls.__test(("002793", 292, 308, 314, 410))
 
         # 鎵ц鏄惁闇�瑕佹挙閿�
 
@@ -1645,6 +2063,7 @@
     def __test(cls, datas):
         code = datas[0]
         load_l2_data(code)
+        L2TradeDataProcessor.random_key[code] = random.randint(0, 100000)
         # 鍏堟墽琛屼笅鍗�
         buy_single_index = datas[1]
         buy_exec_index = datas[2]
@@ -1662,13 +2081,19 @@
 
     @classmethod
     def test(cls):
-        cls.__test(("002528", 212, 219, 372, 601))
-        cls.__test(("003005", 212, 219, 372, 601))
+        # 浠g爜 涔板叆淇″彿璧峰鐐�  涔板叆淇℃伅鎵ц浣嶇疆  璁$畻鏈綅 鏈�杩滆绠椾綅缃�
+        cls.__test(("002793", 292, 308, 332, 410))
 
         # 鎵ц鏄惁闇�瑕佹挙閿�
 
 
 if __name__ == "__main__":
-    L2LimitUpSellStatisticUtil.test()
-    print(t.time())
+    # AverageBigNumComputer.test()
+    # LongAverageBigNumComputer.test()
     # L2TradeDataProcessor.test()
+    load_l2_data("600213")
+
+    buy_index, buy_data = l2_data_util.get_buy_data_with_cancel_data(local_today_datas["600213"][84],
+                                                                     local_today_num_operate_map.get(
+                                                                         "600213"))
+    print(buy_index, buy_data)

--
Gitblit v1.8.0