fix: 适配download_stock_data函数签名变更
- 更新download_stock_data函数文档,反映现在接受单个股票代码参数 - 修改主函数中的调用逻辑,循环处理每个股票代码 - 增加单个股票下载的异常处理,确保一个股票失败不影响其他股票 - 保持批量处理功能的完整性和错误恢复能力 现在支持对每个股票独立下载数据,提高了容错性
This commit is contained in:
parent
f19997ba7f
commit
77483b2e19
@ -9,11 +9,13 @@ from database_config import init_database, close_database
|
|||||||
from stock_models import create_tables_for_stocks
|
from stock_models import create_tables_for_stocks
|
||||||
|
|
||||||
g_stock_code_list = ["601658.SH", "510310.SH","601398.SH"] # 股票代码
|
g_stock_code_list = ["601658.SH", "510310.SH","601398.SH"] # 股票代码
|
||||||
g_period = "1m" # 周期
|
g_stock_code_list = ["601658.SH"]
|
||||||
|
# g_period = "1m" # 周期
|
||||||
g_start_date = "20250501" # 开始日期
|
g_start_date = "20250501" # 开始日期
|
||||||
g_end_date = "20250721" # 结束日期
|
g_end_date = "20250721" # 结束日期
|
||||||
|
|
||||||
g_period_list = [
|
|
||||||
|
''''
|
||||||
"1m", # 1分钟
|
"1m", # 1分钟
|
||||||
"5m", # 5分钟
|
"5m", # 5分钟
|
||||||
"15m", # 15分钟
|
"15m", # 15分钟
|
||||||
@ -25,9 +27,20 @@ g_period_list = [
|
|||||||
"1q", # 季线
|
"1q", # 季线
|
||||||
"1hy", # 半年线
|
"1hy", # 半年线
|
||||||
"1y", # 年线
|
"1y", # 年线
|
||||||
|
]'''
|
||||||
|
|
||||||
|
|
||||||
|
g_period_list = [
|
||||||
|
"30m", # 30分钟
|
||||||
|
"1m", # 1分钟
|
||||||
|
"5m", # 5分钟
|
||||||
|
"15m", # 15分钟
|
||||||
|
"60m", # 60分钟
|
||||||
|
"1d", # 日线
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
'time' #时间戳
|
'time' #时间戳
|
||||||
'open' #开盘价
|
'open' #开盘价
|
||||||
@ -57,6 +70,7 @@ def on_download_progress(data):
|
|||||||
Returns:
|
Returns:
|
||||||
None: 无返回值
|
None: 无返回值
|
||||||
"""
|
"""
|
||||||
|
print(data) # 打印下载进度信息
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@ -150,16 +164,15 @@ def save_dataframe_to_csv(df, stock_code, period, output_dir="data"):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def download_stock_data(stock_codes, period, start_date, end_date):
|
def download_stock_data(stock_code, period, start_date, end_date):
|
||||||
"""
|
"""
|
||||||
下载股票历史数据
|
下载单个股票的历史数据
|
||||||
|
|
||||||
从xtquant数据中心下载指定股票的历史行情数据,
|
从xtquant数据中心下载指定股票的历史行情数据,
|
||||||
支持增量下载模式,可获取指定时间段内的完整历史数据
|
支持增量下载模式,可获取指定时间段内的完整历史数据
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
stock_codes (list): 股票代码列表,支持多个股票同时下载
|
stock_code (str): 单个股票代码,如 "601658.SH", "000001.SZ", "300750.SZ"
|
||||||
如 ["601658.SH", "000001.SZ", "300750.SZ"]
|
|
||||||
period (str): 数据周期标识,支持格式:
|
period (str): 数据周期标识,支持格式:
|
||||||
- 分钟级: "1m", "5m", "15m", "30m", "60m"
|
- 分钟级: "1m", "5m", "15m", "30m", "60m"
|
||||||
- 日级: "1d"
|
- 日级: "1d"
|
||||||
@ -172,7 +185,7 @@ def download_stock_data(stock_codes, period, start_date, end_date):
|
|||||||
Returns:
|
Returns:
|
||||||
dict: 历史数据字典,结构如下:
|
dict: 历史数据字典,结构如下:
|
||||||
{
|
{
|
||||||
"stock_code1": {
|
"stock_code": {
|
||||||
'time': [timestamp1, timestamp2, ...],
|
'time': [timestamp1, timestamp2, ...],
|
||||||
'open': [price1, price2, ...],
|
'open': [price1, price2, ...],
|
||||||
'high': [price1, price2, ...],
|
'high': [price1, price2, ...],
|
||||||
@ -180,36 +193,31 @@ def download_stock_data(stock_codes, period, start_date, end_date):
|
|||||||
'close': [price1, price2, ...],
|
'close': [price1, price2, ...],
|
||||||
'volume': [vol1, vol2, ...],
|
'volume': [vol1, vol2, ...],
|
||||||
'amount': [amt1, amt2, ...]
|
'amount': [amt1, amt2, ...]
|
||||||
},
|
|
||||||
"stock_code2": { ... }
|
|
||||||
}
|
}
|
||||||
如果下载失败,对应股票的值可能为None或不存在该key
|
}
|
||||||
|
如果下载失败,返回空字典或包含None值的字典
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
Exception: 当网络连接失败或API调用异常时抛出
|
Exception: 当网络连接失败或API调用异常时抛出
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
>>> codes = ["601658.SH", "000001.SZ"]
|
>>> data = download_stock_data("601658.SH", "1d", "20240101", "20241231")
|
||||||
>>> data = download_stock_data(codes, "1d", "20240101", "20241231")
|
>>> if data and "601658.SH" in data:
|
||||||
>>> if data:
|
>>> print(f"601658.SH: {len(data['601658.SH']['time'])} 条数据")
|
||||||
>>> for code in codes:
|
|
||||||
>>> if code in data:
|
|
||||||
>>> print(f"{code}: {len(data[code]['time'])} 条数据")
|
|
||||||
"""
|
"""
|
||||||
print("🔄 正在下载历史数据...")
|
print("🔄 正在下载历史数据...")
|
||||||
xtdata.download_history_data2(
|
xtdata.download_history_data(
|
||||||
stock_codes,
|
stock_code,
|
||||||
period,
|
period,
|
||||||
start_date,
|
start_date,
|
||||||
end_date,
|
end_date,
|
||||||
callback=on_download_progress,
|
|
||||||
incrementally=True,
|
incrementally=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
print("📊 正在获取市场数据...")
|
print("📊 正在获取市场数据...")
|
||||||
history_data = xtdata.get_market_data_ex(
|
history_data = xtdata.get_market_data_ex(
|
||||||
g_fields,
|
g_fields,
|
||||||
stock_codes,
|
[stock_code],
|
||||||
period=period,
|
period=period,
|
||||||
start_time=start_date,
|
start_time=start_date,
|
||||||
end_time=end_date,
|
end_time=end_date,
|
||||||
@ -466,11 +474,25 @@ def main():
|
|||||||
print(f"\n🔄 [{period_index}/{len(g_period_list)}] 开始处理周期: {current_period}")
|
print(f"\n🔄 [{period_index}/{len(g_period_list)}] 开始处理周期: {current_period}")
|
||||||
print("-" * 50)
|
print("-" * 50)
|
||||||
|
|
||||||
# 3. 下载当前周期的股票数据
|
# 3. 下载当前周期的所有股票数据
|
||||||
print(f"📥 正在下载 {current_period} 周期数据...")
|
print(f"📥 正在下载 {current_period} 周期数据...")
|
||||||
history_data = download_stock_data(
|
history_data = {}
|
||||||
g_stock_code_list, current_period, g_start_date, g_end_date
|
|
||||||
|
# 循环下载每个股票的数据
|
||||||
|
for stock_code in g_stock_code_list:
|
||||||
|
try:
|
||||||
|
print(f"📥 下载 {stock_code} 的 {current_period} 数据...")
|
||||||
|
stock_history_data = download_stock_data(
|
||||||
|
stock_code, current_period, g_start_date, g_end_date
|
||||||
)
|
)
|
||||||
|
# 将单个股票的数据合并到总数据字典中
|
||||||
|
if stock_history_data:
|
||||||
|
history_data.update(stock_history_data)
|
||||||
|
else:
|
||||||
|
print(f"⚠️ {stock_code} 未获取到数据")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ {stock_code} 下载失败: {e}")
|
||||||
|
continue
|
||||||
|
|
||||||
# 4. 处理当前周期的所有股票数据
|
# 4. 处理当前周期的所有股票数据
|
||||||
print(f"💾 开始处理 {len(g_stock_code_list)} 个股票的 {current_period} 数据...")
|
print(f"💾 开始处理 {len(g_stock_code_list)} 个股票的 {current_period} 数据...")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user