Administrator
2025-01-09 7de28c45eb5fc393bfed07ffbefb69dc63eeaa4d
third_data/history_k_data_util.py
@@ -10,7 +10,8 @@
import constant
from db.redis_manager_delegate import RedisUtils
from log_module.log import logger_request_api
from log_module.log import logger_request_api, logger_debug
from third_data import hx_qc_value_util
from utils import tool, middle_api_protocol
from db import redis_manager_delegate as redis_manager
@@ -87,8 +88,6 @@
        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})
@@ -155,7 +154,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)
@@ -165,7 +164,7 @@
            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
        # 返回指定日期的上个交易日
@@ -181,7 +180,8 @@
        else:
            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")
                                                     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):
@@ -189,16 +189,17 @@
            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")
                                                 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):
        if constant.JUEJIN_LOCAL_API:
            account_id, s_id, token = cls.getJueJinAccountInfo()
            gmapi.set_token(token)
            return gmapi.get_previous_trading_date("SHSE", date)
            # account_id, s_id, token = cls.getJueJinAccountInfo()
            # gmapi.set_token(token)
            # return gmapi.get_previous_trading_date("SHSE", date)
            pass
        else:
            return JueJinHttpApi.get_previous_trading_date("SHSE", date)
@@ -232,7 +233,15 @@
    @classmethod
    def get_history_tick_n(cls, code, count, fields=None):
        return JueJinApi.get_history_tick_n(code, count, fields)
        # return JueJinApi.get_history_tick_n(code, count, fields)
        if constant.is_windows():
            return JueJinApi.get_history_tick_n(code, count, fields)
        else:
            try:
                return hx_qc_value_util.get_history_k_bars(code, count)
            except Exception as e:
                logger_debug.exception(e)
    @classmethod
    def get_gp_current_info(cls, codes):
@@ -241,7 +250,10 @@
    # 返回指定日期的上个交易日
    @classmethod
    def get_previous_trading_date(cls, date):
        return JueJinApi.get_previous_trading_date(date)
        if constant.is_windows():
            return JueJinApi.get_previous_trading_date(date)
        else:
            return hx_qc_value_util.get_previous_trading_date(date)
    @classmethod
    def get_previous_trading_date_cache(cls, date):
@@ -255,6 +267,11 @@
    # 获取最近的交易日
    @classmethod
    def get_latest_trading_date(cls, day_count):
        """
       获取最近几个交易日(不包含今天)
        @param day_count:
        @return:
        """
        now_day = tool.get_now_date_str()
        days = []
        for i in range(day_count):
@@ -265,6 +282,11 @@
    @classmethod
    def get_latest_trading_date_cache(cls, day_count):
        """
        获取最近几个交易日(不包含今天)
        @param day_count:
        @return:
        """
        key = f"{tool.get_now_date_str()}-{day_count}"
        if key in cls.__latest_trading_date_cache:
            return cls.__latest_trading_date_cache[key]
@@ -275,11 +297,12 @@
    # 返回指定日期的下个交易日
    @classmethod
    def get_next_trading_date(cls, date):
        return JueJinApi.get_next_trading_date(date)
        # return JueJinApi.get_next_trading_date(date)
        return hx_qc_value_util.get_next_trading_date(date)
    @classmethod
    def get_trading_dates(cls, start_date, end_date):
        return JueJinApi.get_trading_dates(start_date, end_date)
        return hx_qc_value_util.get_trade_calendar(start_date, end_date)
    @classmethod
    def get_now_price(cls, codes):
@@ -333,13 +356,34 @@
        return results
def get_k_bar_dead_date():
    """
    获取K线的截止日期
    @return:
    """
    dates = HistoryKDatasUtils.get_latest_trading_date_cache(5)
    latest_trading_date = None
    if dates:
        latest_trading_date = dates[0]
    if latest_trading_date is None:
        raise Exception("没有获取到上一个交易日的日期")
    # 4点之后改为获取今日的数据
    if tool.get_now_time_as_int() > 160000:
        latest_trading_date = tool.get_now_date_str()
    return latest_trading_date
if __name__ == "__main__":
    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)
    print(HistoryKDatasUtils.get_previous_trading_date("2024-12-31"))
    print(HistoryKDatasUtils.get_history_tick_n("000095", 10))
    # 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)