refactor data sufficiency check and improve factor calculation validation

This commit is contained in:
Your Name 2025-10-08 15:36:03 +08:00
parent 1d1c04f85f
commit 9d5148e80c
2 changed files with 17 additions and 34 deletions

View File

@ -333,25 +333,8 @@ def _check_data_availability(
specs: Sequence[FactorSpec], specs: Sequence[FactorSpec],
) -> bool: ) -> bool:
"""检查证券数据是否足够计算所有请求的因子""" """检查证券数据是否足够计算所有请求的因子"""
# 检查所需的最大窗口 # 检查数据是否满足基本要求
close_windows = [spec.window for spec in specs if spec.name.startswith(("mom_", "volat_"))] if not check_data_sufficiency(ts_code, trade_date):
turnover_windows = [spec.window for spec in specs if spec.name.startswith("turn_")]
max_close_window = max(close_windows) if close_windows else 0
max_turn_window = max(turnover_windows) if turnover_windows else 0
# 获取时间序列数据
close_series = _fetch_series_values(broker, "daily", "close", ts_code, trade_date, max_close_window)
if max_close_window > 0 and not check_data_sufficiency(
close_series, max_close_window, "close", ts_code, trade_date
):
return False
turnover_series = _fetch_series_values(
broker, "daily_basic", "turnover_rate", ts_code, trade_date, max_turn_window
)
if max_turn_window > 0 and not check_data_sufficiency(
turnover_series, max_turn_window, "turnover_rate", ts_code, trade_date
):
return False return False
# 检查快照数据 # 检查快照数据
@ -456,21 +439,12 @@ def _compute_security_factors(
) )
# 数据有效性检查 # 数据有效性检查
if not close_series: if not check_data_sufficiency(ts_code, trade_date):
# 如果是因为数据库中没有数据就用debug级别记录 LOGGER.debug(
if not _check_stock_exists(broker, ts_code, trade_date): "数据不满足计算条件 ts_code=%s date=%s",
LOGGER.debug( ts_code, trade_date,
"股票当日无交易 ts_code=%s date=%s", extra=LOG_EXTRA
ts_code, trade_date, )
extra=LOG_EXTRA
)
else:
# 如果股票存在但缺少收盘价用warning级别记录
LOGGER.warning(
"股票数据缺失 ts_code=%s date=%s",
ts_code, trade_date,
extra=LOG_EXTRA
)
return {} return {}
turnover_series = _fetch_series_values( turnover_series = _fetch_series_values(

View File

@ -136,6 +136,15 @@ def check_data_sufficiency(
return False return False
return True return True
def check_series_sufficiency(
data: Sequence,
required_length: int,
field_name: str,
ts_code: str,
trade_date: str
) -> bool:
"""检查数据序列是否满足计算要求。 """检查数据序列是否满足计算要求。
Args: Args: