This commit is contained in:
songzhuoyuan
2026-08-11 20:41:31 +08:00
parent 2ce13e5bae
commit befdc32aea
24 changed files with 1311 additions and 530 deletions
+22 -17
View File
@@ -333,15 +333,26 @@ class SmartMonitorKline:
self.logger.info(f"✅ TDX获取K线数据成功 {stock_code},共{len(df)}")
return df
else:
self.logger.warning(f"TDX未返回K线数据 {stock_code},尝试降级到AKShare")
self.logger.warning(f"TDX未返回K线数据 {stock_code},尝试降级到Tushare")
except Exception as e:
self.logger.warning(f"TDX获取K线数据失败 {stock_code}: {type(e).__name__}, 尝试降级到AKShare")
self.logger.warning(f"TDX获取K线数据失败 {stock_code}: {type(e).__name__}, 尝试降级到Tushare")
# 计算日期范围
end_date = datetime.now().strftime('%Y%m%d')
start_date = (datetime.now() - timedelta(days=days + 30)).strftime('%Y%m%d') # 多取30天以确保足够数据
# 方法2: 尝试使用AKShare获取(只尝试1次,避免IP封禁
# 方法2: 降级到Tushare(优先于AKShare
if data_fetcher and data_fetcher.ts_pro:
self.logger.info(f"降级使用Tushare获取K线数据 {stock_code}")
df = self._get_kline_from_tushare(stock_code, days, data_fetcher.ts_pro)
if df is not None and not df.empty:
self.logger.info(f"✅ Tushare获取K线数据成功 {stock_code},共{len(df)}")
return df
self.logger.warning(f"Tushare未返回K线数据 {stock_code},尝试降级到AKShare")
else:
self.logger.warning(f"未配置Tushare,尝试使用AKShare")
# 方法3: 尝试使用AKShare获取(只尝试1次,避免IP封禁)
try:
import akshare as ak
df = ak.stock_zh_a_hist(
@@ -358,17 +369,9 @@ class SmartMonitorKline:
self.logger.info(f"✅ AKShare获取K线数据成功 {stock_code},共{len(df)}")
return df
else:
self.logger.warning(f"AKShare未返回K线数据 {stock_code},尝试降级到Tushare")
self.logger.warning(f"AKShare未返回K线数据 {stock_code}")
except Exception as e:
self.logger.warning(f"AKShare获取K线数据失败 {stock_code}: {type(e).__name__}, 尝试降级到Tushare")
# 方法3: 降级到Tushare
if data_fetcher and data_fetcher.ts_pro:
self.logger.info(f"降级使用Tushare获取K线数据 {stock_code}")
df = self._get_kline_from_tushare(stock_code, days, data_fetcher.ts_pro)
if df is not None and not df.empty:
self.logger.info(f"✅ Tushare获取K线数据成功 {stock_code},共{len(df)}")
return df
self.logger.warning(f"AKShare获取K线数据失败 {stock_code}: {type(e).__name__}")
self.logger.error(f"所有数据源都无法获取K线数据 {stock_code}")
return None
@@ -404,12 +407,15 @@ class SmartMonitorKline:
end_date = datetime.now().strftime('%Y%m%d')
start_date = (datetime.now() - timedelta(days=days + 60)).strftime('%Y%m%d')
# 获取日K线数据(前复权)
df = ts_pro.daily(
# 获取日K线数据(前复权,daily接口不支持adj参数,需用pro_bar
import tushare as ts
df = ts.pro_bar(
api=ts_pro,
ts_code=ts_code,
start_date=start_date,
end_date=end_date,
adj='qfq'
adj='qfq',
retry_count=1
)
if df is None or df.empty:
@@ -493,4 +499,3 @@ if __name__ == '__main__':
print("K线图已保存到 test_kline.html")
else:
print("获取K线数据失败")