fix: improve XAU price fallback with multiple symbol attempts

This commit is contained in:
2026-06-15 17:25:59 +00:00
parent 1defb34d47
commit 4969fa9aa6
+24 -4
View File
@@ -43,11 +43,14 @@ class XAUBriefAgent:
return self._get_gold_fallback() return self._get_gold_fallback()
def _get_gold_fallback(self): def _get_gold_fallback(self):
"""Fallback: dados da Binance (XAU/USDT).""" """Fallback: dados da Binance via par alternativo (GOLD/USDT)."""
result = {} result = {}
# Tenta XAU/BRL ou XAU/USDT na Binance
symbols = ["XAUUSDT", "GLDUSDT", "PAXGUSDT"]
for sym in symbols:
try: try:
r = requests.get( r = requests.get(
"https://api.binance.com/api/v3/ticker/24hr?symbol=XAUUSDT", f"https://api.binance.com/api/v3/ticker/24hr?symbol={sym}",
timeout=8 timeout=8
) )
if r.status_code == 200: if r.status_code == 200:
@@ -57,11 +60,13 @@ class XAUBriefAgent:
result["high"] = float(data.get("highPrice", 0)) result["high"] = float(data.get("highPrice", 0))
result["low"] = float(data.get("lowPrice", 0)) result["low"] = float(data.get("lowPrice", 0))
result["volume"] = float(data.get("quoteVolume", 0)) result["volume"] = float(data.get("quoteVolume", 0))
if result["xau_usd"] > 100: # preço válido de ouro
break
except: except:
pass continue
# Segundo fallback: exchangerate # Segundo fallback: exchangerate
if not result.get("xau_usd"): if not result.get("xau_usd") or result["xau_usd"] < 100:
try: try:
r = requests.get( r = requests.get(
"https://api.exchangerate-api.com/v4/latest/XAU", "https://api.exchangerate-api.com/v4/latest/XAU",
@@ -74,6 +79,21 @@ class XAUBriefAgent:
except: except:
pass pass
# Terceiro fallback: Metal-price.org API pública
if not result.get("xau_usd") or result["xau_usd"] < 100:
try:
r = requests.get(
"https://api.metals.live/v1/spot/gold",
timeout=8
)
if r.status_code == 200:
data = r.json()
if isinstance(data, list) and len(data) > 0:
result["xau_usd"] = data[0].get("gold", 0)
result["chp"] = 0
except:
pass
return result return result
# ── Treasury Yields (US 10Y) ─────────────────────────────────────────────── # ── Treasury Yields (US 10Y) ───────────────────────────────────────────────