From 1176a493039ded49d888b451793bc272e18a9b5a Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期二, 02 四月 2024 14:16:05 +0800
Subject: [PATCH] 激进买

---
 l2/l2_data_manager_new.py |  269 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 245 insertions(+), 24 deletions(-)

diff --git a/l2/l2_data_manager_new.py b/l2/l2_data_manager_new.py
index 4b99ef7..64f08e2 100644
--- a/l2/l2_data_manager_new.py
+++ b/l2/l2_data_manager_new.py
@@ -10,6 +10,7 @@
 from db.redis_manager_delegate import RedisUtils
 from l2.huaxin import l2_huaxin_util, huaxin_delegate_postion_manager
 from l2.l2_sell_manager import L2MarketSellManager, L2LimitUpSellManager
+from l2.l2_transaction_data_manager import HuaXinSellOrderStatisticManager
 from l2.transaction_progress import TradeBuyQueue
 from log_module import async_log_util, log_export
 from third_data import kpl_data_manager, block_info, history_k_data_util
@@ -677,6 +678,7 @@
                     info = cls.__trade_log_placr_order_info_dict[code]
                     info.mode = order_begin_pos.mode
                     info.set_buy_index(order_begin_pos.buy_single_index, order_begin_pos.buy_exec_index)
+                    info.set_sell_info(order_begin_pos.sell_info)
                     if jx_blocks:
                         info.set_kpl_blocks(list(jx_blocks))
                     elif jx_blocks_by:
@@ -690,7 +692,11 @@
                         elif not can_buy_result[0] and not can_buy_result[1]:
                             info.set_kpl_match_blocks(["闈炵嫭鑻椾笉婊¤冻韬綅"])
                         else:
-                            info.set_kpl_match_blocks(can_buy_result[0])
+                            temps = []
+                            temps.extend(can_buy_result[0])
+                            if can_buy_result[5]:
+                                temps.append(f"婵�杩涗拱鍏ワ細{can_buy_result[5]}")
+                            info.set_kpl_match_blocks(temps)
                     trade_record_log_util.add_place_order_log(code, info)
                 except Exception as e:
                     async_log_util.error(logger_l2_error, f"鍔犲叆涔板叆璁板綍鏃ュ織鍑洪敊锛歿str(e)}")
@@ -1295,24 +1301,36 @@
         new_get_single = False
         buy_single_index = order_begin_pos.buy_single_index
         if buy_single_index is None:
-            # 灏濊瘯璁$畻蹇�熸垚浜や俊鍙�
-            has_single, _index, sell_info = cls.__compute_fast_order_begin_pos(code, compute_start_index,
-                                                                               compute_end_index)
+            # ------------------------------纭畾淇″彿绉嶇被----------------------------------
+            # 绗竴姝ワ細鑾峰彇婵�杩涗笅鍗曚俊鍙�
+            continue_count = cls.__l2PlaceOrderParamsManagerDict[code].get_begin_continue_buy_count()
+            has_single, _index, sell_info = cls.__compute_active_order_begin_pos(code, continue_count, max(
+                (compute_start_index - continue_count - 1) if new_add else compute_start_index, 0), compute_end_index)
             fast_msg = None
             if has_single:
-                order_begin_pos.mode = OrderBeginPosInfo.MODE_FAST
+                order_begin_pos.mode = OrderBeginPosInfo.MODE_ACTIVE
                 order_begin_pos.sell_info = sell_info
-            elif _index is not None and _index < 0:
                 fast_msg = sell_info
