支持短合约代码自动补全交易所后缀

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
fish
2026-05-07 22:31:45 +08:00
parent ee3acd1c4d
commit 01309dd8ff
3 changed files with 19 additions and 4 deletions

View File

@@ -53,7 +53,9 @@ def health():
def run_pipeline(req: RunRequest):
ref_date = datetime.strptime(req.trade_date, "%Y%m%d").date() if req.trade_date else None
ts_code = req.ts_code or contracts.active_contract(req.symbol, ref_date)
if not req.ts_code:
if req.ts_code:
ts_code = contracts.normalize_ts_code(ts_code)
else:
print(f"[AUTO] {req.symbol} 当月主力 -> {ts_code}")
df = fetcher.fetch_contract(ts_code)
@@ -138,7 +140,8 @@ def run_range(req: RunRangeRequest):
@app.post("/api/v1/run/full")
def run_full(req: RunFullRequest):
"""拉取指定合约全部历史数据,保存 candles对所有可打分日期逐日打分并保存。"""
df = fetcher.fetch_contract(req.ts_code)
ts_code = contracts.normalize_ts_code(req.ts_code)
df = fetcher.fetch_contract(ts_code)
storage.save_candles(df)
results, warnings, total_days, scored_count = scorer.score_all(df)
@@ -149,7 +152,7 @@ def run_full(req: RunFullRequest):
skipped_count = total_days - scored_count
return {
"ts_code": req.ts_code,
"ts_code": ts_code,
"total_days": total_days,
"scored_count": scored_count,
"skipped_count": skipped_count,

View File

@@ -66,6 +66,18 @@ ROLLOVER_RULES: dict[str, dict] = {
}
def normalize_ts_code(ts_code: str) -> str:
"""把用户输入的短代码(如 FG2509)补全为带交易所后缀的完整代码(如 FG2509.ZCE)。"""
if "." in ts_code:
return ts_code
# 提取品种代码: 前两个字母
symbol = ts_code[:2]
if symbol not in ROLLOVER_RULES:
raise ValueError(f"未配置 {symbol} 的品种规则,无法自动补全交易所后缀")
exchange = ROLLOVER_RULES[symbol]["exchange"]
return f"{ts_code}.{exchange}"
def active_contract(symbol: str, today: Optional[date] = None) -> str:
"""按主力轮换规则,返回当日 ts_code(含交易所后缀)。"""
if symbol not in ROLLOVER_RULES: