From 97cc7f2d7428ea890c0a0ada76e5bffafd2463e4 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期四, 26 十月 2023 22:56:11 +0800
Subject: [PATCH] L2总卖实现

---
 l2/l2_data_manager.py |  107 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 61 insertions(+), 46 deletions(-)

diff --git a/l2/l2_data_manager.py b/l2/l2_data_manager.py
index 08f1730..73fecc5 100644
--- a/l2/l2_data_manager.py
+++ b/l2/l2_data_manager.py
@@ -12,6 +12,45 @@
 _redisManager = redis_manager.RedisManager(1)
 
 
+# 涓嬪崟涓存椂淇℃伅
+class OrderBeginPosInfo(object):
+    MODE_NORMAL = 0
+    MODE_FAST = 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):
+        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
+
+    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):
     # 浠锋牸涓嶅尮閰�
     CODE_PRICE_ERROR = 1
@@ -56,8 +95,8 @@
         for k in keys:
             code = k.split("-")[-1]
             val = RedisUtils.get(redis_, k)
-            val = json.loads(val)
-            CodeDataCacheUtil.set_cache(cls.__buy_compute_index_info_cache, code, val)
+            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:
@@ -66,7 +105,6 @@
             CodeDataCacheUtil.set_cache(cls.__buy_cancel_single_pos_cache, code, int(val))
 
     # 鍒犻櫎涔板叆鐐规暟鎹�
-
     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))
@@ -82,11 +120,12 @@
         _data = json.loads(_data_json)
         return _data[0], _data[1], _data[2], _data[3], _data[4], _data[5], _data[6]
 
-    def get_buy_compute_start_data_cache(self, 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]
-        return None, None, None, 0, 0, [], 0
+
+        return OrderBeginPosInfo()
 
     # 璁剧疆涔板叆鐐圭殑鍊�
     # buy_single_index 涔板叆淇″彿浣�
@@ -94,51 +133,18 @@
     # compute_index 璁$畻浣嶇疆
     # nums 绱绾拱棰�
 
-    def set_buy_compute_start_data(self, 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):
         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 = self.get_buy_compute_start_data_cache(
-                code)
-            data_ = (_buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),
-                     volume_rate)
+            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, json.dumps(data_))
-
-    # 鑾峰彇鎾や拱鍏ュ紑濮嬭绠楃殑淇℃伅
-    # 杩斿洖鏁版嵁鐨勫唴瀹逛负锛氭挙閿�鐐圭储寮� 鎾や拱绾拱棰� 璁$畻鐨勬暟鎹储寮�
-
-    def get_buy_cancel_single_pos(self, code):
-        info = RedisUtils.get(self.__get_redis(), "buy_cancel_single_pos-{}".format(code))
-        if info is None:
-            return None
-        else:
-            return int(info)
-
-    def get_buy_cancel_single_pos_cache(self, code):
-        cache_result = tool.CodeDataCacheUtil.get_cache(self.__buy_cancel_single_pos_cache, code)
-        if cache_result[0]:
-            return cache_result[1]
-        return None
-
-    # 璁剧疆涔版挙鐐逛俊鎭�
-    # buy_num 绾拱棰�  computed_index璁$畻鍒扮殑涓嬫爣  index鎾や拱淇″彿璧风偣
-
-    def set_buy_cancel_single_pos(self, code, index):
-        tool.CodeDataCacheUtil.set_cache(self.__buy_cancel_single_pos_cache, code, index)
-        expire = tool.get_expire()
-        RedisUtils.setex_async(self.__db, "buy_cancel_single_pos-{}".format(code), expire, index)
-
-    # 鍒犻櫎涔版挙鐐规暟鎹�
-    def delete_buy_cancel_point(self, code):
-        tool.CodeDataCacheUtil.clear_cache(self.__buy_cancel_single_pos_cache, code)
-        RedisUtils.delete_async(self.__db, "buy_cancel_single_pos-{}".format(code))
+        RedisUtils.setex_async(self.__db, _key, expire, data_.to_json_str())
 
 
 # 娓呴櫎l2鏁版嵁
@@ -191,4 +197,13 @@
 
 
 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