From bd7cddc5e79de35f575e6b26c4d26781588b72bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=AE=87=E5=B3=B0?= Date: Mon, 2 Mar 2026 01:53:05 +0800 Subject: [PATCH] Fix SZSE trading calendar fetch for historical years (2000-2004) Remove dead SZSE_CALENDAR_URL constant left behind after the akshare refactor. Remove debug print statements that created unnecessary Ticker network requests. Add error handling around the akshare calendar fetch to provide clear error messages when akshare is not installed or the Sina API is unavailable. Fixes #2088 Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/data_collector/utils.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/scripts/data_collector/utils.py b/scripts/data_collector/utils.py index bf87d0de5e..0d701e1268 100644 --- a/scripts/data_collector/utils.py +++ b/scripts/data_collector/utils.py @@ -24,7 +24,6 @@ HS_SYMBOLS_URL = "http://app.finance.ifeng.com/hq/list.php?type=stock_a&class={s_type}" CALENDAR_URL_BASE = "http://push2his.eastmoney.com/api/qt/stock/kline/get?secid={market}.{bench_code}&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&klt=101&fqt=0&beg=19900101&end=20991231" -SZSE_CALENDAR_URL = "http://www.szse.cn/api/report/exchange/onepersistenthour/monthList?month={month}&random={random}" CALENDAR_BENCH_URL_MAP = { "CSI300": CALENDAR_URL_BASE.format(market=1, bench_code="000300"), @@ -73,20 +72,28 @@ def _get_calendar(url): calendar = _CALENDAR_MAP.get(bench_code, None) if calendar is None: if bench_code.startswith("US_") or bench_code.startswith("IN_") or bench_code.startswith("BR_"): - print(Ticker(CALENDAR_BENCH_URL_MAP[bench_code])) - print(Ticker(CALENDAR_BENCH_URL_MAP[bench_code]).history(interval="1d", period="max")) df = Ticker(CALENDAR_BENCH_URL_MAP[bench_code]).history(interval="1d", period="max") calendar = df.index.get_level_values(level="date").map(pd.Timestamp).unique().tolist() else: if bench_code.upper() == "ALL": - import akshare as ak # pylint: disable=C0415 - - trade_date_df = ak.tool_trade_date_hist_sina() - trade_date_list = trade_date_df["trade_date"].tolist() - trade_date_list = [pd.Timestamp(d) for d in trade_date_list] - dates = pd.DatetimeIndex(trade_date_list) - filtered_dates = dates[(dates >= "2000-01-04") & (dates <= pd.Timestamp.today().normalize())] - calendar = filtered_dates.tolist() + try: + import akshare as ak # pylint: disable=C0415 + + trade_date_df = ak.tool_trade_date_hist_sina() + trade_date_list = trade_date_df["trade_date"].tolist() + trade_date_list = [pd.Timestamp(d) for d in trade_date_list] + dates = pd.DatetimeIndex(trade_date_list) + filtered_dates = dates[(dates >= "2000-01-04") & (dates <= pd.Timestamp.today().normalize())] + calendar = filtered_dates.tolist() + except ImportError: + logger.error( + "akshare is required for fetching the full CN trading calendar. " + "Install it with: pip install akshare" + ) + raise + except Exception as e: + logger.error(f"Failed to fetch trading calendar via akshare: {e}") + raise else: calendar = _get_calendar(CALENDAR_BENCH_URL_MAP[bench_code]) _CALENDAR_MAP[bench_code] = calendar