From 7de28c45eb5fc393bfed07ffbefb69dc63eeaa4d Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期四, 09 一月 2025 15:36:04 +0800 Subject: [PATCH] BUG修复 --- third_data/history_k_data_util.py | 304 ++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 265 insertions(+), 39 deletions(-) diff --git a/third_data/history_k_data_util.py b/third_data/history_k_data_util.py index ab6fc25..aada0ae 100644 --- a/third_data/history_k_data_util.py +++ b/third_data/history_k_data_util.py @@ -1,11 +1,105 @@ """ 鍘嗗彶K绾挎湇鍔� """ +import datetime import decimal +import json +import time -import tool -from db import redis_manager -import gm.api as gmapi +import requests + +import constant +from db.redis_manager_delegate import RedisUtils +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 + + +# import gm.api as gmapi + + +class JueJinHttpApi: + __BASE_URL = "http://193.112.35.168:10009/" + + @classmethod + def __request(cls, path_str, data_json): + def deformat_date(val): + if type(val) == str and val.find('T') > -1 and val.find(':') > -1 and val.find( + '+') > -1: + return datetime.datetime.fromisoformat(val) + return val + + DELEGATE = True + fdata = None + if DELEGATE: + fdata = middle_api_protocol.load_juejin(path_str, data_json) + __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璇锋眰 + response = requests.post(url, json=data_json) + result = response.text + resultJson = json.loads(result) + if resultJson['code'] == 0: + fdata = resultJson['data'] + if fdata: + if type(fdata) == list: + for d in fdata: + if type(d) != dict: + continue + for k in d: + d[k] = deformat_date(d[k]) + elif type(fdata) == dict: + for k in fdata: + fdata[k] = deformat_date(fdata[k]) + return fdata + else: + return None + + @classmethod + def get_instruments(cls, symbols, fields): + return cls.__request("get_instruments", {"symbols": symbols, "fields": fields}) + + @classmethod + def history_n(cls, symbol, frequency, count, adjust, fields): + return cls.__request("history_n", {"symbol": symbol, "frequency": frequency, "count": count, "adjust": adjust, + "fields": fields}) + + @classmethod + def current(cls, symbols, fields): + return cls.__request("current", {"symbols": symbols, "fields": fields}) + + @classmethod + def get_exchanges_codes(cls, exchanges, sec_types, skip_suspended, skip_st, fields): + return cls.__request("get_instruments", + {"exchanges": exchanges, "sec_types": sec_types, "skip_suspended": skip_suspended, + "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}) + + @classmethod + def get_next_trading_date(cls, exchange, date): + return cls.__request("get_next_trading_date", {"exchange": exchange, "date": date}) + + @classmethod + def get_trading_dates(cls, exchange, start_date, end_date): + return cls.__request("get_trading_dates", + {"exchange": exchange, "start_date": start_date, "end_date": end_date}) class JueJinApi: @@ -15,18 +109,21 @@ @classmethod def getJueJinAccountInfo(cls): redis = cls.__redisManager.getRedis() - account_id = redis.get("juejin-account-id") - strategy_id = redis.get("juejin-strategy-id") - token = redis.get("juejin-token") - return account_id, strategy_id, token + try: + account_id = RedisUtils.get(redis, "juejin-account-id", auto_free=False) + strategy_id = RedisUtils.get(redis, "juejin-strategy-id", auto_free=False) + token = RedisUtils.get(redis, "juejin-token", auto_free=False) + return account_id, strategy_id, token + finally: + RedisUtils.realse(redis) @classmethod 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 @@ -34,56 +131,101 @@ def get_gp_latest_info(cls, codes, fields=None): if not codes: return [] - account_id, s_id, token = cls.getJueJinAccountInfo() symbols = cls.get_juejin_code_list_with_prefix(codes) - gmapi.set_token(token) - data = gmapi.get_instruments(symbols=",".join(symbols), fields=fields) - print(data) - return data + if constant.JUEJIN_LOCAL_API: + account_id, s_id, token = cls.getJueJinAccountInfo() + gmapi.set_token(token) + data = gmapi.get_instruments(symbols=",".join(symbols), fields=fields) + return data + else: + return JueJinHttpApi.get_instruments(symbols=",".join(symbols), fields=fields) @classmethod def get_history_tick_n(cls, code, count, fields=None): - account_id, s_id, token = cls.getJueJinAccountInfo() symbols = cls.get_juejin_code_list_with_prefix([code]) - gmapi.set_token(token) - # 鍓嶉櫎鏉� - results = gmapi.history_n(symbol=symbols[0], frequency="1d", count=count, adjust=1, fields=fields) - return results + if constant.JUEJIN_LOCAL_API: + account_id, s_id, token = cls.getJueJinAccountInfo() + gmapi.set_token(token) + # 鍓嶉櫎鏉� + results = gmapi.history_n(symbol=symbols[0], frequency="1d", count=count, adjust=1, fields=fields) + return results + else: + results = JueJinHttpApi.history_n(symbol=symbols[0], frequency="1d", count=count, adjust=1, fields=fields) + return results @classmethod - def get_gp_current_info(cls, codes): + def get_gp_current_info(cls, codes, fields=None): if not codes: return [] - account_id, s_id, token = cls.getJueJinAccountInfo() symbols = cls.get_juejin_code_list_with_prefix(codes) - gmapi.set_token(token) - data = gmapi.current(symbols=",".join(symbols)) - print(data) - return data - + if constant.JUEJIN_LOCAL_API: + account_id, s_id, token = cls.getJueJinAccountInfo() + gmapi.set_token(token) + data = gmapi.current(symbols=",".join(symbols)) + return data + else: + data = JueJinHttpApi.current(symbols=",".join(symbols), fields=fields) + return data # 杩斿洖鎸囧畾鏃ユ湡鐨勪笂涓氦鏄撴棩 + + # 鑾峰彇浜ゆ槗鎵�鐨勪唬鐮� + @classmethod + 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=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=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): - account_id, s_id, token = cls.getJueJinAccountInfo() - gmapi.set_token(token) - return gmapi.get_previous_trading_date("SHSE", 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) + pass + else: + return JueJinHttpApi.get_previous_trading_date("SHSE", date) # 杩斿洖鎸囧畾鏃ユ湡鐨勪笅涓氦鏄撴棩 @classmethod def get_next_trading_date(cls, date): - account_id, s_id, token = cls.getJueJinAccountInfo() - gmapi.set_token(token) - return gmapi.get_previous_trading_date("SHSE", date) + if constant.JUEJIN_LOCAL_API: + account_id, s_id, token = cls.getJueJinAccountInfo() + gmapi.set_token(token) + return gmapi.get_next_trading_date("SHSE", date) + else: + return JueJinHttpApi.get_next_trading_date("SHSE", date) @classmethod def get_trading_dates(cls, start_date, end_date): - account_id, s_id, token = cls.getJueJinAccountInfo() - gmapi.set_token(token) - return gmapi.get_trading_dates("SHSE", start_date, end_date) + if constant.JUEJIN_LOCAL_API: + account_id, s_id, token = cls.getJueJinAccountInfo() + gmapi.set_token(token) + return gmapi.get_trading_dates("SHSE", start_date, end_date) + else: + return JueJinHttpApi.get_trading_dates("SHSE", start_date, end_date) class HistoryKDatasUtils(object): + __previous_trading_date_cache = {} + __latest_trading_date_cache = {} @classmethod def get_gp_latest_info(cls, codes, fields=None): @@ -91,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): @@ -100,16 +250,59 @@ # 杩斿洖鎸囧畾鏃ユ湡鐨勪笂涓氦鏄撴棩 @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): + 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): + """ + 鑾峰彇鏈�杩戝嚑涓氦鏄撴棩锛堜笉鍖呭惈浠婂ぉ锛� + @param day_count: + @return: + """ + 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): + """ + 鑾峰彇鏈�杩戝嚑涓氦鏄撴棩锛堜笉鍖呭惈浠婂ぉ锛� + @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] + days = cls.get_latest_trading_date(day_count) + cls.__latest_trading_date_cache[key] = days + return days # 杩斿洖鎸囧畾鏃ユ湡鐨勪笅涓氦鏄撴棩 @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): @@ -161,3 +354,36 @@ code_name = data['sec_name'] results[code] = code_name 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__": + 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) -- Gitblit v1.8.0