tushare
This commit is contained in:
+48
-36
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
智策板块数据采集模块
|
||||
使用AKShare获取板块相关数据
|
||||
优先使用Tushare,失败时使用AKShare获取板块相关数据
|
||||
"""
|
||||
|
||||
import akshare as ak
|
||||
@@ -12,6 +12,7 @@ import logging
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sector_strategy_db import SectorStrategyDatabase
|
||||
from data_source_manager import data_source_manager
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
@@ -254,40 +255,52 @@ class SectorStrategyDataFetcher:
|
||||
except:
|
||||
pass
|
||||
|
||||
# 大盘指数
|
||||
# 大盘指数(优先tushare,失败回退akshare)
|
||||
def _get_index_data(index_code, ts_code, name):
|
||||
if data_source_manager.tushare_available:
|
||||
try:
|
||||
end = datetime.now().strftime('%Y%m%d')
|
||||
start = (datetime.now() - timedelta(days=10)).strftime('%Y%m%d')
|
||||
df = data_source_manager.tushare_api.index_daily(
|
||||
ts_code=ts_code, start_date=start, end_date=end
|
||||
)
|
||||
if df is not None and not df.empty:
|
||||
row = df.iloc[0]
|
||||
return {
|
||||
"code": index_code,
|
||||
"name": name,
|
||||
"close": row.get('close', 0),
|
||||
"change_pct": row.get('pct_chg', 0),
|
||||
"change": row.get('change', 0)
|
||||
}
|
||||
print(f" tushare未获取到{name},尝试备用数据源")
|
||||
except Exception as e:
|
||||
print(f" tushare获取{name}失败: {e}")
|
||||
try:
|
||||
df = self._safe_request(ak.stock_zh_index_spot_em, symbol=name)
|
||||
if df is not None and not df.empty:
|
||||
row = df.iloc[0]
|
||||
return {
|
||||
"code": index_code,
|
||||
"name": name,
|
||||
"close": row.get('最新价', 0),
|
||||
"change_pct": row.get('涨跌幅', 0),
|
||||
"change": row.get('涨跌额', 0)
|
||||
}
|
||||
except Exception as e:
|
||||
print(f" akshare获取{name}失败: {e}")
|
||||
return None
|
||||
|
||||
try:
|
||||
# 上证指数
|
||||
df_sh = ak.stock_zh_index_spot_em(symbol="上证指数")
|
||||
if df_sh is not None and not df_sh.empty:
|
||||
overview["sh_index"] = {
|
||||
"code": "000001",
|
||||
"name": "上证指数",
|
||||
"close": df_sh.iloc[0].get('最新价', 0),
|
||||
"change_pct": df_sh.iloc[0].get('涨跌幅', 0),
|
||||
"change": df_sh.iloc[0].get('涨跌额', 0)
|
||||
}
|
||||
|
||||
# 深证成指
|
||||
df_sz = self._safe_request(ak.stock_zh_index_spot_em, symbol="深证成指")
|
||||
if df_sz is not None and not df_sz.empty:
|
||||
overview["sz_index"] = {
|
||||
"code": "399001",
|
||||
"name": "深证成指",
|
||||
"close": df_sz.iloc[0].get('最新价', 0),
|
||||
"change_pct": df_sz.iloc[0].get('涨跌幅', 0),
|
||||
"change": df_sz.iloc[0].get('涨跌额', 0)
|
||||
}
|
||||
|
||||
# 创业板指
|
||||
df_cyb = self._safe_request(ak.stock_zh_index_spot_em, symbol="创业板指")
|
||||
if df_cyb is not None and not df_cyb.empty:
|
||||
overview["cyb_index"] = {
|
||||
"code": "399006",
|
||||
"name": "创业板指",
|
||||
"close": df_cyb.iloc[0].get('最新价', 0),
|
||||
"change_pct": df_cyb.iloc[0].get('涨跌幅', 0),
|
||||
"change": df_cyb.iloc[0].get('涨跌额', 0)
|
||||
}
|
||||
sh_index = _get_index_data("000001", "000001.SH", "上证指数")
|
||||
if sh_index:
|
||||
overview["sh_index"] = sh_index
|
||||
sz_index = _get_index_data("399001", "399001.SZ", "深证成指")
|
||||
if sz_index:
|
||||
overview["sz_index"] = sz_index
|
||||
cyb_index = _get_index_data("399006", "399006.SZ", "创业板指")
|
||||
if cyb_index:
|
||||
overview["cyb_index"] = cyb_index
|
||||
except:
|
||||
pass
|
||||
|
||||
@@ -310,7 +323,7 @@ class SectorStrategyDataFetcher:
|
||||
try:
|
||||
import tushare as ts
|
||||
ts.set_token(tushare_token)
|
||||
self.ts_pro = ts.pro_api()
|
||||
self.ts_pro = ts.pro_api(timeout=float(os.getenv('TUSHARE_TIMEOUT', '15')))
|
||||
print(" [Tushare] ✅ 初始化成功")
|
||||
except Exception as e:
|
||||
print(f" [Tushare] 初始化失败: {e}")
|
||||
@@ -757,4 +770,3 @@ if __name__ == "__main__":
|
||||
print(f"\n... (总长度: {len(formatted_text)} 字符)")
|
||||
else:
|
||||
print(f"\n数据采集失败: {data.get('error', '未知错误')}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user