-                continue_count = cls.__l2PlaceOrderParamsManagerDict[code].get_begin_continue_buy_count()
-                # 鏈変拱鍏ヤ俊鍙�
-                has_single, _index = cls.__compute_order_begin_pos(code, max(
-                    (compute_start_index - continue_count - 1) if new_add else compute_start_index, 0), continue_count,
-                                                                   compute_end_index)
-                order_begin_pos.mode = OrderBeginPosInfo.MODE_NORMAL
-                # 鑾峰彇鏈�鏂扮殑涔颁俊鎭�
-                sell_info = cls.__L2MarketSellManager.get_current_total_sell_data(code)
-                order_begin_pos.sell_info = sell_info
+
+            if not has_single:
+                # 绗簩姝ワ細璁$畻闂數涓嬪崟淇″彿
+                has_single, _index, sell_info = cls.__compute_fast_order_begin_pos(code, compute_start_index,
+                                                                                   compute_end_index)
+                fast_msg = None
+                if has_single:
+                    order_begin_pos.mode = OrderBeginPosInfo.MODE_FAST
+                    order_begin_pos.sell_info = sell_info
+                elif _index is not None and _index < 0:
+                    fast_msg = sell_info
+                    # 绗笁姝�: 璁$畻甯歌涔板叆淇″彿
+                    has_single, _index = cls.__compute_order_begin_pos(code, max(
+                        (compute_start_index - continue_count - 1) if new_add else compute_start_index, 0),
+                                                                       continue_count,
+                                                                       compute_end_index)
+                    order_begin_pos.mode = OrderBeginPosInfo.MODE_NORMAL
+                    # 鑾峰彇鏈�鏂扮殑涔颁俊鎭�
+                    sell_info = cls.__L2MarketSellManager.get_current_total_sell_data(code)
+                    order_begin_pos.sell_info = sell_info
             # 濡傛灉涔板叆淇″彿涓庝笂娆$殑涔板叆淇″彿涓�鏍峰氨涓嶈兘绠楁柊鐨勪俊鍙�
             if cls.__last_buy_single_dict.get(code) == _index:
                 has_single = None
@@ -1350,20 +1368,14 @@
                                          f"鑾峰彇鐨勪俊鍙蜂綅鏃犳晥锛堟澘涓婁拱锛岃寖鍥达細{trade_index + 1}-{order_begin_pos.buy_single_index} 鏈垚浜ゆ�绘墜{total_num}/闃堝�納thresh_hold_num}锛�")
                             return None
 
+                # -----------------------------涔板叆璁$畻鍒濆鍖�,璁$畻绾拱棰濋槇鍊�-----------------------
                 cls.__last_buy_single_dict[code] = buy_single_index
                 new_get_single = True
                 order_begin_pos.num = 0
                 order_begin_pos.count = 0
                 order_begin_pos.buy_single_index = buy_single_index
+                # 璋冩暣闂數涓嬪崟鐨勪拱鍏ラ槇鍊�
                 if order_begin_pos.sell_info and order_begin_pos.mode == OrderBeginPosInfo.MODE_FAST:
-                    # k_format = code_nature_analyse.CodeNatureRecordManager().get_k_format_cache(code)
-                    # if k_format and (k_format[1][0] or k_format[3][0]):
-                    #     # 鑲′环鏂伴珮鎴栬�呴�艰繎鍓嶉珮
-                    #     order_begin_pos.threshold_money = int(sell_info[1])
-                    # else:
-                    # if float(total_datas[buy_single_index]["val"]["price"]) >= 3 and cls.volume_rate_info[code][
-                    #     0] > 0.3 and sell_info[1] > 2000 * 10000 and int(
-                    #     tool.get_now_time_str().replace(":", "")) < int("100000"):
                     situation = cls.__MarketSituationManager.get_situation_cache()
                     if sell_info[1] > 2000 * 10000 and situation != MarketSituationManager.SITUATION_GOOD:
                         # 甯傚満琛屾儏濂芥椂涓嶆墦鎶�
@@ -1379,6 +1391,10 @@
                             order_begin_pos.threshold_money = int(sell_info[1] * 0.8)
                     else:
                         order_begin_pos.threshold_money = int(sell_info[1])
