diff --git a/src/data_process_model/main.py b/src/data_process_model/main.py index 171d501..1faa7ad 100644 --- a/src/data_process_model/main.py +++ b/src/data_process_model/main.py @@ -13,6 +13,21 @@ g_period = "1m" # 周期 g_start_date = "20250501" # 开始日期 g_end_date = "20250721" # 结束日期 +g_period_list = [ + "1m", # 1分钟 + "5m", # 5分钟 + "15m", # 15分钟 + "30m", # 30分钟 + "60m", # 60分钟 + "1d", # 日线 + "1w", # 周线 + "1mon", # 月线 + "1q", # 季线 + "1hy", # 半年线 + "1y", # 年线 +] + + """ 'time' #时间戳 'open' #开盘价 @@ -373,20 +388,22 @@ def process_all_stocks(history_data, stock_codes): def main(): """ - 数据处理模型主程序入口函数 + 数据处理模型主程序入口函数 (批量周期模式) - 执行完整的股票数据处理流程,包括数据下载、格式化处理、 - CSV文件保存和结果统计,提供完整的数据处理解决方案 + 执行完整的股票数据处理流程,支持批量下载多个周期的数据, + 包括数据下载、格式化处理、CSV文件保存和MySQL数据库存储 主要执行步骤: - 1. 根据全局配置下载指定股票的历史数据 - 2. 对每个股票的数据进行格式化处理(时间转换、DataFrame化) - 3. 将处理后的数据保存为独立的CSV文件 - 4. 输出处理结果统计和文件路径信息 + 1. 初始化MySQL数据库连接 + 2. 循环处理所有配置的数据周期 + 3. 对每个周期下载指定股票的历史数据 + 4. 对每个股票的数据进行格式化处理(时间转换、DataFrame化) + 5. 将处理后的数据保存为CSV文件和MySQL数据库 + 6. 输出处理结果统计和文件路径信息 全局配置依赖: - g_stock_code_list: 股票代码列表,如 ["601658.SH", "000001.SZ"] - - g_period: 数据周期,如 "1m", "1d" + - g_period_list: 数据周期列表,如 ["1m", "5m", "1d", "1w"] - g_start_date: 开始日期,如 "20240101" - g_end_date: 结束日期,如 "20241231" @@ -400,8 +417,8 @@ def main(): Side Effects: - 在控制台输出详细的处理进度和结果 - 在 "data" 目录下创建多个CSV文件 - - 建立xtquant数据连接 - - 可能修改全局状态或缓存 + - 建立xtquant数据连接和MySQL数据库连接 + - 为每个周期创建对应的数据库表 Raises: KeyboardInterrupt: 用户手动中断程序执行 @@ -410,20 +427,24 @@ def main(): Example: >>> # 设置全局配置 >>> g_stock_code_list = ["601658.SH", "000001.SZ"] - >>> g_period = "1d" + >>> g_period_list = ["1m", "1d", "1w"] >>> g_start_date = "20240101" >>> g_end_date = "20240131" >>> >>> # 执行主程序 >>> main() - 🎯 数据处理模型主程序 - 📥 开始下载历史数据... - ✅ 成功处理 2 个股票文件 - 📁 data/601658_SH_1d_20250722_143021_123456.csv - 📁 data/000001_SZ_1d_20250722_143022_789012.csv + 🎯 数据处理模型主程序 (批量周期模式) + 📥 开始下载 3 个周期的历史数据... + ✅ 成功处理所有周期数据 """ try: - print("🎯 数据处理模型主程序 (支持MySQL长连接)") + print("🎯 数据处理模型主程序 (支持MySQL长连接 + 批量周期下载)") + print("=" * 60) + + # 显示将要处理的配置信息 + print(f"📊 股票代码: {', '.join(g_stock_code_list)}") + print(f"📈 数据周期: {', '.join(g_period_list)} ({len(g_period_list)}个周期)") + print(f"📅 时间范围: {g_start_date} ~ {g_end_date}") print("=" * 60) # 1. 初始化MySQL数据库连接 @@ -433,47 +454,84 @@ def main(): return print("✅ MySQL数据库连接成功,保持长连接状态") - # 2. 下载股票数据 - history_data = download_stock_data( - g_stock_code_list, g_period, g_start_date, g_end_date - ) + # 统计总体结果 + all_csv_files = [] + all_mysql_records = {} + total_periods_processed = 0 + failed_periods = [] - # 3. 处理所有股票数据 - print(f"💾 开始处理 {len(g_stock_code_list)} 个股票的数据...") - processed_data = process_all_stocks(history_data, g_stock_code_list) - - if not processed_data: - print("⚠️ 没有成功处理的股票数据") - return + # 2. 循环处理每个数据周期 + for period_index, current_period in enumerate(g_period_list, 1): + try: + print(f"\n🔄 [{period_index}/{len(g_period_list)}] 开始处理周期: {current_period}") + print("-" * 50) - # 4a. 保存到CSV文件 - print("📁 开始保存CSV文件...") - csv_files = save_all_to_csv(processed_data, g_period) - - # 4b. 保存到MySQL数据库 - print("🗄️ 开始保存到MySQL数据库...") - print("🔄 创建MySQL数据表...") - stock_models = create_tables_for_stocks(g_stock_code_list, g_period) - mysql_records = save_all_to_mysql(processed_data, stock_models) + # 3. 下载当前周期的股票数据 + print(f"📥 正在下载 {current_period} 周期数据...") + history_data = download_stock_data( + g_stock_code_list, current_period, g_start_date, g_end_date + ) - # 5. 显示处理结果统计 + # 4. 处理当前周期的所有股票数据 + print(f"💾 开始处理 {len(g_stock_code_list)} 个股票的 {current_period} 数据...") + processed_data = process_all_stocks(history_data, g_stock_code_list) + + if not processed_data: + print(f"⚠️ {current_period} 周期没有成功处理的股票数据,跳过...") + failed_periods.append(current_period) + continue + + # 5a. 保存到CSV文件 + print(f"📁 保存 {current_period} 数据到CSV文件...") + csv_files = save_all_to_csv(processed_data, current_period) + all_csv_files.extend(csv_files) + + # 5b. 保存到MySQL数据库 + print(f"🗄️ 保存 {current_period} 数据到MySQL数据库...") + print(f"🔄 创建 {current_period} 周期的MySQL数据表...") + stock_models = create_tables_for_stocks(g_stock_code_list, current_period) + mysql_records = save_all_to_mysql(processed_data, stock_models) + + # 合并MySQL记录统计 + for stock_code, count in mysql_records.items(): + key = f"{stock_code}_{current_period}" + all_mysql_records[key] = count + + total_periods_processed += 1 + print(f"✅ {current_period} 周期处理完成!") + + except Exception as e: + print(f"❌ {current_period} 周期处理失败: {e}") + failed_periods.append(current_period) + continue + + # 6. 显示最终处理结果统计 print("\n" + "=" * 60) - print("📈 数据处理完成总结:") + print("📈 批量数据处理完成总结:") + print(f"🎯 处理周期: {total_periods_processed}/{len(g_period_list)} 个成功") + + if failed_periods: + print(f"❌ 失败周期: {', '.join(failed_periods)}") # MySQL结果统计 - mysql_total = sum(mysql_records.values()) - print(f"🗄️ MySQL数据库记录: {mysql_total} 条") - for stock_code, count in mysql_records.items(): - print(f" 📊 {stock_code}: {count} 条记录") + mysql_total = sum(all_mysql_records.values()) + print(f"🗄️ MySQL数据库记录: {mysql_total} 条") + for key, count in all_mysql_records.items(): + print(f" 📊 {key}: {count} 条记录") # CSV文件统计 - csv_count = len(csv_files) + csv_count = len(all_csv_files) print(f"📁 CSV文件数量: {csv_count} 个") - for file_path in csv_files: - print(f" � {file_path}") + for file_path in all_csv_files[:10]: # 只显示前10个文件,避免输出过长 + print(f" 📁 {file_path}") + if csv_count > 10: + print(f" 📁 ... 及其他 {csv_count - 10} 个文件") print("=" * 60) - print("🎉 所有数据已成功保存到MySQL数据库和CSV文件!") + if total_periods_processed > 0: + print(f"🎉 成功处理 {total_periods_processed} 个周期的数据,已保存到MySQL数据库和CSV文件!") + else: + print("❌ 没有成功处理任何周期的数据") except KeyboardInterrupt: print("\n⚠️ 收到中断信号,停止数据处理")