fix(stocksdk): normalize realtime change percentage

This commit is contained in:
Marquis03
2026-08-20 10:35:52 +08:00
parent a49c67c5b5
commit 177fca7ae8
2 changed files with 15 additions and 4 deletions
+10 -1
View File
@@ -203,7 +203,16 @@ class StockSDKProvider:
except bridge.StockSDKBridgeError as e: except bridge.StockSDKBridgeError as e:
logger.warning("stock-sdk realtime 拉取失败: %s", e) logger.warning("stock-sdk realtime 拉取失败: %s", e)
return [] return []
return result.get("rows") or [] rows = result.get("rows") or []
normalized: list[dict] = []
for row in rows:
item = dict(row)
# stock-sdk 的 changePercent 是百分数值(-1.15 = -1.15%);
# provider 入口契约统一使用小数制(-0.0115 = -1.15%)。
if item.get("change_pct") is not None:
item["change_pct"] = float(item["change_pct"]) / 100
normalized.append(item)
return normalized
# ---- instruments (标的维表) ---- # ---- instruments (标的维表) ----
def get_instruments(self, asset_type: str = "stock") -> list[dict]: def get_instruments(self, asset_type: str = "stock") -> list[dict]:
+5 -3
View File
@@ -80,12 +80,14 @@ def test_get_minute_datetime_is_beijing_wall_clock(monkeypatch):
assert df["symbol"][0] == "600519.SH" assert df["symbol"][0] == "600519.SH"
def test_get_realtime_passthrough(monkeypatch): def test_get_realtime_normalizes_change_pct_to_decimal(monkeypatch):
rows = [{"symbol": "600519.SH", "name": "贵州茅台", "last_price": 1200.0, rows = [{"symbol": "600519.SH", "name": "贵州茅台", "last_price": 1200.0,
"prev_close": 1194.0, "open": 1186.0, "high": 1203.0, "low": 1180.0, "volume": 16325}] "prev_close": 1194.0, "open": 1186.0, "high": 1203.0, "low": 1180.0,
"volume": 16325, "change_pct": -1.15}]
_patch_run_job(monkeypatch, {"realtime": {"ok": True, "op": "realtime", "rows": rows}}) _patch_run_job(monkeypatch, {"realtime": {"ok": True, "op": "realtime", "rows": rows}})
out = StockSDKProvider().get_realtime() out = StockSDKProvider().get_realtime()
assert out == rows assert abs(out[0]["change_pct"] - (-0.0115)) < 1e-12
assert rows[0]["change_pct"] == -1.15
required = {"symbol", "last_price", "prev_close", "open", "high", "low", "volume"} required = {"symbol", "last_price", "prev_close", "open", "high", "low", "volume"}
assert required <= set(out[0].keys()) assert required <= set(out[0].keys())