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