refactor(flask_api): fetch添加adj参数,fetch_with_adj简化

FlaskAPIDataSource.fetch() 新增 adj 参数,fetch_with_adj() 简化

- FlaskAPIDataSource.fetch(adj='raw'): 请求参数包含 adj
- fetch_with_adj(): 简化为 return self.fetch(adj=adj)(减少 ~120行)
- flask_server.py: 缓存逻辑已支持 adj 参数,无需修改
This commit is contained in:
2026-05-23 18:32:20 +08:00
parent c319fd42be
commit 7f2af6b470
2 changed files with 108 additions and 17 deletions

View File

@@ -61,30 +61,41 @@ class FlaskAPIDataSource:
code: str,
start_date: str,
end_date: str,
adj: str = 'raw',
asset_type: str = None,
timeframe: str = '1d'
) -> Optional[pd.DataFrame]:
"""
获取单只标的 OHLCV 数据
获取单只标的 OHLCV 数据(支持 adj 参数)
Args:
code: 标的代码
start_date: 开始日期 YYYY-MM-DD
end_date: 结束日期 YYYY-MM-DD
adj: 复权类型 'raw'(原始) / 'qfq'(前复权) / 'hfq'(后复权),默认 'raw'
asset_type: 资产类型(可选,用于覆盖自动检测)
timeframe: K线周期加密货币需要
Returns:
DataFrame with columns: date, open, high, low, close, volume
adj='hfq' 时 A股 ETF 会额外返回 adj_factor, close_hfq
示例:
# 原始价格
df = source.fetch("000300.SH", "2020-01-01", "2024-12-31")
# A股股票后复权
df = source.fetch("000001.SZ", "2020-01-01", "2024-12-31", adj='hfq')
"""
# 构建请求 URL
url = f"{self.base_url}{self.api_path}"
# 构建请求参数
# 构建请求参数(包含 adj
params = {
'code': code,
'start': start_date,
'end': end_date,
'adj': adj, # 添加 adj 参数
}
# 加密货币需要 timeframe 参数
@@ -296,6 +307,38 @@ class FlaskAPIDataSource:
print(f"{code} 净值获取失败: {e}")
return None
def fetch_with_adj(
self,
code: str,
start_date: str,
end_date: str,
adj: str = 'raw',
asset_type: str = None,
timeframe: str = '1d'
) -> Optional[pd.DataFrame]:
"""
获取 OHLCV 数据(支持复权参数)- 简化版
直接调用 fetch(adj=adj),无需重复实现。
Args:
code: 标的代码
start_date: 开始日期 YYYY-MM-DD
end_date: 结束日期 YYYY-MM-DD
adj: 复权参数raw/qfq/hfq默认 'raw'
asset_type: 资产类型(可选)
timeframe: K线周期加密货币需要
Returns:
DataFrame结构因 adj 参数略有不同
示例:
# A股股票后复权
df = source.fetch_with_adj("000001.SZ", "2020-01-01", "2024-12-31", adj='hfq')
"""
# 直接调用 fetch传递 adj 参数
return self.fetch(code, start_date, end_date, adj, asset_type, timeframe)
def get_health(self) -> Dict:
"""获取服务健康状态"""
# 先尝试 ohlcv 端点检查服务是否可用