From cbe19ea6066a600cbd0b5110db5d43f8252d14a8 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期四, 13 六月 2024 11:23:53 +0800
Subject: [PATCH] L撤成交进度相关改进

---
 third_data/history_k_data_util.py |   88 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 77 insertions(+), 11 deletions(-)

diff --git a/third_data/history_k_data_util.py b/third_data/history_k_data_util.py
index 7f014eb..c29f6f7 100644
--- a/third_data/history_k_data_util.py
+++ b/third_data/history_k_data_util.py
@@ -4,13 +4,17 @@
 import datetime
 import decimal
 import json
+import time
 
 import requests
 
 import constant
 from db.redis_manager_delegate import RedisUtils
+from log_module.log import logger_request_api
 from utils import tool, middle_api_protocol
 from db import redis_manager_delegate as redis_manager
+
+
 # import gm.api as gmapi
 
 
@@ -29,7 +33,14 @@
         fdata = None
         if DELEGATE:
             fdata = middle_api_protocol.load_juejin(path_str, data_json)
-            fdata = middle_api_protocol.request(fdata)
+            __start_time = time.time()
+            try:
+                fdata = middle_api_protocol.request(fdata)
+            finally:
+                __use_time = time.time() - __start_time
+                if __use_time > 5:
+                    logger_request_api.info(f"鎺橀噾API璇锋眰鏃堕棿锛歿path_str}-{int(__use_time)}")
+
         else:
             url = f'{cls.__BASE_URL}{path_str}'
             # 鍙戦�丳OST璇锋眰
@@ -72,6 +83,11 @@
                               "skip_st": skip_st, "fields": fields})
 
     @classmethod
+    def get_history_instruments(cls, symbols, start_date, end_date, fields):
+        return cls.__request("get_history_instruments",
+                             {"symbols": symbols, "start_date": start_date, "end_date": end_date, "fields": fields})
+
+    @classmethod
     def get_previous_trading_date(cls, exchange, date):
         return cls.__request("get_previous_trading_date", {"exchange": exchange, "date": date})
 
@@ -104,9 +120,9 @@
     def get_juejin_code_list_with_prefix(cls, codes):
         list = []
         for d in codes:
-            if d[0:2] == '00':
+            if tool.is_sz_code(d):
                 list.append("SZSE.{}".format(d))
-            elif d[0:2] == '60':
+            elif tool.is_sh_code(d):
                 list.append("SHSE.{}".format(d))
         return list
 
@@ -137,7 +153,7 @@
             return results
 
     @classmethod
-    def get_gp_current_info(cls, codes):
+    def get_gp_current_info(cls, codes, fields=None):
         if not codes:
             return []
         symbols = cls.get_juejin_code_list_with_prefix(codes)
@@ -147,22 +163,33 @@
             data = gmapi.current(symbols=",".join(symbols))
             return data
         else:
-            data = JueJinHttpApi.current(symbols=",".join(symbols), fields='')
+            data = JueJinHttpApi.current(symbols=",".join(symbols), fields=fields)
             return data
         # 杩斿洖鎸囧畾鏃ユ湡鐨勪笂涓氦鏄撴棩
 
     # 鑾峰彇浜ゆ槗鎵�鐨勪唬鐮�
     @classmethod
-    def get_exchanges_codes(cls, exchanges):
+    def get_exchanges_codes(cls, exchanges, skip_suspended=True, skip_st=True):
         if constant.JUEJIN_LOCAL_API:
             account_id, s_id, token = cls.getJueJinAccountInfo()
             gmapi.set_token(token)
-            return gmapi.get_instruments(exchanges=exchanges, sec_types=[1], skip_suspended=True, skip_st=True,
+            return gmapi.get_instruments(exchanges=exchanges, sec_types=[1], skip_suspended=skip_suspended,
+                                         skip_st=skip_st,
                                          fields="symbol,sec_type,sec_id,sec_name,listed_date,sec_level,is_suspended,pre_close")
         else:
-            return JueJinHttpApi.get_exchanges_codes(exchanges=exchanges, sec_types=[1], skip_suspended=True,
-                                                     skip_st=True,
+            return JueJinHttpApi.get_exchanges_codes(exchanges=exchanges, sec_types=[1], skip_suspended=skip_suspended,
+                                                     skip_st=skip_st,
                                                      fields="symbol,sec_type,sec_id,sec_name,listed_date,sec_level,is_suspended,pre_close")
+
+    @classmethod
+    def get_history_instruments(cls, symbols, start_date, end_date, fields=None):
+        if constant.JUEJIN_LOCAL_API:
+            account_id, s_id, token = cls.getJueJinAccountInfo()
+            gmapi.set_token(token)
+            return gmapi.get_history_instruments(symbols=symbols, start_date=start_date, end_date=end_date,
+                                                 fields="symbol,sec_type,sec_id,sec_name,listed_date,sec_level,is_suspended,pre_close")
+        else:
+            return JueJinHttpApi.get_history_instruments(symbols, start_date, end_date, fields)
 
     @classmethod
     def get_previous_trading_date(cls, date):
@@ -194,6 +221,8 @@
 
 
 class HistoryKDatasUtils(object):
+    __previous_trading_date_cache = {}
+    __latest_trading_date_cache = {}
 
     @classmethod
     def get_gp_latest_info(cls, codes, fields=None):
@@ -211,6 +240,35 @@
     @classmethod
     def get_previous_trading_date(cls, date):
         return JueJinApi.get_previous_trading_date(date)
+
+    @classmethod
+    def get_previous_trading_date_cache(cls, date):
+        if date in cls.__previous_trading_date_cache:
+            return cls.__previous_trading_date_cache.get(date)
+        fdata = cls.get_previous_trading_date(date)
+        if fdata:
+            cls.__previous_trading_date_cache[date] = fdata
+        return fdata
+
+    # 鑾峰彇鏈�杩戠殑浜ゆ槗鏃�
+    @classmethod
+    def get_latest_trading_date(cls, day_count):
+        now_day = tool.get_now_date_str()
+        days = []
+        for i in range(day_count):
+            pday = cls.get_previous_trading_date_cache(now_day)
+            days.append(pday)
+            now_day = pday
+        return days
+
+    @classmethod
+    def get_latest_trading_date_cache(cls, day_count):
+        key = f"{tool.get_now_date_str()}-{day_count}"
+        if key in cls.__latest_trading_date_cache:
+            return cls.__latest_trading_date_cache[key]
+        days = cls.get_latest_trading_date(day_count)
+        cls.__latest_trading_date_cache[key] = days
+        return days
 
     # 杩斿洖鎸囧畾鏃ユ湡鐨勪笅涓氦鏄撴棩
     @classmethod
@@ -274,5 +332,13 @@
 
 
 if __name__ == "__main__":
-    constant.JUEJIN_LOCAL_API = False
-    print(HistoryKDatasUtils.get_lowest_price_rate("000725", 30))
+    now_day = tool.get_now_date_str()
+    results = JueJinApi.get_history_instruments(JueJinApi.get_juejin_code_list_with_prefix(["600265"]),
+                                                tool.date_sub(now_day, 30), tool.date_sub(now_day, 1))
+    results = results[-5:]
+    normal = True
+    for r in results:
+        if r["sec_level"] != 1:
+            normal = False
+            break
+    print(normal)

--
Gitblit v1.8.0