refactor(datasource): 底层fetch方法添加adj参数
TushareSource.fetch() 和 YFinanceSource.fetch() 新增 adj 参数支持 raw/qfq/hfq - TushareSource.fetch(adj='raw'): 内部路由到 fetch_index/fetch_stock_adj/fetch_etf_adj - YFinanceSource.fetch(adj='raw'): 内部路由到 fetch_adj() 或原始逻辑 - 添加 is_china_stock() 和 _is_etf_code() 方法用于资产类型判断
This commit is contained in:
@@ -196,22 +196,79 @@ class TushareSource:
|
||||
# 只支持中国交易所期货(.SHF上期所、.DCE大商所、.CZC郑商所)
|
||||
# NYMEX (.NYM) 和 ICE (.ICE) 走 YFinance
|
||||
return ".SHF" in code or ".DCE" in code or ".CZC" in code
|
||||
|
||||
def is_china_stock(self, code: str) -> bool:
|
||||
"""判断是否为A股股票(6位数字代码 + .SZ/.SH/.SS)"""
|
||||
# 股票代码:000001.SZ, 600000.SH 等
|
||||
# 区分指数:指数代码通常是 000xxx.SH, 399xxx.SZ, H30xxx.CSI
|
||||
# 股票代码通常是 00xxxx.SZ, 30xxxx.SZ, 60xxxx.SH, 000xxx.SH(部分)
|
||||
import re
|
||||
# 股票代码模式:6位数字 + .SZ/.SH/.SS
|
||||
# 排除指数:000xxx.SH (指数), 399xxx.SZ (指数), Hxxxxx.CSI (指数)
|
||||
if not re.match(r'^\d{6}\.(SZ|SH|SS)$', code):
|
||||
return False
|
||||
# 000xxx.SH 可能是指数也可能是股票,需要更细致判断
|
||||
# 简化处理:000/001/002/003 开头 + .SZ 是股票,600/601/603 开头 + .SH 是股票
|
||||
prefix = code[:3]
|
||||
suffix = code.split('.')[1]
|
||||
if suffix == 'SZ' and prefix in ['000', '001', '002', '003', '300']:
|
||||
return True
|
||||
if suffix == 'SH' and prefix in ['600', '601', '603', '605', '688']:
|
||||
return True
|
||||
return False
|
||||
|
||||
def fetch(self, code: str, start_date: str, end_date: str) -> Optional[pd.DataFrame]:
|
||||
def fetch(self, code: str, start_date: str, end_date: str, adj: str = 'raw') -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
通用数据获取(自动判断类型)
|
||||
通用数据获取(自动判断类型,支持 adj 参数)
|
||||
|
||||
Args:
|
||||
code: 代码
|
||||
start_date: 开始日期
|
||||
end_date: 结束日期
|
||||
adj: 复权类型 'raw'(原始) / 'qfq'(前复权) / 'hfq'(后复权),默认 'raw'
|
||||
|
||||
Returns:
|
||||
DataFrame with columns: date, open, high, low, close, volume
|
||||
adj='hfq' 时 A股 ETF 会额外返回 adj_factor, close_hfq
|
||||
"""
|
||||
if self.is_china_index(code):
|
||||
return self.fetch_index(code, start_date, end_date)
|
||||
elif self.is_futures(code):
|
||||
return self.fetch_futures(code, start_date, end_date)
|
||||
else:
|
||||
return None
|
||||
# 校验 adj 参数
|
||||
if adj not in ['raw', 'qfq', 'hfq']:
|
||||
raise ValueError(f"adj 参数必须是 'raw', 'qfq' 或 'hfq',当前: {adj}")
|
||||
|
||||
# 原始数据
|
||||
if adj == 'raw':
|
||||
if self.is_china_index(code):
|
||||
return self.fetch_index(code, start_date, end_date)
|
||||
elif self.is_futures(code):
|
||||
return self.fetch_futures(code, start_date, end_date)
|
||||
elif self.is_china_stock(code):
|
||||
return self.fetch_stock_adj(code, start_date, end_date, adj='raw')
|
||||
else:
|
||||
return None
|
||||
|
||||
# 复权数据
|
||||
if adj in ['qfq', 'hfq']:
|
||||
# A股股票复权
|
||||
if self.is_china_stock(code):
|
||||
return self.fetch_stock_adj(code, start_date, end_date, adj)
|
||||
# A股 ETF 仅支持 hfq
|
||||
elif self._is_etf_code(code):
|
||||
if adj == 'hfq':
|
||||
return self.fetch_etf_adj(code, start_date, end_date)
|
||||
else:
|
||||
raise ValueError(f"ETF 仅支持 adj='hfq'(后复权),当前: {adj}")
|
||||
else:
|
||||
# 指数/期货不支持复权
|
||||
raise ValueError(f"指数/期货不支持复权,adj='{adj}' 仅适用于股票/ETF")
|
||||
|
||||
def _is_etf_code(self, code: str) -> bool:
|
||||
"""判断是否为ETF代码"""
|
||||
# ETF代码:51xxxx.SH, 52xxxx.SH, 15xxxx.SZ, 16xxxx.SZ
|
||||
import re
|
||||
if not re.match(r'^\d{6}\.(SZ|SH)$', code):
|
||||
return False
|
||||
prefix = code[:2]
|
||||
return prefix in ['51', '52', '15', '16']
|
||||
|
||||
def fetch_etf_adj(self, code: str, start_date: str, end_date: str) -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
@@ -332,4 +389,65 @@ class TushareSource:
|
||||
|
||||
except Exception as e:
|
||||
print(f"Tushare下载交易日历失败: {e}")
|
||||
return pd.DatetimeIndex([])
|
||||
return pd.DatetimeIndex([])
|
||||
|
||||
def fetch_stock_adj(self, code: str, start_date: str, end_date: str, adj: str = 'hfq') -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
获取 A股股票复权价格数据
|
||||
|
||||
使用 pro_bar 接口获取前复权(qfq)或后复权(hfq)价格。
|
||||
|
||||
Args:
|
||||
code: 股票代码,如 '000001.SZ', '600000.SH'
|
||||
start_date: 开始日期 'YYYY-MM-DD'
|
||||
end_date: 结束日期 'YYYY-MM-DD'
|
||||
adj: 复权类型 'qfq'(前复权) 或 'hfq'(后复权),默认 'hfq'
|
||||
|
||||
Returns:
|
||||
DataFrame with columns: date, code, open, high, low, close, volume, adj_factor
|
||||
"""
|
||||
import tushare as ts
|
||||
|
||||
if adj not in ['qfq', 'hfq']:
|
||||
raise ValueError(f"adj 参数必须是 'qfq' 或 'hfq',当前: {adj}")
|
||||
|
||||
try:
|
||||
ts_code = code.replace('.SS', '.SH')
|
||||
|
||||
# 使用 pro_bar 接口获取复权数据
|
||||
df = ts.pro_bar(
|
||||
ts_code=ts_code,
|
||||
adj=adj,
|
||||
start_date=start_date.replace('-', ''),
|
||||
end_date=end_date.replace('-', ''),
|
||||
adjfactor=True # 返回复权因子
|
||||
)
|
||||
|
||||
if df is None or len(df) == 0:
|
||||
return None
|
||||
|
||||
# 标准化列名
|
||||
df = df.rename(columns={
|
||||
'ts_code': 'code',
|
||||
'trade_date': 'date',
|
||||
'vol': 'volume',
|
||||
})
|
||||
|
||||
# 转换日期格式
|
||||
df['date'] = pd.to_datetime(df['date'])
|
||||
df = df.set_index('date')
|
||||
df = df.sort_index()
|
||||
|
||||
# 恢复原始代码格式(.SS -> .SH 反转)
|
||||
df['code'] = code
|
||||
|
||||
# 标准化返回字段
|
||||
columns = ['code', 'open', 'high', 'low', 'close', 'volume']
|
||||
if 'adj_factor' in df.columns:
|
||||
columns.append('adj_factor')
|
||||
|
||||
return df[columns]
|
||||
|
||||
except Exception as e:
|
||||
print(f"Tushare下载股票复权数据 {code} 失败: {e}")
|
||||
return None
|
||||
Reference in New Issue
Block a user