From 4062c0d9d4b7bc8e1d9e866a86c98db5c51f4fa0 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 15 十一月 2023 16:12:47 +0800
Subject: [PATCH] K线形态加入计算

---
 code_attribute/code_nature_analyse.py |  122 +++++++++++++++++++++-------------------
 1 files changed, 65 insertions(+), 57 deletions(-)

diff --git a/code_attribute/code_nature_analyse.py b/code_attribute/code_nature_analyse.py
index 89a63b3..86f126f 100644
--- a/code_attribute/code_nature_analyse.py
+++ b/code_attribute/code_nature_analyse.py
@@ -18,58 +18,83 @@
 
 
 class CodeNatureRecordManager:
-    __redisManager = RedisManager(0)
     __k_format_cache = {}
     __nature_cache = {}
 
+    __db = 0
+    __instance = None
+    __redis_manager = redis_manager.RedisManager(0)
+
+    def __new__(cls, *args, **kwargs):
+        if not cls.__instance:
+            cls.__instance = super(CodeNatureRecordManager, cls).__new__(cls, *args, **kwargs)
+            cls.__load_datas()
+        return cls.__instance
+
     @classmethod
     def __get_redis(cls):
-        return cls.__redisManager.getRedis()
+        return cls.__redis_manager.getRedis()
+
+    @classmethod
+    def __load_datas(cls):
+        __redis = cls.__get_redis()
+        try:
+            keys = RedisUtils.keys(__redis, "k_format-*")
+            for k in keys:
+                code = k.split("-")[1]
+                val = RedisUtils.get(__redis, k)
+                val = json.loads(val)
+                cls.__k_format_cache[code] = val
+            keys = RedisUtils.keys(__redis, "code_nature-*")
+            for k in keys:
+                code = k.split("-")[1]
+                val = RedisUtils.get(__redis, k)
+                val = json.loads(val)
+                cls.__nature_cache[code] = val
+        except:
+            pass
+        finally:
+            RedisUtils.realse(__redis)
 
     # 淇濆瓨K绾垮舰鎬�
-    @classmethod
-    def save_k_format(cls, code, k_format):
-        RedisUtils.setex(cls.__get_redis(), f"k_format-{code}", tool.get_expire(), json.dumps(k_format))
+    def save_k_format(self, code, k_format):
+        self.__k_format_cache[code] = k_format
+        RedisUtils.setex_async(self.__db, f"k_format-{code}", tool.get_expire(), json.dumps(k_format))
 
-    @classmethod
-    def get_k_format(cls, code):
-        val = RedisUtils.get(cls.__get_redis(), f"k_format-{code}")
+    def get_k_format(self, code):
+        val = RedisUtils.get(self.__get_redis(), f"k_format-{code}")
         if val:
             return json.loads(val)
         return None
 
-    @classmethod
-    def get_k_format_cache(cls, code):
-        val = None
-        if code in cls.__k_format_cache:
-            val = cls.__k_format_cache[code]
-        if not val:
-            val = cls.get_k_format(code)
-            if val:
-                cls.__k_format_cache[code] = val
+    def get_k_format_cache(self, code):
+        val = self.__k_format_cache.get(code)
         # 澶嶅埗
         return copy.deepcopy(val) if val else None
 
-    # 淇濆瓨鑲℃��
-    @classmethod
-    def save_nature(cls, code, natures):
-        RedisUtils.setex(cls.__get_redis(), f"code_nature-{code}", tool.get_expire(), json.dumps(natures))
+    def clear(self):
+        self.__k_format_cache.clear()
+        self.__nature_cache.clear()
+        keys = RedisUtils.keys(self.__get_redis(), "k_format-*")
+        for k in keys:
+            RedisUtils.delete(self.__get_redis(), k)
+        keys = RedisUtils.keys(self.__get_redis(), "code_nature-*")
+        for k in keys:
+            RedisUtils.delete(self.__get_redis(), k)
 
-    @classmethod
-    def get_nature(cls, code):
-        val = RedisUtils.get(cls.__get_redis(), f"code_nature-{code}")
+    # 淇濆瓨鑲℃��
+
+    def save_nature(self, code, natures):
+        RedisUtils.setex_async(self.__db, f"code_nature-{code}", tool.get_expire(), json.dumps(natures))
+
+    def get_nature(self, code):
+        val = RedisUtils.get(self.__get_redis(), f"code_nature-{code}")
         if val:
             return json.loads(val)
         return None
 
-    @classmethod
-    def get_nature_cache(cls, code):
-        if code in cls.__nature_cache:
-            return cls.__nature_cache[code]
-        val = cls.get_nature(code)
-        if val:
-            cls.__nature_cache[code] = val
-        return val
+    def get_nature_cache(self, code):
+        return self.__nature_cache.get(code)
 
 
 class LatestMaxVolumeManager:
@@ -344,33 +369,17 @@
     datas = copy.deepcopy(datas)
     datas.sort(key=lambda x: x["bob"])
     datas = datas[-80:]
-    max_volume = 0
-    max_volume_index = 0
 
+    max_price = 0
+    max_price_index = 0
     for index in range(0, len(datas)):
         data = datas[index]
-        if max_volume < data["volume"]:
-            max_volume = data["volume"]
-            max_volume_index = index
-
-    price = 0
-    price_index = 0
-    for index in range(max_volume_index, len(datas)):
-        data = datas[index]
-        if data["high"] > price:
-            price = data["high"]
-            price_index = index
-
-    index = price_index
-    # 鏈�澶ч噺褰撴棩鏈�楂樹环姣斿綋鏃ヤ箣鍚庣殑鏈�楂樹环娑ㄥ箙鍦�15%浠ュ唴
-    if (price - datas[max_volume_index]["high"]) / datas[max_volume_index]["high"] < 0.15:
-        price = datas[max_volume_index]["high"]
-        index = max_volume_index
-
-    print(max_volume)
-    rate = (price - limit_up_price) / limit_up_price
-    if 0 < rate < 0.03:
-        return True, datas[index]['bob'].strftime("%Y-%m-%d")
+        if data["high"] > max_price:
+            max_price = data["high"]
+            max_price_index = index
+    rate = (max_price - float(limit_up_price)) / max_price
+    if 0 < rate < 0.02:
+        return True, datas[max_price_index]['bob'].strftime("%Y-%m-%d")
     return False, ''
 
 
@@ -541,4 +550,3 @@
     HighIncreaseCodeManager().add_code("000333")
     print(HighIncreaseCodeManager().is_in("000333"))
     print(HighIncreaseCodeManager().is_in("000222"))
-

--
Gitblit v1.8.0