+                if order_begin_pos.sell_info and order_begin_pos.mode == OrderBeginPosInfo.MODE_ACTIVE:
+                    # 鎬诲崠棰濈殑1.5鍊�
+                    order_begin_pos.threshold_money = int(sell_info[1] * 1.5)
+
                 l2_log.debug(code, "鑾峰彇鍒颁拱鍏ヤ俊鍙疯捣濮嬬偣锛歿} ,璁$畻鑼冨洿锛歿}-{} 锛岄噺姣旓細{}锛屾槸鍚︽澘涓婁拱锛歿}锛屾暟鎹細{} 妯″紡锛歿}锛坽}锛�", buy_single_index,
                              compute_start_index,
                              compute_end_index, cls.volume_rate_info[code], order_begin_pos.at_limit_up,
@@ -1404,7 +1420,19 @@
 
         # 涔板叆绾拱棰濈粺璁�
         new_buy_exec_index, buy_nums, buy_count, rebegin_buy_pos, max_num_set_new, not_buy_msg = None, None, None, None, [], ""
-        if order_begin_pos.mode == OrderBeginPosInfo.MODE_FAST:
+        if order_begin_pos.mode == OrderBeginPosInfo.MODE_ACTIVE:
+            threshold_money = order_begin_pos.threshold_money
+            new_buy_exec_index, buy_nums, buy_count, rebegin_buy_pos, threshold_money_new, max_num_set_new, not_buy_msg = cls.__sum_buy_num_for_order_active(
+                code,
+                start_process_index,
+                compute_end_index,
+                order_begin_pos.num,
+                order_begin_pos.count,
+                threshold_money,
+                order_begin_pos.buy_single_index, order_begin_pos.max_num_set)
+            threshold_money = threshold_money_new
+            order_begin_pos.threshold_money = threshold_money
+        elif order_begin_pos.mode == OrderBeginPosInfo.MODE_FAST:
 
             threshold_money = order_begin_pos.threshold_money
             new_buy_exec_index, buy_nums, buy_count, rebegin_buy_pos, threshold_money_new, max_num_set_new, not_buy_msg = cls.__sum_buy_num_for_order_fast(
@@ -1604,6 +1632,71 @@
             if is_first_limit_up:
                 return True, i, [refer_sell_data[0], threshold_money]
         return False, None, None
+
+    # 璁$畻婵�杩涗拱鐨勪笅鍗曚俊鍙�
+    @classmethod
+    def __compute_active_order_begin_pos(cls, code, continue_count, start_index, end_index):
+        total_datas = local_today_datas[code]
+        start_time_str = total_datas[start_index]["val"]["time"]
+        # 鑾峰彇鏈�杩戠殑鎬诲崠淇℃伅
+        refer_sell_data = cls.__L2MarketSellManager.get_refer_sell_data(code, start_time_str)
+        if refer_sell_data is None:
+            return False, -1, "鎬诲崠涓虹┖"
+        # 鑾峰彇褰撳墠鏄惁鍙縺杩涗拱
+        # 鑾峰彇鏉垮潡鏄惁鍙互婵�杩涗拱
+        # 鍙拱鐨勬澘鍧�, 鏄惁鐙嫍, 娑堟伅, 鍙拱鐨勫己鍔挎澘鍧�, 鍏抽敭璇�, 婵�杩涗拱鐨勬澘鍧�
+        can_buy_result = CodePlateKeyBuyManager.can_buy(code)
+        if can_buy_result and can_buy_result[0] and can_buy_result[5]:
+            # 鏈夊彲涔版澘鍧楋紝鏈夋縺杩涗拱鏉垮潡
+            # 绗竴姝ワ細 璁$畻鎬诲崠棰�
+            threshold_money = refer_sell_data[1]
+            for i in range(start_index - 1, -1, -1):
+                val = total_datas[i]["val"]
+                if tool.compare_time(val["time"], refer_sell_data[0]) <= 0:
+                    break
+                if L2DataUtil.is_sell(val):
+                    threshold_money += val["num"] * int(float(val["price"]) * 100)
+                elif L2DataUtil.is_sell_cancel(val):
+                    threshold_money -= val["num"] * int(float(val["price"]) * 100)
+            # 绗簩姝ワ細璁$畻璧峰淇″彿
+            second_930 = 9 * 3600 + 30 * 60 + 0
+            total_datas = local_today_datas.get(code)
+            if end_index - start_index + 1 < continue_count:
+                return False, -1, "淇″彿涓嶈繛缁�"
+            last_index = None
+            count = 0
+            start = None
+            for i in range(start_index, end_index + 1):
+                _val = total_datas[i]["val"]
+                time_s = L2DataUtil.get_time_as_second(_val["time"])
+                # 鏃堕棿瑕�>=09:30:00
+                if time_s < second_930:
+                    continue
+                if L2DataUtil.is_limit_up_price_buy(_val):
+                    # 閲戦瑕佸ぇ浜�50涓�
+                    if _val["num"] * float(_val["price"]) < 5000:
+                        continue
+                    if last_index is None or (total_datas[last_index]["val"]["time"] == total_datas[i]["val"]["time"]):
+                        if start is None:
+                            start = i
+                        last_index = i
+                        count += total_datas[i]["re"]
+                        if count >= continue_count:
+                            return True, start, [refer_sell_data[0], threshold_money]
+                    else:
+                        # 鏈潯鏁版嵁浣滀负璧风偣
+                        last_index = i
+                        count = datas[i]["re"]
+                        start = i
+
+                elif not L2DataUtil.is_sell(_val) and not L2DataUtil.is_sell_cancel(_val):
+                    # 鍓旈櫎鍗栦笌鍗栨挙
+                    last_index = None
+                    count = 0
+                    start = None
+            return False, -1, "鏈幏鍙栧埌婵�杩涗拱鐨勮捣濮嬩俊鍙�"
+        else:
+            return False, -1, "涓嶅彲婵�杩涗拱"
 
     @classmethod
     def __get_threshmoney(cls, code):
@@ -1949,6 +2042,134 @@
 
         return None, buy_nums, buy_count, None, threshold_money, max_buy_num_set, not_buy_msg
 
+    # 杩斿洖(涔板叆鎵ц鐐�, 鎬绘墜, 鎬荤瑪鏁�, 浠庢柊璁$畻璧风偣, 绾拱棰濋槇鍊�)
+    # 璁$畻婵�杩涗拱鍏�
+    @classmethod
+    def __sum_buy_num_for_order_active(cls, code, compute_start_index, compute_end_index, origin_num, origin_count,
+                                       threshold_money_origin, buy_single_index, max_num_set):
+        _start_time = t.time()
+        total_datas = local_today_datas[code]
+        # is_first_code = gpcode_manager.FirstCodeManager().is_in_first_record_cache(code)
+
+        buy_nums = origin_num
+        buy_count = origin_count
+        limit_up_price = gpcode_manager.get_limit_up_price(code)
+        if limit_up_price is None:
+            raise Exception("娑ㄥ仠浠锋棤娉曡幏鍙�")
+        limit_up_price = float(limit_up_price)
+
+        threshold_money = threshold_money_origin
+        # 鐩爣鎵嬫暟
+        threshold_num = round(threshold_money / (limit_up_price * 100))
+
+        # buy_single_time_seconds = L2DataUtil.get_time_as_second(total_datas[buy_single_index]["val"]["time"])
+
+        # 鍙互瑙﹀彂涔帮紝褰撴湁娑ㄥ仠涔颁俊鍙锋椂鎵嶄細瑙﹀彂涔�
+        trigger_buy = True
+        # 闂撮殧鏈�澶ф椂闂翠负3s
+        max_space_time_ms = 3 * 1000
+        # 涓嶄笅鍗曠殑淇℃伅
+        not_buy_msg = ""
+        max_buy_num_set = set(max_num_set)
+        for i in range(compute_start_index, compute_end_index + 1):
+            data = total_datas[i]
+            _val = total_datas[i]["val"]
+            trigger_buy = False
+            # 蹇呴』涓鸿繛缁�2绉掑唴鐨勬暟鎹�
+            if L2DataUtil.time_sub_as_ms(_val, total_datas[buy_single_index]["val"]) > max_space_time_ms:
+                cls.__TradePointManager.delete_buy_point(code)
+                if i == compute_end_index:
+                    # 鏁版嵁澶勭悊瀹屾瘯
+                    return None, buy_nums, buy_count, None, threshold_money, max_buy_num_set, f"銆恵i}銆戜俊鍙蜂笉杩炵画锛屽泭鎷椂闂�-{max_space_time_ms}ms"
+                else:
+                    # 璁$畻涔板叆淇″彿锛屼笉鑳藉悓涓�鏃堕棿寮�濮嬭绠�
+                    for ii in range(buy_single_index + 1, compute_end_index + 1):
+                        if total_datas[buy_single_index]["val"]["time"] != total_datas[ii]["val"]["time"]:
+                            return None, buy_nums, buy_count, ii, threshold_money, max_buy_num_set, f"銆恵i}銆戜俊鍙蜂笉杩炵画锛屽泭鎷椂闂�-{max_space_time_ms}ms"
+            if L2DataUtil.is_sell(_val):
+                threshold_money += _val["num"] * int(float(_val["price"]) * 100)
+                threshold_num = round(threshold_money / (limit_up_price * 100))
+            elif L2DataUtil.is_sell_cancel(_val):
+                threshold_money -= _val["num"] * int(float(_val["price"]) * 100)
+                threshold_num = round(threshold_money / (limit_up_price * 100))
+            # 娑ㄥ仠涔�
+            elif L2DataUtil.is_limit_up_price_buy(_val):
+                if l2_data_util.is_big_money(_val):
+                    max_buy_num_set.add(i)
+                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:
+                    l2_log.info(code, logger_l2_trade_buy,
+                                f"{code}鑾峰彇鍒颁拱鍏ユ墽琛岀偣(婵�杩涗笅鍗�)锛歿i} 缁熻绾拱鎵嬫暟锛歿buy_nums} 鐩爣绾拱鎵嬫暟锛歿threshold_num} 缁熻绾拱鍗曟暟锛歿buy_count}")
+            elif L2DataUtil.is_limit_up_price_buy_cancel(_val):
+                # 鍒ゆ柇涔板叆浣嶇疆鏄惁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓�
+                buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(total_datas[i],
+                                                                                                    local_today_buyno_map.get(
+                                                                                                        code))
+                if buy_index is not None:
+
+                    # 鎵惧埌涔版挙鏁版嵁鐨勪拱鍏ョ偣
+                    if buy_index >= buy_single_index:
+                        max_buy_num_set.discard(buy_index)
+                        buy_nums -= int(_val["num"]) * int(data["re"])
+                        buy_count -= int(data["re"])
+                        l2_log.buy_debug(code, "{}鏁版嵁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍚� 鎾や拱绾拱鎵嬫暟锛歿} 鐩爣鎵嬫暟锛歿}", i, buy_nums, threshold_num)
+                    else:
+                        l2_log.buy_debug(code, "{}鏁版嵁鍦ㄤ拱鍏ヤ俊鍙蜂箣鍓嶏紝涔板叆浣嶏細{}", i, buy_index)
+                        if total_datas[buy_single_index]["val"]["time"] == total_datas[buy_index]["val"]["time"]:
+                            # 鍚屼竴绉�,褰撲綔涔板叆淇″彿涔嬪悗澶勭悊
+                            buy_nums -= int(_val["num"]) * int(data["re"])
+                            buy_count -= int(data["re"])
+                            max_buy_num_set.discard(buy_index)
+                            # 澶у崟鎾ら攢
+                            l2_log.buy_debug(code, "{}鏁版嵁涔板叆浣嶄笌棰勪及涔板叆浣嶅湪鍚屼竴绉�", i)
+                else:
+                    # 鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�
+                    l2_log.buy_debug(code, "鏈壘鍒颁拱鎾ゆ暟鎹殑涔板叆鐐�: 浣嶇疆-{} 鏁版嵁-{}", i, data)
+                    buy_nums -= int(_val["num"]) * int(total_datas[i]["re"])
+                    buy_count -= int(total_datas[i]["re"])
+            l2_log.buy_debug(code, "浣嶇疆-{}锛屾�绘墜鏁帮細{}锛岀洰鏍囨墜鏁帮細{}", i,
+                             buy_nums, threshold_num)
+
+            # 璁$畻淇″彿浣嶇疆涔嬪悗鐨勪富鍔ㄥ崠锛屽姞鍏ュ埌闃堝�间箣涓�
+            sell_orders = HuaXinSellOrderStatisticManager.get_latest_transaction_datas(code, min_sell_order_no=int(
+                total_datas[buy_single_index]['val']['orderNo']))
+            sell_order_num = sum([x[1] for x in sell_orders]) // 100
+            # 绾拱棰濊冻澶燂紝涓旂瑪鏁板ぇ浜�2绗�
+            if buy_nums < threshold_num + sell_order_num:
+                not_buy_msg = f"銆恵i}銆戠函涔伴涓嶅锛寋buy_nums}/{threshold_num}"
+                continue
+            if not trigger_buy:
+                not_buy_msg = f"銆恵i}銆戞病鏈変拱鍗曡Е鍙�"
+                continue
+            if buy_count < 2:
+                not_buy_msg = f"銆恵i}銆戝畨鍏ㄧ瑪鏁颁笉瓒筹紝{buy_count}/{2}"
+                continue
+
+            # max_buy_num_set = cls.__filter_not_deal_indexes(code, max_buy_num_set)
+            # big_money_count_threshhold = cls.__l2PlaceOrderParamsManagerDict[code].get_big_num_count()
+            # if len(max_buy_num_set) < big_money_count_threshhold:
+            #     not_buy_msg = f"銆恵i}銆戝ぇ鍗曚笉婊¤冻瑕佹眰锛岄渶瑕侊細{big_money_count_threshhold} 鎬伙細{len(max_buy_num_set)}"
+            #     continue
+
+            try:
+                info = cls.__trade_log_placr_order_info_dict[code]
+                info.set_trade_factor(threshold_money, 0, [])
+            except Exception as e:
+                async_log_util.error(logger_l2_error, f"璁板綍浜ゆ槗鍥犲瓙鍑洪敊锛歿str(e)}")
+            l2_log.buy_debug(code, f"婵�杩涗拱涓诲姩鍗栨墜鏁帮細{sell_order_num}")
+
+            return i, buy_nums, buy_count, None, threshold_money, max_buy_num_set, "鍙互涓嬪崟"
+
+        l2_log.buy_debug(code, "灏氭湭鑾峰彇鍒颁拱鍏ユ墽琛岀偣(婵�杩涗拱鍏�)锛岃捣濮嬭绠椾綅缃細{} 缁熻绾拱鎵嬫暟锛歿} 鐩爣绾拱鎵嬫暟锛歿}  缁熻绾拱鍗曟暟锛歿} ",
+                         compute_start_index,
+                         buy_nums,
+                         threshold_num, buy_count)
+
+        return None, buy_nums, buy_count, None, threshold_money, max_buy_num_set, not_buy_msg
+
 
 def test_trade_record():
     code = "000333"

--
Gitblit v1.8.0