From 6e71fbcb119e7068ba35380edaa5cc66e7c71f1b Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期四, 27 十月 2022 16:21:05 +0800
Subject: [PATCH] 交易体系完善

---
 trade_data_manager.py |   84 +++++++++++++++++++++++++++++++++++++++---
 1 files changed, 78 insertions(+), 6 deletions(-)

diff --git a/trade_data_manager.py b/trade_data_manager.py
index 0783db4..b150d7b 100644
--- a/trade_data_manager.py
+++ b/trade_data_manager.py
@@ -2,11 +2,13 @@
 浜ゆ槗鏁版嵁鑲¢偅閲屽櫒
 鐢ㄤ簬瀵逛氦鏄撲复鏃舵暟鎹紙浜ゆ槗鐘舵�侊紝浠g爜鐘舵�佺瓑锛夎繘琛岀鐞�
 """
-
+import datetime
 import json
 import time
 
 # 浜ゆ槗鎾ら攢鏁版嵁绠$悊鍣�
+import constant
+import global_util
 import l2_data_util
 import redis_manager
 import tool
@@ -70,7 +72,7 @@
         redis = cls.redisManager.getRedis()
         val_str = redis.get("buy_position_info-{}".format(code))
         if val_str is None:
-            return None, None, None,None
+            return None, None, None, None
         else:
             val = json.loads(val_str)
             return val[0], val[1], val[2], val[3]
@@ -137,7 +139,7 @@
             # 闂撮殧2s鍙婂叾浠ヤ笂琛ㄧず鏁版嵁寮傚父
             # 闂撮殧2s浠ヤ笂鐨勫氨浠ヤ笅鍗曟椂闂翠笅涓�绉掓湯灏句綔涓虹‘璁ょ偣
             start_index = l2_data_index
-            if len(l2_today_datas)-1 > start_index:
+            if len(l2_today_datas) - 1 > start_index:
                 for i in range(start_index + 1, len(l2_today_datas)):
                     _time = l2_today_datas[i]["val"]["time"]
                     if l2_data_util.get_time_as_seconds(_time) - old_time_int >= 2:
@@ -149,7 +151,7 @@
                 cls.__set_buy_sure_position(code, l2_data_index, l2_data)
         elif new_time_int - old_time_int >= 0:
             # 闂撮殧2s鍐呰〃绀烘暟鎹甯�,灏嗗叾浣嶇疆璁剧疆涓烘柊澧炴暟鎹殑涓棿浣嶇疆
-            index = len(l2_today_datas)-1 - (len(l2_add_datas)) // 2
+            index = len(l2_today_datas) - 1 - (len(l2_add_datas)) // 2
             data = l2_today_datas[index]
             cls.__set_buy_sure_position(code, index, data)
         else:
@@ -158,6 +160,76 @@
             pass
 
 
+# 浠g爜瀹炴椂浠锋牸绠$悊鍣�
+class CodeActualPriceProcessor:
+    __redisManager = redis_manager.RedisManager(0)
+
+    def __get_redis(self):
+        return self.__redisManager.getRedis()
+
+    # 淇濆瓨璺屼环鐨勬椂闂�
+    def __save_down_price_time(self, code, time_str):
+        key = "under_water_last_time-{}".format(code)
+        self.__get_redis().setex(key, tool.get_expire(), time_str)
+
+    def __remove_down_price_time(self, code):
+        key = "under_water_last_time-{}".format(code)
+        self.__get_redis().delete(key)
+
+    def __get_last_down_price_time(self, code):
+        key = "under_water_last_time-{}".format(code)
+        return self.__get_redis().get(key)
+
+    def __increment_down_price_time(self, code, seconds):
+        key = "under_water_seconds-{}".format(code)
+        self.__get_redis().incrby(key, seconds)
+
+    def __get_down_price_time_as_seconds(self, code):
+        key = "under_water_seconds-{}".format(code)
+        val = self.__get_redis().get(key)
+        if val is None:
+            return None
+        else:
+            return int(val)
+
+    def process_rate(self, code, rate, time_str):
+        # 9鐐瑰崐涔嬪墠鐨勬暟鎹笉澶勭悊
+        if int(time_str.replace(":", "")) < int("093000"):
+            return
+        # now_str = datetime.datetime.now().strftime("%H:%M:%S")
+        if rate >= 0:
+            down_start_time = self.__get_last_down_price_time(code)
+            if down_start_time is None:
+                return
+            else:
+                # 绱澧炲姞鏃堕棿
+                time_second = tool.trade_time_sub(time_str, down_start_time)
+                self.__increment_down_price_time(code, time_second)
+                # 鍒犻櫎璧峰鏃堕棿
+                self.__remove_down_price_time(code)
+        else:
+            # 璁板綍寮�濮嬪��
+            if self.__get_last_down_price_time(code) is None:
+                self.__save_down_price_time(code, time_str)
+
+    # 淇濆瓨鐜颁环
+    def save_current_price(self, code, price, is_limit_up):
+        global_util.cuurent_prices[code] = (price, is_limit_up, round(time.time()))
+        pass
+
+    # 鏄惁涓烘按涓嬫崬
+    def is_under_water(self, code):
+        time_seconds = self.__get_down_price_time_as_seconds(code)
+        if time_seconds is None:
+            return False
+        else:
+            return time_seconds >= constant.UNDER_WATER_PRICE_TIME_AS_SECONDS
+
+
 if __name__ == "__main__":
-    TradeBuyDataManager.set_buy_capture_time("123456", 178938828, 1232)
-    print(TradeBuyDataManager.get_buy_capture_time("123456"))
+    processor = CodeActualPriceProcessor()
+    processor.process_rate("123456", -0.2, "09:30:00")
+    processor.process_rate("123456", -0.3, "09:40:00")
+    processor.process_rate("123456", 0.3, "09:50:00")
+
+    processor.is_under_water("123456")

--
Gitblit v1.8.0