Merge pull request #140 from intfoo/fix/index-quotes-change-pct-polling

fix(index-quotes): compute change_pct for custom data source
This commit is contained in:
wshy
2026-07-25 16:59:11 +08:00
committed by GitHub
+14
View File
@@ -884,6 +884,20 @@ class QuoteService:
if not keep or "symbol" not in keep:
return pl.DataFrame()
df = df.select(keep)
# 自定义源可能不提供 change_pct/change_amount, 按 last_price/prev_close 补算
# (TickFlow 路径在 _fetch_full_market_quotes 已算好, 此处只补缺失的)
if "change_pct" not in df.columns and "last_price" in df.columns and "prev_close" in df.columns:
# prev_close=0 → inf (非合法 JSON), prev_close=null → null; 用 when 守护
df = df.with_columns(
pl.when(pl.col("prev_close") != 0)
.then((pl.col("last_price") - pl.col("prev_close")) / pl.col("prev_close"))
.otherwise(None)
.alias("change_pct")
)
if "change_amount" not in df.columns and "last_price" in df.columns and "prev_close" in df.columns:
df = df.with_columns(
(pl.col("last_price") - pl.col("prev_close")).alias("change_amount")
)
# change_pct / amplitude: 小数 → 百分比 (统一指数展示口径)
for col in ("change_pct", "amplitude"):
if col in df.columns: