From 3cfa1332c0807a74b4ac5a2150500841f5299147 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 08 三月 2023 22:54:09 +0800
Subject: [PATCH] 首板加入,安全笔数与H撤笔数优化

---
 juejin.py |  116 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 92 insertions(+), 24 deletions(-)

diff --git a/juejin.py b/juejin.py
index 8388cec..b082ce0 100644
--- a/juejin.py
+++ b/juejin.py
@@ -72,6 +72,12 @@
     # 杞藉叆閲�
     global_data_loader.load_volumn()
 
+    # 9鐐�25涔嬪墠鍒犻櫎鎵�鏈変唬鐮�
+    if tool.trade_time_sub(tool.get_now_time_str(), "09:25:00") <= 0:
+        gpcode_manager.clear_listen_codes()
+
+    # TODO 鍒犻櫎鎵�鏈夐鏉夸唬鐮�
+
 
 # 姣忔棩鍒濆鍖�
 def everyday_init():
@@ -152,8 +158,8 @@
     for c in clients:
         for i in range(0, 8):
             gpcode_manager.init_listen_code_by_pos(int(c), i)
-    codes = gpcode_manager.get_gp_list();
-    result = JueJinManager.get_gp_latest_info(codes);
+    codes = gpcode_manager.get_gp_list()
+    result = JueJinManager.get_gp_latest_info(codes)
     for item in result:
         sec_level = item['sec_level']
         symbol = item['symbol']
@@ -255,7 +261,11 @@
 
 # 鑾峰彇鍒扮幇浠�
 def accept_prices(prices):
+    # 鑾峰彇棣栨澘浠g爜
+    first_codes = gpcode_manager.get_first_gp_codes()
+
     print("浠锋牸浠g爜鏁伴噺锛�", len(prices))
+
     __actualPriceProcessor.save_current_price_codes_count(len(prices))
     # 閲囬泦鐨勪唬鐮佹暟閲忎笉瀵�
     if len(gpcode_manager.get_gp_list()) - len(prices) > 2:
@@ -278,6 +288,8 @@
             pricePre = gpcode_manager.get_price_pre(code)
             if pricePre is not None:
                 rate = round((price - pricePre) * 100 / pricePre, 2)
+                if first_codes and code in first_codes:
+                    rate = rate / 2
                 if rate >= 0:
                     # 鏆傚瓨娑ㄥ箙涓烘鐨勪唬鐮�
                     _code_list.append((rate, code))
@@ -297,8 +309,9 @@
                 except Exception as e:
                     logging.exception(e)
 
+        # -------------------------------澶勭悊浜ゆ槗浣嶇疆鍒嗛厤---------------------------------
         # 鎺掑簭
-        new_code_list = sorted(_code_list, key=lambda e: e.__getitem__(0), reverse=True)
+        new_code_list = sorted(_code_list, key=lambda e: (e.__getitem__(0), e.__getitem__(1)), reverse=True)
         # 棰勫~鍏呬笅鍗曚唬鐮�
         _buy_win_codes = []
         for d in new_code_list:
@@ -311,40 +324,46 @@
             logging.exception(e)
             pass
 
+        # -------------------------------澶勭悊L2鐩戝惉---------------------------------
+
         client_ids = client_manager.getValidL2Clients()
         # 鏈�澶氬~鍏呯殑浠g爜鏁伴噺
-        max_count = len(client_ids) * 8
+        max_count = len(client_ids) * constant.L2_CODE_COUNT_PER_DEVICE
         if max_count == 0:
-            max_count = 8
+            max_count = constant.L2_CODE_COUNT_PER_DEVICE
+
+        _delete_list = []
+        for item in new_code_list:
+            if l2_trade_util.is_in_forbidden_trade_codes(item[1]) or item[0] < 0:
+                _delete_list.append(item)
+
+        for item in _delete_list:
+            new_code_list.remove(item)
         # 鎴彇鍓嶅嚑涓唬鐮佸~鍏�
         add_list = new_code_list[:max_count]
         # 鍚庨潰鐨勪唬鐮佸叏閮ㄥ垹闄�
         _delete_list.extend(new_code_list[max_count:])
 
         add_code_list = []
-        del_list = []
+        del_code_list = []
         for d in add_list:
             add_code_list.append(d[1])
 
         for d in _delete_list:
-            del_list.append(d[1])
+            del_code_list.append(d[1])
 
         # 鍚庨潰鐨勪唬鐮佹暟閲�
         # 鍏堝垹闄ゅ簲璇ュ垹闄ょ殑浠g爜
-        for code in del_list:
+        for code in del_code_list:
             if gpcode_manager.is_listen_old(code):
                 # 鍒ゆ柇鏄惁鍦ㄧ洃鍚噷闈�
                 L2CodeOperate.get_instance().add_operate(0, code, "鐜颁环鍙樺寲")
         # 澧炲姞搴旇澧炲姞鐨勪唬鐮�
         for code in add_code_list:
             if not gpcode_manager.is_listen_old(code):
