mirror of
https://github.com/OpenByteInc/QuantDinger.git
synced 2026-09-28 15:22:56 +08:00
58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
from typing import Any, Dict, Optional
|
||
|
||
|
||
def safe_float_price(value: Any, default: Optional[float] = None) -> Optional[float]:
|
||
"""Coerce LLM/string prices to float; invalid values return default."""
|
||
if value is None:
|
||
return default
|
||
if isinstance(value, (int, float)):
|
||
if isinstance(value, float) and (value != value):
|
||
return default
|
||
return float(value)
|
||
try:
|
||
text = str(value).strip().replace(",", "")
|
||
if not text:
|
||
return default
|
||
return float(text)
|
||
except (TypeError, ValueError):
|
||
return default
|
||
|
||
|
||
def build_trend_outlook_summary(trend_outlook: Dict[str, Any], language: str) -> str:
|
||
"""Build the legacy API summary string for a multi-horizon trend outlook."""
|
||
if not trend_outlook:
|
||
return ""
|
||
is_zh = str(language or "").lower().startswith("zh")
|
||
|
||
def label(trend: str) -> str:
|
||
normalized = str(trend or "HOLD").upper()
|
||
if is_zh:
|
||
return {"BUY": "看多", "SELL": "看空", "HOLD": "震荡/中性"}.get(normalized, "震荡/中性")
|
||
return {"BUY": "bullish", "SELL": "bearish", "HOLD": "neutral / range"}.get(
|
||
normalized,
|
||
"neutral / range",
|
||
)
|
||
|
||
n24 = trend_outlook.get("next_24h") or {}
|
||
d3 = trend_outlook.get("next_3d") or {}
|
||
w1 = trend_outlook.get("next_1w") or {}
|
||
m1 = trend_outlook.get("next_1m") or {}
|
||
|
||
if is_zh:
|
||
return ";".join(
|
||
[
|
||
f"约24小时:{label(n24.get('trend'))}(强度 {n24.get('strength', 'neutral')})",
|
||
f"约3天:{label(d3.get('trend'))}(强度 {d3.get('strength', 'neutral')})",
|
||
f"约1周:{label(w1.get('trend'))}(强度 {w1.get('strength', 'neutral')})",
|
||
f"约1月:{label(m1.get('trend'))}(强度 {m1.get('strength', 'neutral')})",
|
||
]
|
||
)
|
||
return " | ".join(
|
||
[
|
||
f"~24h: {label(n24.get('trend'))} ({n24.get('strength', 'neutral')})",
|
||
f"~3d: {label(d3.get('trend'))} ({d3.get('strength', 'neutral')})",
|
||
f"~1w: {label(w1.get('trend'))} ({w1.get('strength', 'neutral')})",
|
||
f"~1m: {label(m1.get('trend'))} ({m1.get('strength', 'neutral')})",
|
||
]
|
||
)
|