From 365491c1fcf523994035e4bd28d8b5872dd6ec98 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期四, 31 七月 2025 14:47:48 +0800
Subject: [PATCH] 除权采用掘金更新K线

---
 l2/l2_data_manager.py |  228 ++++++++++++++++++++++++++++++++------------------------
 1 files changed, 129 insertions(+), 99 deletions(-)

diff --git a/l2/l2_data_manager.py b/l2/l2_data_manager.py
index 8b46f7d..bbbe0ef 100644
--- a/l2/l2_data_manager.py
+++ b/l2/l2_data_manager.py
@@ -3,13 +3,63 @@
 """
 import json
 
-from db import redis_manager
-from db.redis_manager import RedisUtils
+from db import redis_manager_delegate as redis_manager
+from db.redis_manager_delegate import RedisUtils
 from utils import tool
-from log_module.log import logger_l2_trade_buy
 from utils.tool import CodeDataCacheUtil
 
+_db = 1
 _redisManager = redis_manager.RedisManager(1)
+
+
+# 涓嬪崟涓存椂淇℃伅
+class OrderBeginPosInfo(object):
+    MODE_NORMAL = 0  # 鏅�氫笅鍗�
+    MODE_FAST = 1
+    MODE_ACTIVE = 2  # 绉瀬鍔ㄤ拱
+    MODE_RADICAL = 3  # 鎵叆
+    MODE_OPEN_LIMIT_UP = 4  # 鎺�1
+
+    # mode: 0-鏅�氫氦鏄�  1-蹇�熶氦鏄�
+    def __init__(self, buy_single_index=None, buy_exec_index=-1, buy_compute_index=None, num=0, count=0,
+                 max_num_set=None, buy_volume_rate=None, sell_info=None, threshold_money=None, mode=0, mode_desc=None,
+                 at_limit_up=False, first_limit_up_buy=False, min_order_no = None):
+        self.buy_single_index = buy_single_index
+        self.buy_exec_index = buy_exec_index
+        self.buy_compute_index = buy_compute_index
+        self.num = num
+        self.count = count
+        self.threshold_money = threshold_money
+        if max_num_set:
+            self.max_num_set = list(max_num_set)
+        else:
+            self.max_num_set = []
+        self.buy_volume_rate = buy_volume_rate
+        self.sell_info = sell_info
+        self.mode = mode
+        self.mode_desc = mode_desc
+        # 鏄惁鏄澘涓婁拱
+        self.at_limit_up = at_limit_up
+        # 鏄惁涓洪灏佷拱
+        self.first_limit_up_buy = first_limit_up_buy
+        # 缁熻鎵规澶у崟鎴愪氦鐨勬渶灏忚鍗曞彿
+        self.min_order_no = min_order_no
+
+    def get_max_num_set(self):
+        if self.max_num_set:
+            return set(self.max_num_set)
+        return None
+
+    def to_json_str(self):
+        return json.dumps(vars(self))
+
+    def to_dict(self):
+        return vars(self)
+
+    @classmethod
+    def to_object(cls, json_str: str):
+        d = json.loads(json_str)
+        return OrderBeginPosInfo(**d)
 
 
 class L2DataException(Exception):
@@ -32,128 +82,99 @@
 
 # 浜ゆ槗鐐圭鐞嗗櫒锛岀敤浜庣鐞嗕拱鍏ョ偣锛涗拱鎾ょ偣锛涜窛绂讳拱鍏ョ偣鐨勫噣涔板叆鏁版嵁锛涜窛绂讳拱鎾ょ偣鐨勪拱鎾ゆ暟鎹�
 class TradePointManager:
+    __db = 1
+    __redisManager = redis_manager.RedisManager(1)
     __buy_compute_index_info_cache = {}
+    __buy_cancel_single_pos_cache = {}
+    __instance = None
+    # 鏈�杩戠殑涓嬪崟妯″紡
+    __latest_place_order_mode_cache = {}
 
-    @staticmethod
-    def __get_redis():
-        return _redisManager.getRedis()
+    def __new__(cls, *args, **kwargs):
+        if not cls.__instance:
+            cls.__instance = super(TradePointManager, cls).__new__(cls, *args, **kwargs)
+            cls.load_data()
+
+        return cls.__instance
+
+    @classmethod
+    def __get_redis(cls):
+        return cls.__redisManager.getRedis()
+
+    @classmethod
+    def load_data(cls):
+        redis_ = cls.__get_redis()
+        keys = RedisUtils.keys(redis_, "buy_compute_index_info-*")
+        for k in keys:
+            code = k.split("-")[-1]
+            val = RedisUtils.get(redis_, k)
+            order = OrderBeginPosInfo.to_object(val)
+            CodeDataCacheUtil.set_cache(cls.__buy_compute_index_info_cache, code, order)
+
+        keys = RedisUtils.keys(redis_, "buy_cancel_single_pos-*")
+        for k in keys:
+            code = k.split("-")[-1]
+            val = RedisUtils.get(redis_, k)
+            CodeDataCacheUtil.set_cache(cls.__buy_cancel_single_pos_cache, code, int(val))
 
     # 鍒犻櫎涔板叆鐐规暟鎹�
-    @staticmethod
-    def delete_buy_point(code):
-        CodeDataCacheUtil.clear_cache(TradePointManager.__buy_compute_index_info_cache, code)
-        RedisUtils.delete_async(TradePointManager.__get_redis(), "buy_compute_index_info-{}".format(code))
+    def delete_buy_point(self, code):
+        CodeDataCacheUtil.clear_cache(self.__buy_compute_index_info_cache, code)
+        RedisUtils.delete_async(self.__db, "buy_compute_index_info-{}".format(code))
 
     # 鑾峰彇涔板叆鐐逛俊鎭�
     # 杩斿洖鏁版嵁涓猴細涔板叆鐐� 绱绾拱棰� 宸茬粡璁$畻鐨勬暟鎹储寮�
-    @staticmethod
-    def get_buy_compute_start_data(code):
+    def get_buy_compute_start_data(self, code):
         _key = "buy_compute_index_info-{}".format(code)
-        _data_json = RedisUtils.get(TradePointManager.__get_redis(), _key)
+        _data_json = RedisUtils.get(self.__get_redis(), _key)
         if _data_json is None:
             return None, None, None, 0, 0, [], 0
         _data = json.loads(_data_json)
         return _data[0], _data[1], _data[2], _data[3], _data[4], _data[5], _data[6]
 
-    @staticmethod
-    def get_buy_compute_start_data_cache(code):
-        cache_result = CodeDataCacheUtil.get_cache(TradePointManager.__buy_compute_index_info_cache, code)
+    def get_buy_compute_start_data_cache(self, code) -> OrderBeginPosInfo:
+        cache_result = CodeDataCacheUtil.get_cache(self.__buy_compute_index_info_cache, code)
         if cache_result[0]:
             return cache_result[1]
-        val = TradePointManager.get_buy_compute_start_data(code)
-        CodeDataCacheUtil.set_cache(TradePointManager.__buy_compute_index_info_cache, code, val)
-        return val
+
+        return OrderBeginPosInfo()
 
     # 璁剧疆涔板叆鐐圭殑鍊�
     # buy_single_index 涔板叆淇″彿浣�
     # buy_exec_index 涔板叆鎵ц浣�
     # compute_index 璁$畻浣嶇疆
     # nums 绱绾拱棰�
-    @staticmethod
-    def set_buy_compute_start_data(code, buy_single_index, buy_exec_index, compute_index, nums, count, max_num_sets,
-                                   volume_rate):
+
+    def set_buy_compute_start_data_v2(self, code, order: OrderBeginPosInfo):
+
+        if order.mode is not None:
+            self.__latest_place_order_mode_cache[code] = order.mode
+
         expire = tool.get_expire()
         _key = "buy_compute_index_info-{}".format(code)
         data_ = None
-        if buy_single_index is not None:
-            data_ = (buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),
-                     volume_rate)
-
+        if order.buy_single_index is not None:
+            data_ = order
         else:
-            _buy_single_index, _buy_exec_index, _compute_index, _nums, _count, _max_num_index, _volume_rate = TradePointManager.get_buy_compute_start_data_cache(
-                code)
-            data_ = (_buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),
-                     volume_rate)
-        CodeDataCacheUtil.set_cache(TradePointManager.__buy_compute_index_info_cache, code, data_)
-        RedisUtils.setex_async(
-            TradePointManager.__get_redis(), _key, expire,
-            json.dumps(data_))
+            old_order = self.get_buy_compute_start_data_cache(code)
+            order.buy_single_index = old_order.buy_single_index
+            data_ = order
+        CodeDataCacheUtil.set_cache(self.__buy_compute_index_info_cache, code, data_)
+        RedisUtils.setex_async(self.__db, _key, expire, data_.to_json_str())
 
-    # 鑾峰彇鎾や拱鍏ュ紑濮嬭绠楃殑淇℃伅
-    # 杩斿洖鏁版嵁鐨勫唴瀹逛负锛氭挙閿�鐐圭储寮� 鎾や拱绾拱棰� 璁$畻鐨勬暟鎹储寮�
-    @staticmethod
-    def get_buy_cancel_single_pos(code):
-        info = RedisUtils.get(TradePointManager.__get_redis(), "buy_cancel_single_pos-{}".format(code))
-        if info is None:
-            return None
-        else:
-            return int(info)
-
-    # 璁剧疆涔版挙鐐逛俊鎭�
-    # buy_num 绾拱棰�  computed_index璁$畻鍒扮殑涓嬫爣  index鎾や拱淇″彿璧风偣
+    # 鏄惁宸茬粡涓嬪崟
+    @classmethod
+    def is_placed_order(cls, order_begin_pos: OrderBeginPosInfo):
+        return order_begin_pos and order_begin_pos.buy_exec_index is not None and order_begin_pos.buy_exec_index > -1
 
     @classmethod
-    def set_buy_cancel_single_pos(cls, code, index):
-        expire = tool.get_expire()
-        RedisUtils.setex(TradePointManager.__get_redis(), "buy_cancel_single_pos-{}".format(code), expire, index)
-
-    # 鍒犻櫎涔版挙鐐规暟鎹�
-    @classmethod
-    def delete_buy_cancel_point(cls, code):
-        RedisUtils.delete_async(TradePointManager.__get_redis(), "buy_cancel_single_pos-{}".format(code))
-
-    # 璁剧疆涔版挙绾拱棰�
-    @classmethod
-    def set_compute_info_for_cancel_buy(cls, code, index, nums):
-        expire = tool.get_expire()
-        RedisUtils.setex(TradePointManager.__get_redis(), "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):
-        info = RedisUtils.get(TradePointManager.__get_redis(), "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):
-        RedisUtils.delete_async(TradePointManager.__get_redis(), "compute_info_for_cancel_buy-{}".format(code))
-
-    # 浠庝拱鍏ヤ俊鍙峰紑濮嬭缃定鍋滀拱涓庢定鍋滄挙鐨勫崟鏁�
-    @classmethod
-    def set_count_info_for_cancel_buy(cls, code, index, buy_count, cancel_count):
-        expire = tool.get_expire()
-        RedisUtils.setex(TradePointManager.__get_redis(), "count_info_for_cancel_buy-{}".format(code), expire,
-                         json.dumps((index, buy_count, cancel_count)))
-        logger_l2_trade_buy.info("{}淇濆瓨鎾ゅ崟绾拱棰濅俊鎭細{}锛寋}", code, index, buy_count, cancel_count)
-
-    # 鑾峰彇涔版挙绾拱棰濊绠椾俊鎭�
-    @classmethod
-    def get_count_info_for_cancel_buy(cls, code):
-        info = RedisUtils.get(TradePointManager.__get_redis(), "count_info_for_cancel_buy-{}".format(code))
-        if info is None:
-            return None, 0, 0
-        else:
-            info = json.loads(info)
-            return info[0], info[1], info[2]
-
-    @classmethod
-    def delete_count_info_for_cancel_buy(cls, code):
-        RedisUtils.delete_async(TradePointManager.__get_redis(), "count_info_for_cancel_buy-{}".format(code))
+    def get_latest_place_order_mode(cls, code):
+        """
+        鑾峰彇鏈�杩戜笅鍗曠殑妯″紡
+        @param code:
+        @return:
+        """
+        return cls.__latest_place_order_mode_cache.get(code)
 
 
 # 娓呴櫎l2鏁版嵁
@@ -202,8 +223,17 @@
 # 鏄惁鍦╨2鍥哄畾鐩戞帶浠g爜涓�
 def is_in_l2_fixed_codes(code):
     key = "l2-fixed-codes"
-    return RedisUtils.sismember( _redisManager.getRedis(), key, code)
+    return RedisUtils.sismember(_redisManager.getRedis(), key, code)
 
 
 if __name__ == "__main__":
-    TradePointManager.get_buy_compute_start_data_cache("603912")
+    code = "002886"
+    TradePointManager().set_buy_compute_start_data_v2(code, OrderBeginPosInfo(buy_single_index=10,
+                                                                              buy_exec_index=30,
+                                                                              buy_compute_index=40,
+                                                                              num=20000, count=10,
+                                                                              buy_volume_rate=0.6,
+                                                                              mode=OrderBeginPosInfo.MODE_NORMAL,
+                                                                              ))
+    print(TradePointManager().get_buy_compute_start_data_cache(code).max_num_set)
+    RedisUtils.run_loop()

--
Gitblit v1.8.0