-                if not l2_trade_util.is_in_forbidden_trade_codes(code):
-                    L2CodeOperate.get_instance().add_operate(1, code, "鐜颁环鍙樺寲")
-            else:
-                if l2_trade_util.is_in_forbidden_trade_codes(code):
-                    L2CodeOperate.get_instance().add_operate(0, code, "鐜颁环鍙樺寲")
+                L2CodeOperate.get_instance().add_operate(1, code, "鐜颁环鍙樺寲")
 
-        print(add_code_list, del_list)
+        print(add_code_list, del_code_list)
 
 
 def on_bar(context, bars):
@@ -393,8 +412,8 @@
         t1.setDaemon(True)
         t1.start()
 
-    @staticmethod
-    def get_gp_latest_info(codes):
+    @classmethod
+    def get_gp_latest_info(cls, codes):
         account_id, s_id, token = getAccountInfo()
         symbols = gpcode_manager.get_gp_list_with_prefix(codes)
         gmapi.set_token(token)
@@ -402,8 +421,8 @@
         print(data)
         return data
 
-    @staticmethod
-    def get_now_price(codes):
+    @classmethod
+    def get_now_price(cls, codes):
         data = JueJinManager.get_gp_current_info(codes)
         prices = []
         for item in data:
@@ -413,8 +432,8 @@
         return prices
 
     # 鑾峰彇浠g爜鐨勬定骞�
-    @staticmethod
-    def get_codes_limit_rate(codes):
+    @classmethod
+    def get_codes_limit_rate(cls, codes):
         datas = JueJinManager.get_gp_latest_info(codes)
         pre_price_dict = {}
         for data in datas:
@@ -434,14 +453,41 @@
         f_results.reverse()
         return f_results
 
-    @staticmethod
-    def get_gp_current_info(codes):
+    @classmethod
+    def get_history_tick_n(cls, code, count):
+        account_id, s_id, token = getAccountInfo()
+        symbols = gpcode_manager.get_gp_list_with_prefix([code])
+        gmapi.set_token(token)
+        results = gmapi.history_n(symbol=symbols[0], frequency="1d", count=count)
+        return results
+
+    @classmethod
+    def get_lowest_price_rate(cls, code, count):
+        datas = cls.get_history_tick_n(code, count)
+        low_price = datas[0]["close"]
+        for data in datas:
+            if low_price > data["close"]:
+                low_price = data["close"]
+        return (datas[-1]["close"] - low_price) / low_price
+
+    @classmethod
+    def get_gp_current_info(cls, codes):
         account_id, s_id, token = getAccountInfo()
         symbols = gpcode_manager.get_gp_list_with_prefix(codes)
         gmapi.set_token(token)
         data = gmapi.current(symbols=",".join(symbols))
         print(data)
         return data
+
+    @classmethod
+    def get_gp_codes_names(cls, codes):
+        datas = cls.get_gp_latest_info(codes)
+        results = {}
+        for data in datas:
+            code = data["symbol"].split(".")[1]
+            code_name = data['sec_name']
+            results[code] = code_name
+        return results
 
     def start(self):
         account_id, s_id, token = getAccountInfo()
@@ -507,9 +553,30 @@
     return _fresult
 
 
+# 鑾峰彇杩�90澶╃殑鏈�澶ч噺涓庢渶杩戠殑閲�
+def get_volumn(code) -> object:
+    end = datetime.datetime.now()
+    account_id, s_id, token = getAccountInfo()
+    gmapi.set_token(token)
+    gmapi.set_account_id(account_id)
+    results = gmapi.history_n(symbol=gpcode_manager.get_gp_list_with_prefix([code])[0], frequency="1d",
+                              count=60,
+                              fields="volume",
+                              end_time=end)
+    if not results:
+        return None
+    yes_volume = results[-1]["volume"]
+    max_volume = results[0]["volume"]
+    for result in results:
+        volumn = int(result["volume"])
+        if volumn > max_volume:
+            max_volume = volumn
+    return (max_volume, yes_volume)
+
+
 # 鏍规嵁娑ㄥ箙楂樹綆鍒嗛厤浜ゆ槗绐楀彛
 def distribute_buy_win():
-    if tool.trade_time_sub(tool.get_now_time_str(),"09:30:00") > 0:
+    if tool.trade_time_sub(tool.get_now_time_str(), "09:30:00") > 0:
         raise Exception("鍙兘9:30涔嬪墠閲嶆柊鍒嗛厤绐楀彛")
 
     datas = JueJinManager.get_codes_limit_rate(gpcode_manager.get_gp_list())
@@ -519,4 +586,5 @@
 
 
 if __name__ == '__main__':
-    distribute_buy_win()
+    print(get_volumn("002115"))
+    print(JueJinManager.get_lowest_price_rate("002713", 15))

--
Gitblit v1.8.0