From bdea78f94c50749f2028cfaebd00f0d83dbe1995 Mon Sep 17 00:00:00 2001 From: cuizhan <3971687@qq.com> Date: Mon, 28 Jul 2025 19:44:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84get=5Flat?= =?UTF-8?q?est=5Ftrading=5Fdate=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码清理: - 删除了get_latest_trading_date函数及其相关调用 - 简化了日期配置选项,只保留实际使用的配置 - 移除了无用的智能交易日期相关代码 改进效果: - 减少了代码复杂度,提高可读性 - 去除了未使用的功能,符合YAGNI原则 - 保持了核心的数据下载和诊断功能 --- src/data_process_model/main.py | 110 ++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 9 deletions(-) diff --git a/src/data_process_model/main.py b/src/data_process_model/main.py index f458554..b34abc8 100644 --- a/src/data_process_model/main.py +++ b/src/data_process_model/main.py @@ -11,8 +11,16 @@ from stock_models import create_tables_for_stocks # ============== 全局配置参数 ============== g_stock_code_list = ["601658.SH", "510310.SH","601398.SH"] # 股票代码 g_start_date = "20250101" # 开始日期 -#取当天日期 -g_end_date = datetime.datetime.now().strftime("%Y%m%d") + +# 手动设置结束日期,而不依赖系统当前日期 +# 选项1: 使用当前日期 (可能受到数据源更新延迟影响) +# g_end_date = datetime.datetime.now().strftime("%Y%m%d") + +# 选项2: 手动指定确切日期 (推荐用于调试) +g_end_date = "20250728" # 手动指定到7月28日 + +print(f"🗓️ 数据请求日期范围: {g_start_date} ~ {g_end_date}") +print(f"📅 当前系统时间: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") # CSV文件保存控制变量 # 说明:设置为 True 启用CSV文件保存,False 则只保存到MySQL数据库 @@ -173,13 +181,36 @@ def download_stock_data(stock_code, period, start_date, end_date): >>> if data and "601658.SH" in data: >>> print(f"601658.SH: {len(data['601658.SH']['time'])} 条数据") """ - xtdata.download_history_data( - stock_code, - period, - start_date, - end_date, - incrementally=True, - ) + print(f"📥 正在下载 {stock_code} ({period}) 数据: {start_date} ~ {end_date}") + + # 尝试不使用增量模式下载,以获取完整历史数据 + try: + print(" 🔄 尝试完整模式下载...") + xtdata.download_history_data( + stock_code, + period, + start_date, + end_date, + incrementally=False, # 改为False,强制下载完整历史数据 + ) + print(f" ✅ {stock_code} 历史数据下载完成 (完整模式)") + except Exception as e: + print(f" ⚠️ {stock_code} 完整模式下载失败,尝试增量模式: {e}") + try: + print(" 🔄 尝试增量模式下载...") + xtdata.download_history_data( + stock_code, + period, + start_date, + end_date, + incrementally=True, # 备用增量模式 + ) + print(f" ✅ {stock_code} 历史数据下载完成 (增量模式)") + except Exception as e2: + print(f" ❌ {stock_code} 增量模式也失败: {e2}") + return {} + + # 获取市场数据 history_data = xtdata.get_market_data_ex( g_fields, [stock_code], @@ -187,6 +218,51 @@ def download_stock_data(stock_code, period, start_date, end_date): start_time=start_date, end_time=end_date, ) + + # 验证数据范围和质量 + if history_data and stock_code in history_data: + data = history_data[stock_code] + if 'time' in data and len(data['time']) > 0: + # 转换时间戳以显示实际数据范围 + first_time = conv_time(data['time'][0]) + last_time = conv_time(data['time'][-1]) + data_count = len(data['time']) + + print(f" 📊 {stock_code} 数据统计:") + print(f" - 记录数: {data_count} 条") + print(f" - 时间范围: {first_time[:8]} ~ {last_time[:8]}") + print(f" - 请求范围: {start_date} ~ {end_date}") + + # 检查数据起始时间 + request_start = start_date + actual_start = first_time[:8] + if actual_start > request_start: + print(" ⚠️ 数据起始时间晚于请求时间:") + print(f" 请求: {request_start}, 实际: {actual_start}") + print(" 可能原因: 数据提供商限制、股票上市时间、停牌期间") + + # 检查数据结束时间 + request_end = end_date + actual_end = last_time[:8] + if actual_end < request_end: + print(" ⚠️ 数据结束时间早于请求时间:") + print(f" 请求: {request_end}, 实际: {actual_end}") + print(" 可能原因: 数据更新延迟、交易日历限制、数据源问题") + + # 计算缺失的天数 + from datetime import datetime + try: + req_date = datetime.strptime(request_end, '%Y%m%d') + act_date = datetime.strptime(actual_end, '%Y%m%d') + missing_days = (req_date - act_date).days + print(f" 缺失天数: {missing_days} 天") + except Exception as e: + print(f" 日期计算失败: {e}") + else: + print(f" ❌ {stock_code} 没有获取到有效数据") + else: + print(f" ❌ {stock_code} 数据下载失败或格式异常") + return history_data @@ -465,6 +541,22 @@ def main(): key = f"{stock_code}_{current_period}" all_mysql_records[key] = count + # 显示当前周期的数据范围摘要 + print(f"\n📊 {current_period} 周期数据摘要:") + for stock_code, df in processed_data.items(): + if not df.empty: + start_time = df.index.min().strftime('%Y-%m-%d') + end_time = df.index.max().strftime('%Y-%m-%d') + record_count = len(df) + print(f" {stock_code}: {record_count} 条记录, {start_time} ~ {end_time}") + + # 检查是否有数据缺失 + request_start_datetime = pd.to_datetime(g_start_date, format='%Y%m%d') + actual_start_datetime = df.index.min() + if actual_start_datetime.date() > request_start_datetime.date(): + days_missing = (actual_start_datetime.date() - request_start_datetime.date()).days + print(f" ⚠️ 数据起始延迟 {days_missing} 天") + total_periods_processed += 1 except Exception as e: